|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Simple Bot to reply to Telegram messages |
| 5 | +# This program is dedicated to the public domain under the CC0 license. |
| 6 | + |
| 7 | +""" |
| 8 | +This Bot uses the Updater class to handle the bot. |
| 9 | +
|
| 10 | +First, a few handler functions are defined. Then, those functions are passed to |
| 11 | +the Dispatcher and registered at their respective places. |
| 12 | +Then, the bot is started and runs until we press Ctrl-C on the command line. |
| 13 | +
|
| 14 | +Usage: |
| 15 | +Basic inline bot example. Applies different text transformations. |
| 16 | +Press Ctrl-C on the command line or send a signal to the process to stop the |
| 17 | +bot. |
| 18 | +""" |
| 19 | +from random import getrandbits |
| 20 | + |
| 21 | +import re |
| 22 | + |
| 23 | +from telegram import Updater, Update, InlineQueryResultArticle, ParseMode |
| 24 | +import logging |
| 25 | + |
| 26 | +# Enable logging |
| 27 | +logging.basicConfig( |
| 28 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 29 | + level=logging.INFO) |
| 30 | + |
| 31 | +logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +# Define a few command handlers. These usually take the two arguments bot and |
| 35 | +# update. Error handlers also receive the raised TelegramError object in error. |
| 36 | +def start(bot, update): |
| 37 | + bot.sendMessage(update.message.chat_id, text='Hi!') |
| 38 | + |
| 39 | + |
| 40 | +def help(bot, update): |
| 41 | + bot.sendMessage(update.message.chat_id, text='Help!') |
| 42 | + |
| 43 | + |
| 44 | +def escape_markdown(text): |
| 45 | + """Helper function to escape telegram markup symbols""" |
| 46 | + escape_chars = '\*_`\[' |
| 47 | + return re.sub(r'([%s])' % escape_chars, r'\\\1', text) |
| 48 | + |
| 49 | + |
| 50 | +def inlinequery(bot, update): |
| 51 | + if update.inline_query is not None and update.inline_query.query: |
| 52 | + query = update.inline_query.query |
| 53 | + results = list() |
| 54 | + |
| 55 | + results.append(InlineQueryResultArticle( |
| 56 | + id=hex(getrandbits(64))[2:], |
| 57 | + title="Caps", |
| 58 | + message_text=query.upper())) |
| 59 | + |
| 60 | + results.append(InlineQueryResultArticle( |
| 61 | + id=hex(getrandbits(64))[2:], |
| 62 | + title="Bold", |
| 63 | + message_text="*%s*" % escape_markdown(query), |
| 64 | + parse_mode=ParseMode.MARKDOWN)) |
| 65 | + |
| 66 | + results.append(InlineQueryResultArticle( |
| 67 | + id=hex(getrandbits(64))[2:], |
| 68 | + title="Italic", |
| 69 | + message_text="_%s_" % escape_markdown(query), |
| 70 | + parse_mode=ParseMode.MARKDOWN)) |
| 71 | + |
| 72 | + bot.answerInlineQuery(update.inline_query.id, results=results) |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +def error(bot, update, error): |
| 77 | + logger.warn('Update "%s" caused error "%s"' % (update, error)) |
| 78 | + |
| 79 | + |
| 80 | +def main(): |
| 81 | + # Create the Updater and pass it your bot's token. |
| 82 | + updater = Updater("TOKEN") |
| 83 | + |
| 84 | + # Get the dispatcher to register handlers |
| 85 | + dp = updater.dispatcher |
| 86 | + |
| 87 | + # on different commands - answer in Telegram |
| 88 | + dp.addTelegramCommandHandler("start", start) |
| 89 | + dp.addTelegramCommandHandler("help", help) |
| 90 | + |
| 91 | + # on noncommand i.e message - echo the message on Telegram |
| 92 | + dp.addTelegramInlineHandler(inlinequery) |
| 93 | + |
| 94 | + # log all errors |
| 95 | + dp.addErrorHandler(error) |
| 96 | + |
| 97 | + # Start the Bot |
| 98 | + updater.start_polling() |
| 99 | + |
| 100 | + # Block until the user presses Ctrl-C or the process receives SIGINT, |
| 101 | + # SIGTERM or SIGABRT. This should be used most of the time, since |
| 102 | + # start_polling() is non-blocking and will stop the bot gracefully. |
| 103 | + updater.idle() |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + main() |
0 commit comments