Skip to content

Commit 23457be

Browse files
committed
telegram commands cleanup
1 parent 8738526 commit 23457be

File tree

1 file changed

+68
-72
lines changed

1 file changed

+68
-72
lines changed

packages/main/src/telegram_bot.ts

Lines changed: 68 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ export default class TelegramBot extends TelegramApi {
3838
// bot command: /code
3939
code = async (update: TelegramUpdate): Promise<Response> =>
4040
((url) =>
41-
(update.inline_query &&
42-
this.answerInlineQuery(update.inline_query.id, [
43-
new TelegramInlineQueryResultArticle(url),
44-
])) ??
45-
this.sendMessage(update.message?.chat.id ?? 0, url))(
41+
update.inline_query
42+
? this.answerInlineQuery(update.inline_query.id, [
43+
new TelegramInlineQueryResultArticle(url),
44+
])
45+
: this.sendMessage(update.message?.chat.id ?? 0, url))(
4646
"https://github.com/codebam/cf-workers-telegram-bot"
4747
);
4848

@@ -137,11 +137,11 @@ export default class TelegramBot extends TelegramApi {
137137
.then((response) => responseToJSON(response))
138138
.then((json) =>
139139
((message) =>
140-
(update.inline_query &&
141-
this.answerInlineQuery(update.inline_query.id, [
142-
new TelegramInlineQueryResultArticle(message),
143-
])) ||
144-
this.sendMessage(update.message?.chat.id ?? 0, message))(
140+
update.inline_query
141+
? this.answerInlineQuery(update.inline_query.id, [
142+
new TelegramInlineQueryResultArticle(message),
143+
])
144+
: this.sendMessage(update.message?.chat.id ?? 0, message))(
145145
`Kanye says... ${json.quote}`
146146
)
147147
)
@@ -154,19 +154,19 @@ export default class TelegramBot extends TelegramApi {
154154
.then((joke) => joke as Joke)
155155
.then((joke_response) =>
156156
((message) =>
157-
(update.inline_query &&
158-
this.answerInlineQuery(
159-
update.inline_query.id,
160-
[
161-
new TelegramInlineQueryResultArticle(
162-
message,
163-
joke_response.joke ?? joke_response.setup,
164-
"HTML"
165-
),
166-
],
167-
0
168-
)) ??
169-
this.sendMessage(update.message?.chat.id ?? 0, message, "HTML"))(
157+
update.inline_query
158+
? this.answerInlineQuery(
159+
update.inline_query.id,
160+
[
161+
new TelegramInlineQueryResultArticle(
162+
message,
163+
joke_response.joke ?? joke_response.setup,
164+
"HTML"
165+
),
166+
],
167+
0
168+
)
169+
: this.sendMessage(update.message?.chat.id ?? 0, message, "HTML"))(
170170
joke_response.joke ??
171171
`${joke_response.setup}\n\n<tg-spoiler>${joke_response.delivery}</tg-spoiler>`
172172
)
@@ -177,62 +177,59 @@ export default class TelegramBot extends TelegramApi {
177177
fetch("https://shibe.online/api/shibes")
178178
.then((response) => response.json())
179179
.then((json) => json as [string])
180-
.then(
181-
(shibe_response) =>
182-
(update.inline_query &&
183-
this.answerInlineQuery(
180+
.then((shibe_response) =>
181+
update.inline_query
182+
? this.answerInlineQuery(
184183
update.inline_query.id,
185184
[new TelegramInlineQueryResultPhoto(shibe_response[0])],
186185
0
187-
)) ??
188-
this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0])
186+
)
187+
: this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0])
189188
);
190189

191190
// bot command: /cat
192191
cat = async (update: TelegramUpdate): Promise<Response> =>
193192
fetch("https://meow.senither.com/v1/random")
194193
.then((response) => response.json())
195194
.then((json) => json as { data: { url: string } })
196-
.then(
197-
(json) =>
198-
(update.inline_query &&
199-
this.answerInlineQuery(
195+
.then((json) =>
196+
update.inline_query
197+
? this.answerInlineQuery(
200198
update.inline_query.id,
201199
[new TelegramInlineQueryResultPhoto(json.data.url)],
202200
0
203-
)) ??
204-
this.sendPhoto(update.message?.chat.id ?? 0, json.data.url)
201+
)
202+
: this.sendPhoto(update.message?.chat.id ?? 0, json.data.url)
205203
);
206204

207205
// bot command: /bored
208206
bored = async (update: TelegramUpdate): Promise<Response> =>
209207
fetch("https://boredapi.com/api/activity/")
210208
.then((response) => responseToJSON(response))
211209
.then((json) => json as Bored)
212-
.then(
213-
(bored_response) =>
214-
(update.inline_query &&
215-
this.answerInlineQuery(
210+
.then((bored_response) =>
211+
update.inline_query
212+
? this.answerInlineQuery(
216213
update.inline_query.id,
217214
[new TelegramInlineQueryResultArticle(bored_response.activity)],
218215
0
219-
)) ??
220-
this.sendMessage(
221-
update.message?.chat.id ?? 0,
222-
bored_response.activity
223-
)
216+
)
217+
: this.sendMessage(
218+
update.message?.chat.id ?? 0,
219+
bored_response.activity
220+
)
224221
);
225222

226223
// bot command: /epoch
227224
epoch = async (update: TelegramUpdate): Promise<Response> =>
228225
((seconds) =>
229-
(update.inline_query &&
230-
this.answerInlineQuery(
231-
update.inline_query.id,
232-
[new TelegramInlineQueryResultArticle(seconds)],
233-
0
234-
)) ??
235-
this.sendMessage(update.message?.chat.id ?? 0, seconds))(
226+
update.inline_query
227+
? this.answerInlineQuery(
228+
update.inline_query.id,
229+
[new TelegramInlineQueryResultArticle(seconds)],
230+
0
231+
)
232+
: this.sendMessage(update.message?.chat.id ?? 0, seconds))(
236233
Math.floor(Date.now() / 1000).toString()
237234
);
238235

@@ -241,15 +238,14 @@ export default class TelegramBot extends TelegramApi {
241238
((key) =>
242239
this.get_set
243240
.get(key)
244-
.then(
245-
(value) =>
246-
(update.inline_query &&
247-
this.answerInlineQuery(
241+
.then((value) =>
242+
update.inline_query
243+
? this.answerInlineQuery(
248244
update.inline_query.id,
249245
[new TelegramInlineQueryResultArticle(value ?? "")],
250246
0
251-
)) ??
252-
this.sendMessage(update.message?.chat.id ?? 0, value ?? "")
247+
)
248+
: this.sendMessage(update.message?.chat.id ?? 0, value ?? "")
253249
))(args[1]);
254250

255251
// bot command: /set
@@ -299,24 +295,24 @@ export default class TelegramBot extends TelegramApi {
299295
// bot command: /roll
300296
roll = async (update: TelegramUpdate, args: string[]): Promise<Response> =>
301297
((outcome, message) =>
302-
(update.inline_query &&
303-
this.answerInlineQuery(update.inline_query.id, [
304-
new TelegramInlineQueryResultArticle(
298+
update.inline_query
299+
? this.answerInlineQuery(update.inline_query.id, [
300+
new TelegramInlineQueryResultArticle(
301+
message(
302+
update.inline_query.from.username,
303+
update.inline_query.from.first_name,
304+
outcome
305+
)
306+
),
307+
])
308+
: this.sendMessage(
309+
update.message?.chat.id ?? 0,
305310
message(
306-
update.inline_query.from.username,
307-
update.inline_query.from.first_name,
311+
update.message?.from.username ?? "",
312+
update.message?.from.first_name ?? "",
308313
outcome
309314
)
310-
),
311-
])) ??
312-
this.sendMessage(
313-
update.message?.chat.id ?? 0,
314-
message(
315-
update.message?.from.username ?? "",
316-
update.message?.from.first_name ?? "",
317-
outcome
318-
)
319-
))(
315+
))(
320316
Math.floor(Math.random() * (parseInt(args[1]) || 6 - 1 + 1) + 1),
321317
(username: string, first_name: string, outcome: number) =>
322318
`${first_name ?? username} rolled a ${

0 commit comments

Comments
 (0)