mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
more robust echobot, let roboed go
This commit is contained in:
parent
897f9615f0
commit
94fd6851ab
2 changed files with 10 additions and 45 deletions
|
@ -1,16 +1,19 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Simple Bot to reply to Telegram messages
|
||||
# Simple Bot to reply to Telegram messages. This is built on the API wrapper, see
|
||||
# echobot2.py to see the same example built on the telegram.ext bot framework.
|
||||
# This program is dedicated to the public domain under the CC0 license.
|
||||
|
||||
import logging
|
||||
import telegram
|
||||
from telegram.error import NetworkError, Unauthorized
|
||||
from time import sleep
|
||||
|
||||
|
||||
update_id = None
|
||||
|
||||
def main():
|
||||
global update_id
|
||||
# Telegram Bot Authorization Token
|
||||
bot = telegram.Bot('TOKEN')
|
||||
|
||||
|
@ -25,7 +28,7 @@ def main():
|
|||
|
||||
while True:
|
||||
try:
|
||||
update_id = echo(bot, update_id)
|
||||
echo(bot)
|
||||
except NetworkError:
|
||||
sleep(1)
|
||||
except Unauthorized:
|
||||
|
@ -33,21 +36,21 @@ def main():
|
|||
update_id += 1
|
||||
|
||||
|
||||
def echo(bot, update_id):
|
||||
|
||||
def echo(bot):
|
||||
global update_id
|
||||
# Request updates after the last update_id
|
||||
for update in bot.getUpdates(offset=update_id, timeout=10):
|
||||
# chat_id is required to reply to any message
|
||||
chat_id = update.message.chat_id
|
||||
update_id = update.update_id + 1
|
||||
if not update.message: # we ignore updates without messages
|
||||
continue
|
||||
message = update.message.text
|
||||
|
||||
if message:
|
||||
# Reply to the message
|
||||
bot.sendMessage(chat_id=chat_id, text=message)
|
||||
|
||||
return update_id
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,38 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
#
|
||||
# Robô Ed Telegram Bot
|
||||
# This program is dedicated to the public domain under the CC0 license.
|
||||
|
||||
import logging
|
||||
import telegram
|
||||
import urllib
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token
|
||||
|
||||
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update
|
||||
|
||||
while True:
|
||||
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
|
||||
text = update.message.text
|
||||
chat_id = update.message.chat.id
|
||||
update_id = update.update_id
|
||||
|
||||
if text:
|
||||
roboed = ed(text) # Ask something to Robô Ed
|
||||
bot.sendMessage(chat_id=chat_id, text=roboed)
|
||||
LAST_UPDATE_ID = update_id + 1
|
||||
|
||||
|
||||
def ed(text):
|
||||
url = 'http://www.ed.conpet.gov.br/mod_perl/bot_gateway.cgi?server=0.0.0.0%3A8085&charset_post=utf-8&charset=utf-8&pure=1&js=0&tst=1&msg=' + text
|
||||
data = urllib.urlopen(url).read()
|
||||
|
||||
return data.strip()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue