|
| 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 | +This Bot uses the Updater class to handle the bot. |
| 8 | +
|
| 9 | +First, a few callback functions are defined. Then, those functions are passed to |
| 10 | +the Dispatcher and registered at their respective places. |
| 11 | +Then, the bot is started and runs until we press Ctrl-C on the command line. |
| 12 | +
|
| 13 | +Usage: |
| 14 | +Example of a bot-user conversation using ConversationHandler. |
| 15 | +Send /start to initiate the conversation. |
| 16 | +Press Ctrl-C on the command line or send a signal to the process to stop the |
| 17 | +bot. |
| 18 | +""" |
| 19 | + |
| 20 | +from telegram import ReplyKeyboardMarkup |
| 21 | +from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler, |
| 22 | + ConversationHandler) |
| 23 | + |
| 24 | +import logging |
| 25 | + |
| 26 | +# Enable logging |
| 27 | +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 28 | + level=logging.INFO) |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | +CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3) |
| 33 | + |
| 34 | +reply_keyboard = [['Age', 'Favourite colour'], |
| 35 | + ['Number of siblings', 'Something else...'], |
| 36 | + ['Done']] |
| 37 | +markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True) |
| 38 | + |
| 39 | + |
| 40 | +def facts_to_str(user_data): |
| 41 | + facts = list() |
| 42 | + |
| 43 | + for key, value in user_data.items(): |
| 44 | + facts.append('%s - %s' % (key, value)) |
| 45 | + |
| 46 | + return "\n".join(facts).join(['\n', '\n']) |
| 47 | + |
| 48 | + |
| 49 | +def start(bot, update): |
| 50 | + update.message.reply_text( |
| 51 | + "Hi! My name is Doctor Botter. I will hold a more complex conversation with you. " |
| 52 | + "Why don't you tell me something about yourself?", |
| 53 | + reply_markup=markup) |
| 54 | + |
| 55 | + return CHOOSING |
| 56 | + |
| 57 | + |
| 58 | +def regular_choice(bot, update, user_data): |
| 59 | + text = update.message.text |
| 60 | + user_data['choice'] = text |
| 61 | + update.message.reply_text('Your %s? Yes, I would love to hear about that!' % text.lower()) |
| 62 | + |
| 63 | + return TYPING_REPLY |
| 64 | + |
| 65 | + |
| 66 | +def custom_choice(bot, update): |
| 67 | + update.message.reply_text('Alright, please send me the category first, ' |
| 68 | + 'for example "Most impressive skill"') |
| 69 | + |
| 70 | + return TYPING_CHOICE |
| 71 | + |
| 72 | + |
| 73 | +def received_information(bot, update, user_data): |
| 74 | + text = update.message.text |
| 75 | + category = user_data['choice'] |
| 76 | + user_data[category] = text |
| 77 | + del user_data['choice'] |
| 78 | + |
| 79 | + update.message.reply_text("Neat! Just so you know, this is what you already told me:" |
| 80 | + "%s" |
| 81 | + "You can tell me more, or change your opinion on something." |
| 82 | + % facts_to_str(user_data), |
| 83 | + reply_markup=markup) |
| 84 | + |
| 85 | + return CHOOSING |
| 86 | + |
| 87 | + |
| 88 | +def done(bot, update, user_data): |
| 89 | + if 'choice' in user_data: |
| 90 | + del user_data['choice'] |
| 91 | + |
| 92 | + update.message.reply_text("I learned these facts about you:" |
| 93 | + "%s" |
| 94 | + "Until next time!" % facts_to_str(user_data)) |
| 95 | + |
| 96 | + user_data.clear() |
| 97 | + return ConversationHandler.END |
| 98 | + |
| 99 | + |
| 100 | +def error(bot, update, error): |
| 101 | + logger.warn('Update "%s" caused error "%s"' % (update, error)) |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + # Create the Updater and pass it your bot's token. |
| 106 | + updater = Updater("TOKEN") |
| 107 | + |
| 108 | + # Get the dispatcher to register handlers |
| 109 | + dp = updater.dispatcher |
| 110 | + |
| 111 | + # Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO |
| 112 | + conv_handler = ConversationHandler( |
| 113 | + entry_points=[CommandHandler('start', start)], |
| 114 | + |
| 115 | + states={ |
| 116 | + CHOOSING: [RegexHandler('^(Age|Favourite colour|Number of siblings)$', |
| 117 | + regular_choice, |
| 118 | + pass_user_data=True), |
| 119 | + RegexHandler('^Something else...$', |
| 120 | + custom_choice), |
| 121 | + ], |
| 122 | + |
| 123 | + TYPING_CHOICE: [MessageHandler([Filters.text], |
| 124 | + regular_choice, |
| 125 | + pass_user_data=True), |
| 126 | + ], |
| 127 | + |
| 128 | + TYPING_REPLY: [MessageHandler([Filters.text], |
| 129 | + received_information, |
| 130 | + pass_user_data=True), |
| 131 | + ], |
| 132 | + }, |
| 133 | + |
| 134 | + fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)] |
| 135 | + ) |
| 136 | + |
| 137 | + dp.add_handler(conv_handler) |
| 138 | + |
| 139 | + # log all errors |
| 140 | + dp.add_error_handler(error) |
| 141 | + |
| 142 | + # Start the Bot |
| 143 | + updater.start_polling() |
| 144 | + |
| 145 | + # Run the bot until the you presses Ctrl-C or the process receives SIGINT, |
| 146 | + # SIGTERM or SIGABRT. This should be used most of the time, since |
| 147 | + # start_polling() is non-blocking and will stop the bot gracefully. |
| 148 | + updater.idle() |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + main() |
0 commit comments