2015-07-12 14:54:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
'''Simple Bot to reply Telegram messages'''
|
|
|
|
|
|
|
|
import telegram
|
|
|
|
import time
|
|
|
|
|
|
|
|
# Telegram Bot Authorization Token
|
2015-07-14 09:31:20 +02:00
|
|
|
bot = telegram.Bot('TOKEN')
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
# This will be our global variable to keep the latest update_id when requesting
|
2015-07-15 12:47:13 +02:00
|
|
|
# for updates. It starts with the latest update_id if available.
|
|
|
|
try:
|
|
|
|
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
|
|
|
|
except IndexError:
|
|
|
|
LAST_UPDATE_ID = None
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def echo():
|
|
|
|
global LAST_UPDATE_ID
|
|
|
|
|
|
|
|
# Request updates from last updated_id
|
|
|
|
for update in bot.getUpdates(offset=LAST_UPDATE_ID):
|
|
|
|
if LAST_UPDATE_ID < update.update_id:
|
|
|
|
# chat_id is required to reply any message
|
|
|
|
chat_id = update.message.chat_id
|
2015-07-20 13:59:41 +02:00
|
|
|
message = update.message.text.encode('utf-8')
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
if (message):
|
|
|
|
# Reply the message
|
|
|
|
bot.sendMessage(chat_id=chat_id,
|
|
|
|
text=message)
|
|
|
|
|
|
|
|
# Updates global offset to get the new updates
|
|
|
|
LAST_UPDATE_ID = update.update_id
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
while True:
|
|
|
|
echo()
|
2015-07-14 09:31:20 +02:00
|
|
|
time.sleep(3)
|