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
|
|
|
#
|
2015-11-12 17:26:25 +01:00
|
|
|
# Simple Bot to reply to Telegram messages
|
2015-08-11 21:58:17 +02:00
|
|
|
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2015-08-10 18:57:31 +02:00
|
|
|
|
2015-07-12 14:54:03 +02:00
|
|
|
|
2015-07-30 08:04:59 +02:00
|
|
|
import logging
|
2015-07-12 14:54:03 +02:00
|
|
|
import telegram
|
2015-11-12 17:26:25 +01:00
|
|
|
from time import sleep
|
2015-07-12 14:54:03 +02:00
|
|
|
|
2015-11-12 17:26:25 +01:00
|
|
|
try:
|
|
|
|
from urllib.error import URLError
|
|
|
|
except ImportError:
|
|
|
|
from urllib2 import URLError # python 2
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
2015-07-30 08:04:59 +02:00
|
|
|
def main():
|
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:
|
|
|
|
update_id = bot.getUpdates()[0].update_id
|
|
|
|
except IndexError:
|
|
|
|
update_id = None
|
2015-07-30 08:04:59 +02:00
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
while True:
|
2015-11-12 17:26:25 +01:00
|
|
|
try:
|
|
|
|
update_id = echo(bot, update_id)
|
|
|
|
except telegram.TelegramError as e:
|
|
|
|
# These are network problems with Telegram.
|
|
|
|
if e.message in ("Bad Gateway", "Timed out"):
|
|
|
|
sleep(1)
|
2015-11-20 14:41:12 +01:00
|
|
|
elif e.message == "Unauthorized":
|
|
|
|
# The user has removed or blocked the bot.
|
|
|
|
update_id += 1
|
2015-11-12 17:26:25 +01:00
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
except URLError as e:
|
|
|
|
# These are network problems on our end.
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
def echo(bot, 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
|
2015-08-20 19:58:57 +02:00
|
|
|
chat_id = update.message.chat_id
|
2015-11-12 17:26:25 +01:00
|
|
|
update_id = update.update_id + 1
|
|
|
|
message = update.message.text
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-11-12 17:26:25 +01:00
|
|
|
if message:
|
|
|
|
# Reply to the message
|
2015-08-20 19:58:57 +02:00
|
|
|
bot.sendMessage(chat_id=chat_id,
|
2015-11-12 17:26:25 +01:00
|
|
|
text=message)
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-11-12 17:26:25 +01:00
|
|
|
return update_id
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-07-30 08:04:59 +02:00
|
|
|
main()
|