File tree Expand file tree Collapse file tree 2 files changed +10
-45
lines changed Expand file tree Collapse file tree 2 files changed +10
-45
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
#
4
- # Simple Bot to reply to Telegram messages
4
+ # Simple Bot to reply to Telegram messages. This is built on the API wrapper, see
5
+ # echobot2.py to see the same example built on the telegram.ext bot framework.
5
6
# This program is dedicated to the public domain under the CC0 license.
6
-
7
7
import logging
8
8
import telegram
9
9
from telegram .error import NetworkError , Unauthorized
10
10
from time import sleep
11
11
12
12
13
+ update_id = None
14
+
13
15
def main ():
16
+ global update_id
14
17
# Telegram Bot Authorization Token
15
18
bot = telegram .Bot ('TOKEN' )
16
19
@@ -25,29 +28,29 @@ def main():
25
28
26
29
while True :
27
30
try :
28
- update_id = echo (bot , update_id )
31
+ echo (bot )
29
32
except NetworkError :
30
33
sleep (1 )
31
34
except Unauthorized :
32
35
# The user has removed or blocked the bot.
33
36
update_id += 1
34
37
35
38
36
- def echo (bot , update_id ):
37
-
39
+ def echo (bot ):
40
+ global update_id
38
41
# Request updates after the last update_id
39
42
for update in bot .getUpdates (offset = update_id , timeout = 10 ):
40
43
# chat_id is required to reply to any message
41
44
chat_id = update .message .chat_id
42
45
update_id = update .update_id + 1
46
+ if not update .message : # we ignore updates without messages
47
+ continue
43
48
message = update .message .text
44
49
45
50
if message :
46
51
# Reply to the message
47
52
bot .sendMessage (chat_id = chat_id , text = message )
48
53
49
- return update_id
50
-
51
54
52
55
if __name__ == '__main__' :
53
56
main ()
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments