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 11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33#
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.
56# This program is dedicated to the public domain under the CC0 license.
6-
77import logging
88import telegram
99from telegram .error import NetworkError , Unauthorized
1010from time import sleep
1111
1212
13+ update_id = None
14+
1315def main ():
16+ global update_id
1417 # Telegram Bot Authorization Token
1518 bot = telegram .Bot ('TOKEN' )
1619
@@ -25,29 +28,29 @@ def main():
2528
2629 while True :
2730 try :
28- update_id = echo (bot , update_id )
31+ echo (bot )
2932 except NetworkError :
3033 sleep (1 )
3134 except Unauthorized :
3235 # The user has removed or blocked the bot.
3336 update_id += 1
3437
3538
36- def echo (bot , update_id ):
37-
39+ def echo (bot ):
40+ global update_id
3841 # Request updates after the last update_id
3942 for update in bot .getUpdates (offset = update_id , timeout = 10 ):
4043 # chat_id is required to reply to any message
4144 chat_id = update .message .chat_id
4245 update_id = update .update_id + 1
46+ if not update .message : # we ignore updates without messages
47+ continue
4348 message = update .message .text
4449
4550 if message :
4651 # Reply to the message
4752 bot .sendMessage (chat_id = chat_id , text = message )
4853
49- return update_id
50-
5154
5255if __name__ == '__main__' :
5356 main ()
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments