-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
384 lines (340 loc) · 11.6 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { Context, session } from "https://deno.land/x/[email protected]/mod.ts";
import { freeStorage } from "https://deno.land/x/[email protected]/free/src/mod.ts";
import { autoThread } from "https://raw.githubusercontent.com/grammyjs/auto-thread/main/src/mod.ts";
import {
bot,
GLOBAL_KEY,
globalStorage,
socialLayerClient,
TEMPORARY_KEY,
temporaryStorage,
} from "./constants.ts";
import { formatGroups, parseGroupId } from "./format.ts";
import { sendEventsRaw, sendTodayEvents } from "./events.ts";
import {
sendMessageWithThread,
stringToMap,
stringToSet,
} from "./utilities.ts";
import { handleSubscription } from "./subscription.ts";
import { SolaGroup } from "./types.ts";
import { datetime } from "https://deno.land/x/[email protected]/mod.ts";
import { parseDate } from "./client.ts";
// Stores data per user-chat combination.
function getSessionKey(ctx: Context): string | undefined {
const message_thread_id = ctx.msg?.message_thread_id;
const is_topic_message = ctx.msg?.is_topic_message ?? false;
// Give every user their one personal session storage per chat with the bot
// (an independent session for each group and their private chat)
return ctx.chat === undefined
? undefined
: TEMPORARY_KEY(ctx.chat.id, is_topic_message, message_thread_id);
}
bot.use(session({
type: "multi",
temporary: {
initial: () => ({
groupId: undefined,
offset: 0,
eventIds: [],
lastLoadMoreMessageId: undefined,
lastEndISO: undefined,
lastStartISO: undefined,
hour: 7,
days: 1,
subscription: "",
isSubs: false,
}),
storage: freeStorage(bot.token),
getSessionKey,
},
global: {
initial: () => ({ keys: "" }),
storage: freeStorage(bot.token),
getSessionKey: () => GLOBAL_KEY,
},
}));
// 英文版本
bot.api.setMyCommands([
{ command: "start", description: "Start using the bot to receive greetings and instructions." },
{ command: "list", description: "List all ongoing group activities that are currently available for subscription." },
{
command: "subs",
description: "<group name> Subscribe to activity updates for the specified group. Use `hour=<number>` and/or `days=<number>` to customize notification settings.",
},
{
command: "query",
description: "<group name> Query activity details for the specified group. Use `start=<date>`, `end=<date>`, and/or `days=<number>` to filter results.",
},
{
command: "status",
description: "Check the current subscription status for the channel, including the groups subscribed to and their notification settings.",
},
]);
bot.use(autoThread());
bot.command("start", async (ctx) => {
await ctx.reply(
`👋 Welcome to the Sola Events Bot!
To get started, you can use the following commands:
- /subs <group_name>: Subscribe to daily event updates for a specific group. You can also customize notification settings with "hour=<number>" and/or "days=<number>". For example, "/subs my_group hour=8 days=3".
- /query <group_name>: Query activity details for the specified group. Use "start=<date>", "end=<date>", and/or "days=<number>" to filter results. For example, "/query my_group start=2023-10-01 end=2023-10-31".
- /status: Check the current subscription status for the channel, including the groups subscribed to and their notification settings.
- /list: List all ongoing group activities that are currently available for subscription.
For more information on how to use this bot and see examples of commands in context, please check out the source code at https://github.com/sociallayer-im/sociallayer-bot.`,
{ parse_mode: "Markdown" }
);
});
import { markdownv2 as format } from "https://deno.land/x/[email protected]/mod.ts";
bot.command("status", async (ctx) => {
const subscriptionSet: Set<number> = stringToSet(
ctx.session.temporary.subscription,
);
const hour = ctx.session.temporary.hour;
const days = ctx.session.temporary.days;
const subscriptionArray = [...subscriptionSet]
.filter((groupId) => !isNaN(groupId));
const groupInfos = await socialLayerClient.getGroupInfos(subscriptionArray);
const formattedGroupInfos = groupInfos
.map(({ username, timezone }) =>
format.escape(
`- Group: ${username}\n- Notification Time: ${hour} hour(s) (GMT ${timezone})\n- Displayed Days: ${days} day(s) (starting from today)`,
)
)
.join("\n");
const groupCount = groupInfos.length;
let message;
if (groupCount === 0) {
message = "No groups subscribed";
} else if (groupCount === 1) {
message = `Subscribed group:\n${formattedGroupInfos}`;
} else {
message = `Subscribed groups:\n${formattedGroupInfos}`;
}
await ctx.reply(message, { parse_mode: "MarkdownV2" });
});
bot.command("global", async (ctx) => {
await ctx.reply(`${ctx.session.global.keys || "none"}`);
});
bot.command("clean", async (ctx) => {
ctx.session.global.keys = "";
await ctx.reply("clean!");
});
bot.command("list", async (ctx) => {
const groups: SolaGroup[] = await socialLayerClient.listGroups();
const listGroupId = formatGroups(groups);
ctx.reply(listGroupId, { parse_mode: "HTML" });
});
bot.callbackQuery("load_more", async (ctx) => {
const chatId = ctx.chat?.id;
const message_thread_id = ctx.msg?.message_thread_id;
const is_topic_message = ctx.msg?.is_topic_message ?? false;
const {
groupId,
offset,
lastLoadMoreMessageId,
lastStartISO,
lastEndISO,
isSubs,
subscription,
} = ctx.session.temporary;
console.debug(`load_more: ${lastLoadMoreMessageId || "none"}`);
if (chatId) {
const res = await sendEventsRaw(
(!isSubs && groupId) ? [groupId] : Array.from(stringToSet(subscription)),
offset,
lastLoadMoreMessageId,
chatId,
is_topic_message,
message_thread_id,
lastStartISO ? lastStartISO : "today",
lastEndISO ? lastEndISO : "tomorrow",
);
if (res) {
const { offset, lastLoadMoreMessageId } = res;
ctx.session.temporary.offset = offset;
ctx.session.temporary.lastLoadMoreMessageId = lastLoadMoreMessageId;
}
}
});
Deno.cron("notify", "0 * * * *", async () => {
console.log("每小时一次!");
const storage = globalStorage();
const global = await storage.read(GLOBAL_KEY);
const globalMap = stringToMap(global.keys);
for (const [chatId, ids] of globalMap) {
for (const message_thread_id of ids) {
console.log(`${chatId} ${message_thread_id}`);
const storage = temporaryStorage();
const is_topic_message = message_thread_id !== 0;
const key = TEMPORARY_KEY(chatId, is_topic_message, message_thread_id);
const { offset, subscription, hour, days } = await storage
.read(key);
const localTime = await getLocalTime(
Array.from(stringToSet(subscription)),
hour,
);
if (localTime) {
const res = await sendTodayEvents(
Array.from(stringToSet(subscription)),
offset,
undefined,
chatId,
is_topic_message,
message_thread_id,
days,
);
if (res) {
const { offset, lastLoadMoreMessageId } = res;
await storage.write(key, {
groupId: undefined,
offset,
lastLoadMoreMessageId,
lastStartISO: "today",
lastEndISO: "tomorrow",
hour,
days,
isSubs: true,
subscription,
});
}
}
}
}
});
async function getLocalTime(
subscription: number[],
hour: number,
): Promise<boolean> {
try {
const groupInfo = await socialLayerClient.getGroupTimestamp(subscription);
const timezone = groupInfo.length === 0 ? "Asia/Shanghai" : groupInfo[0];
const date = datetime().toZonedTime(timezone);
return date.hour === hour;
} catch (error) {
console.error(`Failed to get group timezone for ${subscription}: ${error}`);
return false;
}
}
Deno.cron("breath", "*/1 * * * *", () => {
console.log("Breath at every 1th minute.");
});
bot.command("subs", async (ctx) => {
const args = ctx.match.split(" ");
const groupIdInput = args.shift();
const hourInput = args.find((arg) => arg.startsWith("hour="))?.split(
"=",
)[1];
const daysInput = args.find((arg) => arg.startsWith("days="))?.split("=")[1];
let groupId = groupIdInput
? await socialLayerClient.queryGroup(groupIdInput)
: undefined;
if (groupId == undefined) {
groupId = parseGroupId(groupIdInput);
}
const chatId = ctx.chat?.id;
const message_thread_id = ctx.msg?.message_thread_id;
const is_topic_message = ctx.msg?.is_topic_message ?? false;
await handleSubscription(
ctx,
chatId,
groupId,
hourInput,
daysInput,
true,
message_thread_id,
is_topic_message,
);
});
// Main command handler for /query
bot.command("query", async (ctx) => {
const args = ctx.match.split(" ");
const groupIdInput = args.shift();
const startDateInput = args.find((arg) => arg.startsWith("start="))?.split(
"=",
)[1];
const endDateInput = args.find((arg) => arg.startsWith("end="))?.split(
"=",
)[1];
const daysInput = args.find((arg) => arg.startsWith("days="))?.split("=")[1];
let groupId = groupIdInput
? await socialLayerClient.queryGroup(groupIdInput)
: undefined;
if (groupId == undefined) {
groupId = parseGroupId(groupIdInput);
}
const chatId = ctx.chat?.id;
const message_thread_id = ctx.msg?.message_thread_id;
const is_topic_message = ctx.msg?.is_topic_message ?? false;
if (groupId && chatId) {
ctx.session.temporary.groupId = groupId;
ctx.session.temporary.offset = 0;
const groupInfo = await socialLayerClient.getGroupTimestamp([groupId]);
const timezone = groupInfo.length === 0 ? "Asia/Shanghai" : groupInfo[0];
let startDate: Date;
let endDate: Date;
try {
try {
if (!startDateInput) {
startDate = parseDate("today", timezone);
} else {
startDate = parseDate(startDateInput, timezone);
}
if (endDateInput) {
endDate = parseDate(endDateInput, timezone);
} else if (daysInput) {
endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + parseInt(daysInput));
} else {
endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 1);
}
} catch (error) {
console.log(`query error: ${error}`);
await sendMessageWithThread(
chatId,
`❌ Invalid date input. Please try again.`,
message_thread_id,
is_topic_message,
);
return;
}
const startISO = startDate.toISOString();
const endISO = endDate.toISOString();
const res = await sendEventsRaw(
[groupId],
0,
undefined,
chatId,
is_topic_message,
message_thread_id,
startISO,
endISO,
);
if (res) {
const { offset, lastLoadMoreMessageId } = res;
ctx.session.temporary.offset = offset;
ctx.session.temporary.lastLoadMoreMessageId = lastLoadMoreMessageId;
ctx.session.temporary.lastStartISO = startISO;
ctx.session.temporary.lastEndISO = endISO;
ctx.session.temporary.isSubs = false;
ctx.session.temporary.groupId = groupId;
}
} catch (error) {
console.log(`query error: ${error}`);
await sendMessageWithThread(
chatId,
`❌ Internal error ${error}. Please try again.`,
message_thread_id,
is_topic_message,
);
}
} else {
await sendMessageWithThread(
chatId,
"❌ Invalid group ID. Please try again with /query <groupId>.",
message_thread_id,
is_topic_message,
);
}
});
await bot.start();