2015-07-12 14:54:03 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# Simple Bot to reply Telegram messages
|
|
|
|
# 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-07-30 08:04:59 +02:00
|
|
|
LAST_UPDATE_ID = None
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
2015-07-30 08:04:59 +02:00
|
|
|
def main():
|
|
|
|
global LAST_UPDATE_ID
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
# Telegram Bot Authorization Token
|
|
|
|
bot = telegram.Bot('TOKEN')
|
|
|
|
|
|
|
|
# This will be our global variable to keep the latest update_id when requesting
|
|
|
|
# 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
|
|
|
|
|
|
|
|
while True:
|
|
|
|
echo(bot)
|
|
|
|
|
|
|
|
|
|
|
|
def echo(bot):
|
2015-07-12 14:54:03 +02:00
|
|
|
global LAST_UPDATE_ID
|
|
|
|
|
2015-08-20 19:58:57 +02:00
|
|
|
# Request updates after the last updated_id
|
2015-08-24 11:46:33 +02:00
|
|
|
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
|
2015-08-20 19:58:57 +02:00
|
|
|
# chat_id is required to reply any message
|
|
|
|
chat_id = update.message.chat_id
|
2015-10-23 21:48:26 +02:00
|
|
|
reply_text = update.message.text
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-11-10 20:36:26 +01:00
|
|
|
if reply_text:
|
2015-08-20 19:58:57 +02:00
|
|
|
# Reply the message
|
|
|
|
bot.sendMessage(chat_id=chat_id,
|
2015-10-23 21:48:26 +02:00
|
|
|
text=reply_text)
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-11-10 20:36:26 +01:00
|
|
|
# Updates global offset to get the new updates
|
|
|
|
LAST_UPDATE_ID = update.update_id + 1
|
2015-07-12 14:54:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-07-30 08:04:59 +02:00
|
|
|
main()
|