Skip to content

Commit 94fd685

Browse files
committed
more robust echobot, let roboed go
1 parent 897f961 commit 94fd685

File tree

2 files changed

+10
-45
lines changed

2 files changed

+10
-45
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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-
77
import logging
88
import telegram
99
from telegram.error import NetworkError, Unauthorized
1010
from time import sleep
1111

1212

13+
update_id = None
14+
1315
def 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

5255
if __name__ == '__main__':
5356
main()

examples/legacy/roboed.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)