python-telegram-bot/examples/legacy/roboed.py

39 lines
1.1 KiB
Python
Raw Normal View History

2015-07-08 05:59:43 +02:00
#!/usr/bin/env python
# encoding: utf-8
2015-08-11 21:58:17 +02:00
#
# Robô Ed Telegram Bot
# This program is dedicated to the public domain under the CC0 license.
2015-07-08 05:59:43 +02:00
import logging
2015-07-08 05:59:43 +02:00
import telegram
2015-07-14 09:31:20 +02:00
import urllib
2015-07-08 05:59:43 +02:00
def main():
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
2015-07-14 09:31:20 +02:00
bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token
2015-07-08 05:59:43 +02:00
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update
while True:
2015-08-24 11:46:33 +02:00
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
2015-07-08 05:59:43 +02:00
text = update.message.text
chat_id = update.message.chat.id
update_id = update.update_id
2015-08-20 19:58:57 +02:00
if text:
roboed = ed(text) # Ask something to Robô Ed
bot.sendMessage(chat_id=chat_id, text=roboed)
LAST_UPDATE_ID = update_id + 1
2015-07-08 05:59:43 +02:00
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
2015-07-14 09:31:20 +02:00
data = urllib.urlopen(url).read()
2015-07-08 05:59:43 +02:00
return data.strip()
if __name__ == '__main__':
main()