-
Notifications
You must be signed in to change notification settings - Fork 11
/
link-listener.ts
96 lines (85 loc) · 3.08 KB
/
link-listener.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Context } from "grammy";
import { bot } from ".";
import { askToExpand } from "./actions/ask-to-expand";
import { saveToCache } from "./helpers/cache";
import { LINK_REGEX } from "./helpers/link-regex";
import { createSettings, getSettings } from "./helpers/api";
import { expandLink } from "./actions/expand-link";
import { deleteMessage } from "./actions/delete-message";
import { isDribbble, isHackerNews, isInstagram, isPosts, isReddit, isSpotify, isTikTok } from "./helpers/platforms";
import { trackEvent } from "./helpers/analytics";
import { showBotActivity } from "./actions/show-bot-activity";
import { isBanned } from "./helpers/banned";
bot.on("message::url", async (ctx: Context) => {
if (!ctx.msg) return;
// User context
const userInfo = {
username: ctx.from?.username,
firstName: ctx.from?.first_name,
lastName: ctx.from?.last_name,
userId: ctx.from?.id,
};
// Message context
const chatId = ctx.msg?.chat.id;
if (isBanned(chatId)) return;
const msgId = ctx.msg?.message_id;
const isDeletable = !ctx.msg?.caption; // deletable if not a caption of media
const entities = ctx.entities(); // all links in message
const message = ctx.msg?.text ?? ctx.msg?.caption ?? ""; // text or caption
const messageWithNoLinks = entities.reduce((msg, entity) => {
if (entity.type === "url") {
return msg.replace(entity.text, "");
}
return msg;
}, message);
// Get autoexpand settings for this chat
const settings = await getSettings(chatId);
const autoexpand = settings?.autoexpand;
// Create default settings for this chat if they don’t exist
if (!settings) {
await createSettings(chatId, false, true, false);
}
// Loop through all links in message
entities.forEach(async (entity, index) => {
const url = entity.text;
const matchingLink = LINK_REGEX.test(url);
// Ignore if not a link from supported sites
if (!matchingLink) return;
showBotActivity(ctx, chatId);
const identifier = `${ctx.msg?.chat?.id}:${ctx.msg?.message_id}:${index}`;
if (autoexpand) {
// Expand link automatically with provided context
await expandLink(ctx, url, messageWithNoLinks, userInfo, "auto");
// Delete message if it’s not a caption
if (isDeletable) deleteMessage(chatId, msgId, ctx);
// Track autoexpand event and platform
const insta = isInstagram(url);
const tiktok = isTikTok(url);
const posts = isPosts(url);
const hn = isHackerNews(url);
const dribbble = isDribbble(url);
const reddit = isReddit(url);
const spotify = isSpotify(url);
const platform = insta
? "instagram"
: tiktok
? "tiktok"
: posts
? "posts"
: hn
? "hackernews"
: dribbble
? "dribbble"
: reddit
? "reddit"
: spotify
? "spotify"
: "twitter";
trackEvent(`expand.auto.${platform}`);
} else {
// Save message context to cache then ask to expand
await saveToCache(identifier, ctx);
await askToExpand(ctx, identifier, url, isDeletable);
}
});
});