Skip to content

Commit 9338f93

Browse files
authored
Merge pull request python-telegram-bot#325 from python-telegram-bot/examples
more robust echobot, let roboed go
2 parents 897f961 + e10fa66 commit 9338f93

File tree

4 files changed

+11
-53
lines changed

4 files changed

+11
-53
lines changed

README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,7 @@ code and building on top of it.
129129

130130
- `timerbot <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py>`_ uses the ``JobQueue`` to send timed messages.
131131

132-
Examples using only the pure API:
133-
134-
- `echobot <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/legacy/echobot.py>`_ replies back messages.
135-
136-
- `roboed <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/legacy/roboed.py>`_ talks to `Robô Ed <http://www.ed.conpet.gov.br/br/converse.php>`_.
132+
- `echobot <https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/legacy/echobot.py>`_ uses only the pure API to echo messages.
137133

138134
Look at the examples on the `wiki <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Examples>`_ to see other bots the community has built.
139135

examples/legacy/echobot.py renamed to examples/echobot.py

Lines changed: 10 additions & 10 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,28 +28,25 @@ 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
43-
message = update.message.text
4446

45-
if message:
47+
if update.message: # your bot can receive updates without messages
4648
# Reply to the message
47-
bot.sendMessage(chat_id=chat_id, text=message)
48-
49-
return update_id
49+
bot.sendMessage(chat_id=chat_id, text=update.message.text)
5050

5151

5252
if __name__ == '__main__':
File renamed without changes.

examples/legacy/roboed.py

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

0 commit comments

Comments
 (0)