-
Notifications
You must be signed in to change notification settings - Fork 2
/
events.ts
185 lines (168 loc) · 4.8 KB
/
events.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
import { InlineKeyboard } from "https://deno.land/x/[email protected]/mod.ts";
import { bot, socialLayerClient } from "./constants.ts";
import { formatEvent } from "./format.ts";
import { markdownv2 as format } from "https://deno.land/x/[email protected]/mod.ts";
import { parseDate } from "./client.ts";
export async function sendEventsRaw(
groupIds: number[],
offset: number,
lastLoadMoreMessageId: number | undefined,
chatId: number,
is_topic_message: boolean,
message_thread_id: number | undefined,
startISO: string,
endISO: string,
): Promise<
{
offset: number;
lastLoadMoreMessageId: number | undefined;
} | void
> {
const limit = 10;
let newOffset = offset;
const { events, hasNextPage } = await socialLayerClient.getEventsRaw(
groupIds,
startISO,
endISO,
limit,
offset,
);
const eventsCount = events.length;
const hasEvents = eventsCount > 0;
const spots = new Set(
events.map((value) => {
return value.location;
}),
);
const introMessage = offset === 0
? `Hey there, early birds and night owls! Guess what? We've got a day crammed with awesomeness ${
hasNextPage ? `more than ${eventsCount}` : eventsCount
} fantastic events spread across ${spots.size} lively spots. Dive in and enjoy!`
: "";
const groupInfos = await socialLayerClient.getGroupInfos(groupIds);
const info = groupInfos.at(0);
const appPromotionMessage = "Check out app.sola.day... to see more!";
if (hasEvents && info) {
const groupUrl = `https://${info.username}.sola.day`;
console.debug(
`${lastLoadMoreMessageId || "undefined lastLoadMoreMessageId"}`,
);
if (lastLoadMoreMessageId) {
console.debug("delete");
try {
await bot.api.deleteMessage(chatId, lastLoadMoreMessageId);
} catch (error) {
console.error("Failed to delete previous 'Load More' message:", error);
}
}
newOffset += events.length;
const formattedEvents = events.map((event) =>
formatEvent(event, info.username)
).map((msg) => msg.caption)
.join("\n\n");
const messageToSend = `${
introMessage.length != 0
? `${format.bold(format.escape(`${introMessage}\n`))}${
format.underline(format.escape(`${groupUrl}\n\n`))
}`
: ""
}${formattedEvents}${
!hasNextPage
? format.italic(
format.escape(`\n\nThat's all for now!${appPromotionMessage}`),
)
: ""
}`;
console.debug(`${messageToSend}`);
await bot.api.sendMessage(chatId, messageToSend, {
parse_mode: "MarkdownV2",
message_thread_id: is_topic_message ? message_thread_id : undefined,
});
if (hasNextPage) {
const inlineKeyboard = hasNextPage
? new InlineKeyboard().text("Load More", "load_more")
: undefined;
const loadMoreMessage = await bot.api.sendMessage(
chatId,
`More results available! ${appPromotionMessage}`,
{
reply_markup: inlineKeyboard,
message_thread_id: is_topic_message ? message_thread_id : undefined,
},
);
return {
offset: newOffset,
lastLoadMoreMessageId: loadMoreMessage.message_id,
};
}
} else {
await bot.api.sendMessage(
chatId,
`🙈 No events today. ${appPromotionMessage}`,
{
message_thread_id: is_topic_message ? message_thread_id : undefined,
},
);
}
}
export async function sendEvents(
groupIds: number[],
offset: number,
lastLoadMoreMessageId: number | undefined,
chatId: number,
is_topic_message: boolean,
message_thread_id: number | undefined,
startDateInput: string,
days: number,
): Promise<
{
offset: number;
lastLoadMoreMessageId: number | undefined;
} | void
> {
const groupInfo = await socialLayerClient.getGroupTimestamp(groupIds);
const timezone = groupInfo.length === 0 ? "Asia/Shanghai" : groupInfo[0];
// Parse date inputs
const startDate = parseDate(startDateInput, timezone);
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + days);
const startISO = startDate.toISOString();
const endISO = endDate.toISOString();
const res = await sendEventsRaw(
groupIds,
offset,
lastLoadMoreMessageId,
chatId,
is_topic_message,
message_thread_id,
startISO,
endISO,
);
return res;
}
export async function sendTodayEvents(
groupIds: number[],
offset: number,
lastLoadMoreMessageId: number | undefined,
chatId: number,
is_topic_message: boolean,
message_thread_id: number | undefined,
days: number,
): Promise<
{
offset: number;
lastLoadMoreMessageId: number | undefined;
} | void
> {
const res = await sendEvents(
groupIds,
offset,
lastLoadMoreMessageId,
chatId,
is_topic_message,
message_thread_id,
"today",
days,
);
return res;
}