-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbot.py
792 lines (635 loc) · 23.8 KB
/
bot.py
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
import os
import re
import logging
import uuid
import asyncio
from urllib.parse import urlparse
import aiohttp
from aiohttp import web
import json
from .chat import Chat, Sender
from .reloader import run_with_reloader
__author__ = "Stepan Zastupov"
__copyright__ = "Copyright 2015-2017 Stepan Zastupov"
__license__ = "MIT"
API_URL = "https://api.telegram.org"
API_TIMEOUT = 60
RETRY_TIMEOUT = 30
RETRY_CODES = [429, 500, 502, 503, 504]
# Message types to be handled by bot.handle(...)
MESSAGE_TYPES = [
"location",
"photo",
"document",
"audio",
"voice",
"sticker",
"contact",
"venue",
"video",
"game",
"delete_chat_photo",
"new_chat_photo",
"delete_chat_photo",
"new_chat_member",
"left_chat_member",
"new_chat_title",
"group_chat_created",
"successful_payment",
]
# Update types for
MESSAGE_UPDATES = [
"message",
"edited_message",
"channel_post",
"edited_channel_post",
"successful_payment",
]
logger = logging.getLogger("aiotg")
class Bot:
"""Telegram bot framework designed for asyncio
:param str api_token: Telegram bot token, ask @BotFather for this
:param int api_timeout: Timeout for long polling
:param str name: Bot name
:param callable json_serialize: JSON serializer function. (json.dumps by default)
:param callable json_deserialize: JSON deserializer function. (json.loads by default)
:param bool default_in_groups: Enables default callback in groups
:param str proxy: Proxy URL to use for HTTP requests
:param connector: Custom aiohttp connector
"""
_running = False
_offset = 0
def __init__(
self,
api_token,
api_timeout=API_TIMEOUT,
name=None,
json_serialize=json.dumps,
json_deserialize=json.loads,
default_in_groups=False,
connector=None,
):
self.api_token = api_token
self.api_timeout = api_timeout
self.name = name
self.json_serialize = json_serialize
self.json_deserialize = json_deserialize
self.default_in_groups = default_in_groups
self._session = None
self._cleanups = []
self._webhook_uuid = None
self._connector = connector
def no_handle(mt):
return lambda chat, msg: logger.debug("no handle for %s", mt)
# Init default handlers and callbacks
self._handlers = {mt: no_handle(mt) for mt in MESSAGE_TYPES}
self._commands = []
self._callbacks = []
self._inlines = []
self._chosen_inline_result_callbacks = []
self._checkouts = []
self._default = lambda chat, message: None
self._default_callback = lambda chat, cq: None
self._default_inline = lambda iq: None
self._default_chosen_inline_result_callback = lambda res: None
async def loop(self):
"""
Return bot's main loop as coroutine. Use with asyncio.
:Example:
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(bot.loop())
or
>>> loop = asyncio.get_event_loop()
>>> loop.create_task(bot.loop())
"""
self._running = True
while self._running:
updates = await self.api_call(
"getUpdates", offset=self._offset + 1, timeout=self.api_timeout
)
self._process_updates(updates)
def run(self, debug=False, reload=None):
"""
Convenience method for running bots in getUpdates mode
:param bool debug: Enable debug logging and automatic reloading
:param bool reload: Automatically reload bot on code change
:Example:
>>> if __name__ == '__main__':
>>> bot.run()
"""
loop = asyncio.get_event_loop()
logging.basicConfig(level=logging.DEBUG if debug else logging.INFO)
if reload is None:
reload = debug
bot_loop = asyncio.ensure_future(self.loop())
try:
if reload:
loop.run_until_complete(run_with_reloader(loop, bot_loop, self.stop))
else:
loop.run_until_complete(bot_loop)
# User cancels
except KeyboardInterrupt:
logger.debug("User cancelled")
bot_loop.cancel()
self.stop()
# Stop loop
finally:
for cleanup_action in self._cleanups:
cleanup_action()
loop.run_until_complete(self.session.close())
logger.debug("Closing loop")
loop.stop()
loop.close()
def run_webhook(self, webhook_url, **options):
"""
Convenience method for running bots in webhook mode
:Example:
>>> if __name__ == '__main__':
>>> bot.run_webhook(webhook_url="https://yourserver.com/webhooktoken")
Additional documentation on https://core.telegram.org/bots/api#setwebhook
"""
loop = asyncio.get_event_loop()
loop.run_until_complete(self.set_webhook(webhook_url, **options))
if webhook_url:
url = urlparse(webhook_url)
app = self.create_webhook_app(url.path, loop)
host = os.environ.get("HOST", "0.0.0.0")
port = int(os.environ.get("PORT", 0)) or url.port
app.on_cleanup.append(lambda _: self.session.close())
for cleanup_action in self._cleanups:
app.on_cleanup.append(cleanup_action)
web.run_app(app, host=host, port=port, loop=loop)
else:
loop.run_until_complete(self.session.close())
def stop_webhook(self):
"""
Use to switch from Webhook to getUpdates mode
"""
self.run_webhook(webhook_url="")
def add_command(self, regexp, fn):
"""
Manually register regexp based command
"""
self._commands.append((regexp, fn))
def command(self, regexp):
"""
Register a new command
:param str regexp: Regular expression matching the command to register
:Example:
>>> @bot.command(r"/echo (.+)")
>>> def echo(chat, match):
>>> return chat.reply(match.group(1))
"""
def decorator(fn):
self.add_command(regexp, fn)
return fn
return decorator
def default(self, callback):
"""
Set callback for default command that is called on unrecognized
commands for 1-to-1 chats
If default_in_groups option is True, callback is called in groups too
:Example:
>>> @bot.default
>>> def echo(chat, message):
>>> return chat.reply(message["text"])
"""
self._default = callback
return callback
def add_inline(self, regexp, fn):
"""
Manually register regexp based callback
"""
self._inlines.append((regexp, fn))
def inline(self, callback):
"""
Set callback for inline queries
:Example:
>>> @bot.inline
>>> def echo(iq):
>>> return iq.answer([
>>> {"type": "text", "title": "test", "id": "0"}
>>> ])
>>> @bot.inline(r"myinline-(.+)")
>>> def echo(chat, iq, match):
>>> return iq.answer([
>>> {"type": "text", "title": "test", "id": "0"}
>>> ])
"""
if callable(callback):
self._default_inline = callback
return callback
elif isinstance(callback, str):
def decorator(fn):
self.add_inline(callback, fn)
return fn
return decorator
else:
raise TypeError("str expected {} given".format(type(callback)))
def add_chosen_inline_result_callback(self, regexp, fn):
"""
Manually register regexp based callback for the ``chosen_inline_result`` updates
"""
self._chosen_inline_result_callbacks.append((regexp, fn))
def chosen_inline_result_callback(self, callback):
"""
Set callback for ``chosen_inline_result`` updates
:Example:
>>> @bot.chosen_inline_result_callback
>>> def inc_metric(iq):
>>> metrics[iq.result_id].inc()
>>> @bot.chosen_inline_result_callback(r"myinline-(.+)")
>>> def inc_metric(chat, iq, match):
>>> metrics[iq.result_id].inc()
"""
if callable(callback):
self._default_chosen_inline_result_callback = callback
return callback
elif isinstance(callback, str):
def decorator(fn):
self.add_chosen_inline_result_callback(callback, fn)
return fn
return decorator
else:
raise TypeError("str expected {} given".format(type(callback)))
def add_callback(self, regexp, fn):
"""
Manually register regexp based callback
"""
self._callbacks.append((regexp, fn))
def callback(self, callback):
"""
Set callback for callback queries
:Example:
>>> @bot.callback
>>> def echo(chat, cq):
>>> return cq.answer()
>>> @bot.callback(r"buttonclick-(.+)")
>>> def echo(chat, cq, match):
>>> return chat.reply(match.group(1))
"""
if callable(callback):
self._default_callback = callback
return callback
elif isinstance(callback, str):
def decorator(fn):
self.add_callback(callback, fn)
return fn
return decorator
else:
raise TypeError("str expected {} given".format(type(callback)))
def add_checkout(self, regexp, fn):
"""
Manually register regexp based checkout handler
"""
self._checkouts.append((regexp, fn))
def checkout(self, callback):
if callable(callback):
self._default_checkout = callback
elif isinstance(callback, str):
def decorator(fn):
self.add_checkout(callback, fn)
return fn
return decorator
else:
raise TypeError("str expected {} given".format(type(callback)))
def handle(self, msg_type):
"""
Set handler for specific message type
:Example:
>>> @bot.handle("audio")
>>> def handle(chat, audio):
>>> pass
"""
def wrap(callback):
self._handlers[msg_type] = callback
return callback
return wrap
def channel(self, channel_name):
"""
Construct a Chat object used to post to channel
:param str channel_name: Channel name
"""
return Chat(self, channel_name, "channel")
def private(self, user_id):
"""
Construct a Chat object used to post direct messages
:param str user_id: User id
"""
return Chat(self, user_id, "private")
def group(self, group_id):
"""
Construct a Chat object used to post group messages
:param str group_id: Group chat id
"""
return Chat(self, group_id, "group")
def api_call(self, method, **params):
"""
Call Telegram API.
See https://core.telegram.org/bots/api for reference.
:param str method: Telegram API method
:param params: Arguments for the method call
"""
coro = self._api_call(method, **params)
# Explicitly ensure that API call is executed
return asyncio.ensure_future(coro)
async def _api_call(self, method, **params):
url = "{0}/bot{1}/{2}".format(API_URL, self.api_token, method)
logger.debug("api_call %s, %s", method, params)
response = await self.session.post(url, data=params)
if response.status == 200:
return await response.json(loads=self.json_deserialize)
elif response.status in RETRY_CODES:
logger.info(
"Server returned %d, retrying in %d sec.",
response.status,
RETRY_TIMEOUT,
)
await response.release()
await asyncio.sleep(RETRY_TIMEOUT)
return await self.api_call(method, **params)
else:
if response.headers["content-type"] == "application/json":
json_resp = await response.json(loads=self.json_deserialize)
err_msg = json_resp["description"]
else:
err_msg = await response.read()
logger.error(err_msg)
raise BotApiError(err_msg, response=response)
async def get_me(self):
"""
Returns basic information about the bot
(see https://core.telegram.org/bots/api#getme)
"""
json_result = await self.api_call("getMe")
return json_result["result"]
async def leave_chat(self, chat_id):
"""
Use this method for your bot to leave a group, supergroup or channel.
Returns True on success.
:param int chat_id: Unique identifier for the target chat \
or username of the target supergroup or channel \
(in the format @channelusername)
"""
json_result = await self.api_call("leaveChat", chat_id=chat_id)
return json_result["result"]
def send_message(self, chat_id, text, **options):
"""
Send a text message to chat
:param int chat_id: ID of the chat to send the message to
:param str text: Text to send
:param options: Additional sendMessage options
(see https://core.telegram.org/bots/api#sendmessage)
"""
return self.api_call("sendMessage", chat_id=chat_id, text=text, **options)
def edit_message_text(self, chat_id, message_id, text, **options):
"""
Edit a text message in a chat
:param int chat_id: ID of the chat the message to edit is in
:param int message_id: ID of the message to edit
:param str text: Text to edit the message to
:param options: Additional API options
"""
return self.api_call(
"editMessageText",
chat_id=chat_id,
message_id=message_id,
text=text,
**options
)
def edit_message_reply_markup(self, chat_id, message_id, reply_markup, **options):
"""
Edit a reply markup of message in a chat
:param int chat_id: ID of the chat the message to edit is in
:param int message_id: ID of the message to edit
:param str reply_markup: New inline keyboard markup for the message
:param options: Additional API options
"""
return self.api_call(
"editMessageReplyMarkup",
chat_id=chat_id,
message_id=message_id,
reply_markup=reply_markup,
**options
)
async def get_file(self, file_id):
"""
Get basic information about a file and prepare it for downloading.
:param int file_id: File identifier to get information about
:return: File object (see https://core.telegram.org/bots/api#file)
"""
json = await self.api_call("getFile", file_id=file_id)
return json["result"]
def download_file(self, file_path, range=None):
"""
Download a file from Telegram servers
"""
headers = {"range": range} if range else None
url = "{0}/file/bot{1}/{2}".format(API_URL, self.api_token, file_path)
return self.session.get(url, headers=headers)
def get_user_profile_photos(self, user_id, **options):
"""
Get a list of profile pictures for a user
:param int user_id: Unique identifier of the target user
:param options: Additional getUserProfilePhotos options (see
https://core.telegram.org/bots/api#getuserprofilephotos)
"""
return self.api_call("getUserProfilePhotos", user_id=str(user_id), **options)
def track(self, message, name="Message"):
# TODO allow configuring custom tracking
pass
def stop(self):
self._running = False
async def webhook_handle(self, request):
"""
aiohttp.web handle for processing web hooks
:Example:
>>> from aiohttp import web
>>> app = web.Application()
>>> app.router.add_route('/webhook')
"""
if request.headers.get("X-Telegram-Bot-Api-Secret-Token") != self._webhook_uuid:
logger.warning("Probably, a malicious request! " + request)
return web.Response(status=403)
update = await request.json(loads=self.json_deserialize)
self._process_update(update)
return web.Response()
def create_webhook_app(self, path, loop=None):
"""
Shorthand for creating aiohttp.web.Application with registered webhook hanlde
"""
app = web.Application(loop=loop)
app.router.add_route("POST", path, self.webhook_handle)
return app
def set_webhook(self, webhook_url, **options):
"""
Register you webhook url for Telegram service.
A newly generated UUID will be used as a secret_token parameter
if it's not specified explicitly
"""
if "secret_token" not in options:
options["secret_token"] = str(uuid.uuid4())
self._webhook_uuid = options["secret_token"]
return self.api_call("setWebhook", url=webhook_url, **options)
def delete_webhook(self):
"""
Tell Telegram to switch back to getUpdates mode
"""
return self.api_call("deleteWebhook")
def on_cleanup(self, action):
"""
You can set an action that will be executed before closing the loop
:param action: must be a simple callable without any arguments
:Example:
>>> bot.on_cleanup(lambda: [t.cancel() for t in tasks])
"""
self._cleanups.append(action)
@property
def session(self):
if not self._session or self._session.closed:
self._session = aiohttp.ClientSession(
json_serialize=self.json_serialize, connector=self._connector
)
return self._session
def _process_message(self, message):
chat = Chat.from_message(self, message)
for mt, func in self._handlers.items():
if mt in message:
self.track(message, mt)
return func(chat, message[mt])
if "text" not in message:
return
for patterns, handler in self._commands:
m = re.search(patterns, message["text"], re.I)
if m:
self.track(message, handler.__name__)
return handler(chat, m)
# No match, run default if it's a 1to1 chat
# However, if default_in_groups option is active, run default in any chat (not only 1to1)
if not chat.is_group() or self.default_in_groups:
self.track(message, "default")
return self._default(chat, message)
def _process_inline_query(self, query):
iq = InlineQuery(self, query)
for patterns, handler in self._inlines:
match = re.search(patterns, query["query"], re.I)
if match:
return handler(iq, match)
return self._default_inline(iq)
def _process_chosen_inline_result(self, result):
cir = ChosenInlineResult(self, result)
for patterns, handler in self._chosen_inline_result_callbacks:
match = re.search(patterns, result["query"], re.I)
if match:
return handler(cir, match)
return self._default_chosen_inline_result_callback(cir)
def _process_callback_query(self, query):
chat = Chat.from_message(self, query["message"]) if "message" in query else None
cq = CallbackQuery(self, query)
for patterns, handler in self._callbacks:
match = re.search(patterns, cq.data, re.I)
if match:
return handler(chat, cq, match)
if chat and not chat.is_group() or self.default_in_groups:
return self._default_callback(chat, cq)
def _process_pre_checkout_query(self, query):
pcq = PreCheckoutQuery(self, query)
for patterns, handler in self._checkouts:
match = re.search(patterns, pcq.invoice_payload, re.I)
if match:
return handler(pcq, match)
return self._default_checkout(pcq)
def _process_updates(self, updates):
if not updates["ok"]:
logger.error("getUpdates error: %s", updates.get("description"))
return
for update in updates["result"]:
self._process_update(update)
def _process_update(self, update):
logger.debug("update %s", update)
# Update offset
self._offset = max(self._offset, update["update_id"])
coro = None
# Determine update type starting with message updates
for ut in MESSAGE_UPDATES:
if ut in update:
coro = self._process_message(update[ut])
break
else:
if "inline_query" in update:
coro = self._process_inline_query(update["inline_query"])
elif "callback_query" in update:
coro = self._process_callback_query(update["callback_query"])
elif "pre_checkout_query" in update:
coro = self._process_pre_checkout_query(update["pre_checkout_query"])
elif "chosen_inline_result" in update:
coro = self._process_chosen_inline_result(
update["chosen_inline_result"]
)
else:
logger.error("don't know how to handle update: %s", update)
if coro:
asyncio.ensure_future(coro)
class TgBot(Bot):
def __init__(self, *args, **kwargs):
logger.warning("TgBot is depricated, use Bot instead")
super().__init__(*args, **kwargs)
class InlineQuery:
"""
Incoming inline query
See https://core.telegram.org/bots/api#inline-mode for details
"""
def __init__(self, bot, src):
self.bot = bot
self.sender = Sender(src["from"])
self.query_id = src["id"]
self.query = src["query"]
def answer(self, results, **options):
return self.bot.api_call(
"answerInlineQuery",
inline_query_id=self.query_id,
results=self.bot.json_serialize(results),
**options
)
class TgInlineQuery(InlineQuery):
def __init__(self, *args, **kwargs):
logger.warning("TgInlineQuery is depricated, use InlineQuery instead")
super().__init__(*args, **kwargs)
class ChosenInlineResult:
def __init__(self, bot, src):
self.bot = bot
self.sender = Sender(src["from"])
self.result_id = src["result_id"]
self.location = src.get("location")
self.inline_message_id = src.get("inline_messages_id")
self.query = src["query"]
class CallbackQuery:
def __init__(self, bot, src):
self.bot = bot
self.query_id = src["id"]
self.data = src["data"]
self.src = src
def answer(self, **options):
return self.bot.api_call(
"answerCallbackQuery", callback_query_id=self.query_id, **options
)
class PreCheckoutQuery:
def __init__(self, bot, src):
self.bot = bot
self.sender = Sender(src["from"])
self.query_id = src["id"]
self.currency = src["currency"]
self.total_amount = src["total_amount"]
self.invoice_payload = src["invoice_payload"]
def answer(self, error_message=None, **options):
return self.bot.api_call(
"answerPreCheckoutQuery",
pre_checkout_query_id=self.query_id,
ok=error_message is None,
error_message=error_message,
**options
)
class BotApiError(RuntimeError):
def __init__(self, *args, response):
super().__init__(*args)
self.response = response