2015-11-15 20:02:09 +01:00
|
|
|
#!/usr/bin/env python
|
2015-12-21 21:18:53 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-13 13:38:07 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2019-02-13 13:38:07 +01:00
|
|
|
"""
|
|
|
|
Simple Bot to reply to Telegram messages.
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
2015-11-22 14:47:38 +01:00
|
|
|
the Dispatcher and registered at their respective places.
|
2015-12-22 13:23:59 +01:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2015-11-15 20:13:03 +01:00
|
|
|
Usage:
|
|
|
|
Basic Echobot example, repeats messages.
|
2015-12-22 13:23:59 +01:00
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
2015-11-15 20:02:09 +01:00
|
|
|
"""
|
|
|
|
|
2018-05-21 15:00:47 +02:00
|
|
|
import logging
|
2018-05-21 15:00:47 +02:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
|
|
|
|
2015-11-15 20:02:09 +01:00
|
|
|
# Enable logging
|
2016-05-22 18:26:57 +02:00
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2015-11-22 19:20:16 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2015-11-24 21:06:55 +01:00
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
|
|
|
# context. Error handlers also receive the raised TelegramError object in error.
|
2018-09-21 08:57:01 +02:00
|
|
|
def start(update, context):
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send a message when the command /start is issued."""
|
2016-09-20 06:36:55 +02:00
|
|
|
update.message.reply_text('Hi!')
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def help(update, context):
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send a message when the command /help is issued."""
|
2016-09-20 06:36:55 +02:00
|
|
|
update.message.reply_text('Help!')
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def echo(update, context):
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Echo the user message."""
|
2016-09-20 06:36:55 +02:00
|
|
|
update.message.reply_text(update.message.text)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def error(update, context):
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Log Errors caused by Updates."""
|
2018-09-21 08:57:01 +02:00
|
|
|
logger.warning('Update "%s" caused error "%s"', update, context.error)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Start the bot."""
|
2018-09-21 08:57:01 +02:00
|
|
|
# Create the Updater and pass it your bot's token.
|
|
|
|
# Make sure to set use_context=True to use the new context based callbacks
|
|
|
|
# Post version 12 this will no longer be necessary
|
|
|
|
updater = Updater("TOKEN", use_context=True)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
# Get the dispatcher to register handlers
|
2015-11-22 19:15:37 +01:00
|
|
|
dp = updater.dispatcher
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2016-04-28 17:29:34 +02:00
|
|
|
dp.add_handler(CommandHandler("start", start))
|
|
|
|
dp.add_handler(CommandHandler("help", help))
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
# on noncommand i.e message - echo the message on Telegram
|
2016-10-25 19:51:56 +02:00
|
|
|
dp.add_handler(MessageHandler(Filters.text, echo))
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2015-12-22 13:23:59 +01:00
|
|
|
# log all errors
|
2016-04-28 17:29:34 +02:00
|
|
|
dp.add_error_handler(error)
|
2015-11-15 20:02:09 +01:00
|
|
|
|
|
|
|
# Start the Bot
|
2015-12-22 13:23:59 +01:00
|
|
|
updater.start_polling()
|
2015-11-22 19:15:37 +01:00
|
|
|
|
2017-02-18 16:03:50 +01:00
|
|
|
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
2015-12-22 13:23:59 +01:00
|
|
|
# SIGTERM or SIGABRT. This should be used most of the time, since
|
|
|
|
# start_polling() is non-blocking and will stop the bot gracefully.
|
2015-11-22 19:15:37 +01:00
|
|
|
updater.idle()
|
2015-11-15 20:02:09 +01:00
|
|
|
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2015-11-15 20:02:09 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|