-
Notifications
You must be signed in to change notification settings - Fork 244
/
worker.js
671 lines (556 loc) · 18.2 KB
/
worker.js
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
////////////////////////////////////////////////////////////////////
//// Telegram Bot using Cloudflare Worker ////
////////////////////////////////////////////////////////////////////
//// Author: Nikhil John ////
//// Repo: https://github.com/nikhiljohn10/telegram-bot-worker ////
//// License: MIT ////
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
//// Custom Cloudflare Environment Variables ////
//////////////////////////////////////////////////
// ENV_ADMIN_ID = 12345678 ( The chat_id of the admin telegram user which can be optained using /chatInfo command)
// ENV_BOT_HOST_FQDN = https://bot.example.com/
// ENV_BotLogger007Bot = ••••• (Bot Token. This one is the logging bot)
// ENV_CareFree007Bot = ••••• (Bot Token)
// ENV_PingTest007Bot = ••••• (Bot Token)
// ENV_WhatAmIDoingNowBot = ••••• (Bot Token)
/////////////////////////////
//// Bot Configurations ////
/////////////////////////////
// List of all commands available
const commands = {
'chatInfo': async (bot, req, args) => await bot.getChatInfo(req, args),
'ping': async (bot, req, args) => await bot.ping(req, args),
'toss': async (bot, req, args) => await bot.toss(req, args)
}
// Configurations
const bot_configs = [{
'bot_name': 'WhatAmIDoingNowBot',
'token': ENV_WhatAmIDoingNowBot,
'commands': {
'/chatInfo': commands.chatInfo,
'/ping': commands.ping
}
},
{
'bot_name': 'PingTest007Bot',
'token': ENV_PingTest007Bot,
'commands': {
'/chatInfo': commands.chatInfo,
'/ping': commands.ping
}
},
{
'bot_name': 'CareFree007Bot',
'token': ENV_CareFree007Bot,
'commands': {
'/chatInfo': commands.chatInfo,
'/ping': commands.ping,
'/toss': commands.toss
}
}
]
// Work in progress. Some bugs in logger exists.
const logger_config = {
'enabled': true, // Enable/Disable logger
'bot_name': 'BotLogger007Bot',
'token': ENV_BotLogger007Bot,
'chat_id': ENV_ADMIN_ID // Admin chat ID
}
///////////////////////////
//// Webhook Endpoint ////
///////////////////////////
class Webhook {
constructor(url, token) {
this.url = url
this.token = token
}
// trigger getMe command of BotAPI
async getMe() {
return await this.execute(this.url + '/getMe')
}
async set() {
const access_key = await sha256(this.token)
return await this.execute(this.url + '/setWebhook?url=' + ENV_BOT_HOST_FQDN + access_key)
}
async get() {
return await this.execute(this.url + '/getWebhookInfo')
}
async delete() {
return await this.execute(this.url + '/deleteWebhook')
}
async execute(url) {
const response = await fetch(url)
const result = await response.json()
return JSONResponse(result)
}
async process(url) {
const command = url.searchParams.get('command')
if (command == undefined) {
return this.error("No command found", 404)
}
// handles the url commands
switch (command) {
case 'setWebhook':
return await this.set()
case 'getWebhook':
return await this.get()
case 'delWebhook':
return await this.delete()
case 'getMe':
return await this.getMe()
case '':
return this.error("No command found", 404)
default:
return this.error("Invalid command", 400)
}
}
// handles error responses
error(message, status = 403) {
return JSONResponse({
error: message
}, status)
}
}
////////////////////////////
//// Generic Bot Class ////
////////////////////////////
class BotModel {
constructor(config) {
this.token = config.token
this.commands = config.commands
this.url = 'https://api.telegram.org/bot' + config.token
this.webhook = new Webhook(this.url, config.token)
}
// trigger sendAnimation command of BotAPI
async update(request) {
try {
this.message = request.content.message
if (this.message.hasOwnProperty('text')) {
// process text
// Test command and execute
if (!(await this.executeCommand(request))) {
// Test is not a command
await this.sendMessage(this.message.chat.id, "This is not a command")
}
} else if (this.message.hasOwnProperty('photo')) {
// process photo
console.log(this.message.photo)
} else if (this.message.hasOwnProperty('video')) {
// process video
console.log(this.message.video)
} else if (this.message.hasOwnProperty('animation')) {
// process animation
console.log(this.message.animation)
} else if (this.message.hasOwnProperty('locaiton')) {
// process locaiton
console.log(this.message.locaiton)
} else if (this.message.hasOwnProperty('poll')) {
// process poll
console.log(this.message.poll)
} else if (this.message.hasOwnProperty('contact')) {
// process contact
console.log(this.message.contact)
} else if (this.message.hasOwnProperty('dice')) {
// process dice
console.log(this.message.dice)
} else if (this.message.hasOwnProperty('sticker')) {
// process sticker
console.log(this.message.sticker)
} else if (this.message.hasOwnProperty('reply_to_message')) {
// process reply of a message
console.log(this.message.reply_to_message)
} else {
// process unknown type
console.log(this.message)
}
if (logger_config.enabled && !(this.message.hasOwnProperty('text'))) await this.sendMessage(this.message.chat.id, logJSONinHTML(this.message), 'HTML')
} catch (error) {
console.error(error)
return JSONResponse(error.message)
}
// return 200 OK response to every update request
return new Response('True', {
status: 200
})
}
// execute the custom bot commands from bot configurations
async executeCommand(req) {
let cmdArray = this.message.text.split(' ')
const command = cmdArray.shift()
const isCommand = Object.keys(this.commands).includes(command)
if (isCommand) {
await this.commands[command](this, req, cmdArray)
return true
}
return false
}
// trigger sendMessage command of BotAPI
async sendMessage(chat_id, text,
parse_mode = '',
disable_web_page_preview = false,
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendMessage?chat_id=' + chat_id + '&text=' + text
url = addURLOptions(url, {
"parse_mode": parse_mode,
"disable_web_page_preview": disable_web_page_preview,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger forwardMessage command of BotAPI
async forwardMessage(chat_id, from_chat_id, disable_notification = false, message_id) {
let url = this.url + '/sendMessage?chat_id=' + chat_id +
'&from_chat_id=' + from_chat_id +
'&message_id=' + message_id
if (disable_notification) url += '&disable_notification=' + disable_notification
url = addURLOptions(url, {
"disable_notification": disable_notification
})
await fetch(url)
}
// trigger sendPhoto command of BotAPI
async sendPhoto(chat_id, photo,
caption = '',
parse_mode = '',
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendPhoto?chat_id=' + chat_id + '&photo=' + photo
url = addURLOptions(url, {
"caption": caption,
"parse_mode": parse_mode,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger sendVideo command of BotAPI
async sendVideo(chat_id, video,
duration = 0,
width = 0,
height = 0,
thumb = '',
caption = '',
parse_mode = '',
supports_streaming = false,
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendVideo?chat_id=' + chat_id + '&video=' + video
url = addURLOptions(url, {
"duration": duration,
"width": width,
"height": height,
"thumb": thumb,
"caption": caption,
"parse_mode": parse_mode,
"supports_streaming": supports_streaming,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger sendAnimation command of BotAPI
async sendAnimation(chat_id, animation,
duration = 0,
width = 0,
height = 0,
thumb = '',
caption = '',
parse_mode = '',
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendAnimation?chat_id=' + chat_id + '&animation=' + animation
url = addURLOptions(url, {
"duration": duration,
"width": width,
"height": height,
"thumb": thumb,
"caption": caption,
"parse_mode": parse_mode,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger sendLocation command of BotAPI
async sendLocation(chat_id, latitude, longitude,
live_period = 0,
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendLocation?chat_id=' + chat_id + '&latitude=' + latitude + '&longitude=' + longitude
url = addURLOptions(url, {
"live_period": live_period,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger senPoll command of BotAPI
async sendPoll(chat_id, question, options,
is_anonymous = '', // Use 'false' to set it instead of Boolean false
type = '',
allows_multiple_answers = false,
correct_option_id = 0,
explanation = '',
explanation_parse_mode = '',
open_period = 0,
close_date = 0,
is_closed = false,
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendPoll?chat_id=' + chat_id + '&question=' + question + '&options=' + options
url = addURLOptions(url, {
"is_anonymous": is_anonymous,
"type": type,
"allows_multiple_answers": allows_multiple_answers,
"correct_option_id": correct_option_id,
"explanation": explanation,
"explanation_parse_mode": explanation_parse_mode,
"open_period": open_period,
"close_date": close_date,
"is_closed": is_closed,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// trigger senDice command of BotAPI
async sendDice(chat_id,
emoji = '',
disable_notification = false,
reply_to_message_id = 0) {
let url = this.url + '/sendDice?chat_id=' + chat_id
url = addURLOptions(url, {
"emoji": emoji,
"disable_notification": disable_notification,
"reply_to_message_id": reply_to_message_id
})
await fetch(url)
}
// bot api command to get user profile photos
async getUserProfilePhotos(user_id,
offset = 0,
limit = 0) {
let url = this.url + '/getUserProfilePhotos?user_id=' + user_id
url = addURLOptions(url, {
"offset": offset,
"limit": limit
})
const response = await fetch(url)
const result = await response.json()
return result.result.photos
}
}
/////////////////////////////////
//// Telegram Bot Endpoint ////
////////////////////////////////
class TelegramBot extends BotModel {
constructor(config) {
super(config)
}
// bot command: /toss
async toss(req, args) {
const outcome = (Math.floor(Math.random() * 2) == 0) ? 'Heads' : 'Tails'
await this.sendMessage(this.message.chat.id, outcome)
}
// bot command: /ping
async ping(req, args) {
const text = (args.length < 1) ? 'pong' : args.join(' ')
await this.sendMessage(this.message.chat.id, text)
}
// bot command: /chatInfo
async getChatInfo(req, args) {
await this.sendMessage(
this.message.chat.id,
logJSONinHTML(this.message.chat),
'HTML')
}
// Send all the profile pictures to user_id
async sendAllProfilePhotos(chat_id, user_id) {
const profilePhotos = await this.getUserProfilePhotos(user_id)
for (const item of profilePhotos) {
await this.sendPhoto(chat_id, item[0].file_id)
}
}
}
////////////////////////////
//// Logger Bot Class ////
////////////////////////////
class Logger {
constructor(config) {
this.enabled = config.enabled
this.chat_id = config.chat_id
this.token = config.token
this.url = 'https://api.telegram.org/bot' + config.token
this.webhook = new Webhook(this.url, config.token)
}
async load() {
if (this.enabled) this.access_key = await sha256(this.token)
}
async sendLog(req) {
if (this.enabled) {
const url = this.getURL(JSON.stringify({
'client': req.headers.get('cf-connecting-ip'),
'method': req.method,
'type': req.type,
'url': req.url,
'body': req.content
}, null, 2))
await fetch(url)
}
}
async debug(data) {
if (this.enabled) {
const text = ((typeof data) === "string") ? data : JSON.stringify(data, null, 2)
const url = this.getURL(text)
await fetch(url)
}
}
getURL(text) {
let url = this.url + '/sendMessage?chat_id=' + this.chat_id
url = addURLOptions(url, {
"text": preTagString(text),
"parse_mode": 'HTML',
"disable_web_page_preview": true,
"disable_notification": true
})
return url
}
}
//////////////////////////
//// Request Handler ////
//////////////////////////
class Handler {
constructor(configs) {
this.configs = configs
this.tokens = this.configs.map((item) => item.token)
if (logger_config.enabled) this.logger = new Logger(logger_config) // Optional
this.response = new Response()
}
// handles the request
async handle(request) {
await this.logger.load()
const url = new URL(request.url)
const url_key = url.pathname.substring(1).replace(/\/$/, "")
this.access_keys = await Promise.all(this.tokens.map(async (token) => await sha256(token)))
this.bot_id = this.access_keys.indexOf(url_key)
if (this.bot_id > -1) {
this.request = await this.processRequest(request)
this.bot = new TelegramBot({
'token': this.tokens[this.bot_id], // Bot Token
'access_key': this.access_keys[this.bot_id], // Access Key
'commands': this.configs[this.bot_id].commands // Bot commands
})
if (this.request.method === 'POST' && this.request.type.includes('application/json') && this.request.size > 6 && this.request.content.message) this.response = await this.bot.update(this.request)
else if (this.request.method === 'GET') this.response = await this.bot.webhook.process(url)
else this.response = this.error(this.request.content.error)
} else if (request.method === 'GET' && url_key === this.logger.access_key) {
this.response = await this.logger.webhook.process(url)
} else {
this.response = this.error("Invalid access key")
}
// Log access keys to console if access key is not acceptable
for( const id in this.access_keys) console.log(this.configs[id].bot_name,'Access Link -',ENV_BOT_HOST_FQDN+this.access_keys[id])
console.log('Logger Bot Access Link: ' + ENV_BOT_HOST_FQDN+this.logger.access_key)
await this.logger.sendLog(request)
return this.response
}
async processRequest(req) {
let request = req
request.size = parseInt(request.headers.get('content-length')) || 0
request.type = request.headers.get('content-type') || ''
if (request.size && request.type) request.content = await this.getContent(request)
else if (request.method == 'GET') request.content = {
message: 'Accessing webhook'
}
else request.content = {
message: '',
error: 'Invalid content type or body'
}
console.log(req)
return request
}
async getContent(request) {
try {
if (request.type.includes('application/json')) {
return await request.json()
} else if (request.type.includes('text/')) {
return await request.text()
} else if (request.type.includes('form')) {
const formData = await request.formData()
const body = {}
for (const entry of formData.entries()) {
body[entry[0]] = entry[1]
}
return body
} else {
const arrayBuff = await request.arrayBuffer()
const objectURL = URL.createObjectURL(arrayBuff)
return objectURL
}
} catch (error) {
console.error(error.message)
return {
message: '',
error: 'Invalid content/content type'
}
}
}
// handles error responses
error(message, status = 403) {
return JSONResponse({
error: message
}, status)
}
}
//////////////////////
//// Initializer ////
//////////////////////
// Initialize new request handler
const handler = new Handler(bot_configs)
// Listen to all fetch events received by worker
addEventListener('fetch', event => {
event.respondWith(handler.handle(event.request))
})
////////////////////////////
//// Utility functions ////
////////////////////////////
// Generate JSON response
function JSONResponse(data, status = 200) {
const init = {
status: status,
headers: {
'content-type': 'application/json'
}
}
return new Response(JSON.stringify(data, null, 2), init)
}
// SHA256 Hash function
async function sha256(message) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message)
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer)
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer))
// convert bytes to hex string
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('')
return hashHex
}
// Stringify JSON and add <pre> tag HTML
function logJSONinHTML(data) {
return preTagString(JSON.stringify(data, null, 2))
}
function preTagString(str) {
return '<pre>' + str + '</pre>'
}
// Add options in URL
function addURLOptions(urlstr, options = {}) {
let url = urlstr
for (const key of Object.keys(options)) {
if (options[key]) url += '&' + key + '=' + options[key]
}
return url
}