2015-07-12 14:54:03 +02:00
|
|
|
#!/usr/bin/env python
|
2015-11-12 17:26:25 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
2016-06-12 15:30:56 +02:00
|
|
|
# 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.
|
2015-12-25 15:19:17 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2015-07-30 08:04:59 +02:00
|
|
|
import logging
|
2015-07-12 14:54:03 +02:00
|
|
|
import telegram
|
2016-02-09 15:28:54 +01:00
|
|
|
from telegram.error import NetworkError, Unauthorized
|
2015-11-12 17:26:25 +01:00
|
|
|
from time import sleep
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
2016-06-12 15:30:56 +02:00
|
|
|
update_id = None
|
|
|
|
|
2015-07-30 08:04:59 +02:00
|
|
|
def main():
|
2016-06-12 15:30:56 +02:00
|
|
|
global update_id
|
2015-11-20 14:41:12 +01:00
|
|
|
# Telegram Bot Authorization Token
|
|
|
|
bot = telegram.Bot('TOKEN')
|
|
|
|
|
|
|
|
# get the first pending update_id, this is so we can skip over it in case
|
|
|
|
# we get an "Unauthorized" exception.
|
|
|
|
try:
|
2017-05-19 21:49:01 +02:00
|
|
|
update_id = bot.get_updates()[0].update_id
|
2015-11-20 14:41:12 +01:00
|
|
|
except IndexError:
|
|
|
|
update_id = None
|
2015-07-30 08:04:59 +02:00
|
|
|
|
2016-05-22 18:26:57 +02:00
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
2015-07-30 08:04:59 +02:00
|
|
|
|
|
|
|
while True:
|
2015-11-12 17:26:25 +01:00
|
|
|
try:
|
2016-06-12 15:30:56 +02:00
|
|
|
echo(bot)
|
2016-02-09 15:28:54 +01:00
|
|
|
except NetworkError:
|
2015-11-12 17:26:25 +01:00
|
|
|
sleep(1)
|
2016-02-09 15:28:54 +01:00
|
|
|
except Unauthorized:
|
|
|
|
# The user has removed or blocked the bot.
|
|
|
|
update_id += 1
|
2015-11-12 17:26:25 +01:00
|
|
|
|
|
|
|
|
2016-06-12 15:30:56 +02:00
|
|
|
def echo(bot):
|
|
|
|
global update_id
|
2015-11-12 17:26:25 +01:00
|
|
|
# Request updates after the last update_id
|
2017-05-19 21:49:01 +02:00
|
|
|
for update in bot.get_updates(offset=update_id, timeout=10):
|
2015-11-12 17:26:25 +01:00
|
|
|
update_id = update.update_id + 1
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2016-06-12 17:06:03 +02:00
|
|
|
if update.message: # your bot can receive updates without messages
|
2015-11-12 17:26:25 +01:00
|
|
|
# Reply to the message
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text(update.message.text)
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-07-30 08:04:59 +02:00
|
|
|
main()
|