|
| 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 | +GENDER, PHOTO, LOCATION, BIO = range(4) |
| 33 | + |
| 34 | + |
| 35 | +def start(bot, update): |
| 36 | + reply_keyboard = [['Boy', 'Girl', 'Other']] |
| 37 | + |
| 38 | + bot.sendMessage(update.message.chat_id, |
| 39 | + text='Hi! My name is Professor Bot. I will hold a conversation with you. ' |
| 40 | + 'Send /cancel to stop talking to me.\n\n' |
| 41 | + 'Are you a boy or a girl?', |
| 42 | + reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) |
| 43 | + |
| 44 | + return GENDER |
| 45 | + |
| 46 | + |
| 47 | +def gender(bot, update): |
| 48 | + user = update.message.from_user |
| 49 | + logger.info("Gender of %s: %s" % (user.first_name, update.message.text)) |
| 50 | + bot.sendMessage(update.message.chat_id, |
| 51 | + text='I see! Please send me a photo of yourself, ' |
| 52 | + 'so I know what you look like, or send /skip if you don\'t want to.') |
| 53 | + |
| 54 | + return PHOTO |
| 55 | + |
| 56 | + |
| 57 | +def photo(bot, update): |
| 58 | + user = update.message.from_user |
| 59 | + photo_file = bot.getFile(update.message.photo[-1].file_id) |
| 60 | + photo_file.download('user_photo.jpg') |
| 61 | + logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg')) |
| 62 | + bot.sendMessage(update.message.chat_id, text='Gorgeous! Now, send me your location please, ' |
| 63 | + 'or send /skip if you don\'t want to.') |
| 64 | + |
| 65 | + return LOCATION |
| 66 | + |
| 67 | + |
| 68 | +def skip_photo(bot, update): |
| 69 | + user = update.message.from_user |
| 70 | + logger.info("User %s did not send a photo." % user.first_name) |
| 71 | + bot.sendMessage(update.message.chat_id, text='I bet you look great! Now, send me your ' |
| 72 | + 'location please, or send /skip.') |
| 73 | + |
| 74 | + return LOCATION |
| 75 | + |
| 76 | + |
| 77 | +def location(bot, update): |
| 78 | + user = update.message.from_user |
| 79 | + user_location = update.message.location |
| 80 | + logger.info("Location of %s: %f / %f" |
| 81 | + % (user.first_name, user_location.latitude, user_location.longitude)) |
| 82 | + bot.sendMessage(update.message.chat_id, text='Maybe I can visit you sometime! ' |
| 83 | + 'At last, tell me something about yourself.') |
| 84 | + |
| 85 | + return BIO |
| 86 | + |
| 87 | + |
| 88 | +def skip_location(bot, update): |
| 89 | + user = update.message.from_user |
| 90 | + logger.info("User %s did not send a location." % user.first_name) |
| 91 | + bot.sendMessage(update.message.chat_id, text='You seem a bit paranoid! ' |
| 92 | + 'At last, tell me something about yourself.') |
| 93 | + |
| 94 | + return BIO |
| 95 | + |
| 96 | + |
| 97 | +def bio(bot, update): |
| 98 | + user = update.message.from_user |
| 99 | + logger.info("Bio of %s: %s" % (user.first_name, update.message.text)) |
| 100 | + bot.sendMessage(update.message.chat_id, |
| 101 | + text='Thank you! I hope we can talk again some day.') |
| 102 | + |
| 103 | + return ConversationHandler.END |
| 104 | + |
| 105 | + |
| 106 | +def cancel(bot, update): |
| 107 | + user = update.message.from_user |
| 108 | + logger.info("User %s canceled the conversation." % user.first_name) |
| 109 | + bot.sendMessage(update.message.chat_id, |
| 110 | + text='Bye! I hope we can talk again some day.') |
| 111 | + |
| 112 | + return ConversationHandler.END |
| 113 | + |
| 114 | + |
| 115 | +def error(bot, update, error): |
| 116 | + logger.warn('Update "%s" caused error "%s"' % (update, error)) |
| 117 | + |
| 118 | + |
| 119 | +def main(): |
| 120 | + # Create the EventHandler and pass it your bot's token. |
| 121 | + updater = Updater("TOKEN") |
| 122 | + |
| 123 | + # Get the dispatcher to register handlers |
| 124 | + dp = updater.dispatcher |
| 125 | + |
| 126 | + # Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO |
| 127 | + conv_handler = ConversationHandler( |
| 128 | + entry_points=[CommandHandler('start', start)], |
| 129 | + |
| 130 | + states={ |
| 131 | + GENDER: [RegexHandler('^(Boy|Girl|Other)$', gender)], |
| 132 | + |
| 133 | + PHOTO: [MessageHandler([Filters.photo], photo), |
| 134 | + CommandHandler('skip', skip_photo)], |
| 135 | + |
| 136 | + LOCATION: [MessageHandler([Filters.location], location), |
| 137 | + CommandHandler('skip', skip_location)], |
| 138 | + |
| 139 | + BIO: [MessageHandler([Filters.text], bio)] |
| 140 | + }, |
| 141 | + |
| 142 | + fallbacks=[CommandHandler('cancel', cancel)] |
| 143 | + ) |
| 144 | + |
| 145 | + dp.add_handler(conv_handler) |
| 146 | + |
| 147 | + # log all errors |
| 148 | + dp.add_error_handler(error) |
| 149 | + |
| 150 | + # Start the Bot |
| 151 | + updater.start_polling() |
| 152 | + |
| 153 | + # Run the bot until the you presses Ctrl-C or the process receives SIGINT, |
| 154 | + # SIGTERM or SIGABRT. This should be used most of the time, since |
| 155 | + # start_polling() is non-blocking and will stop the bot gracefully. |
| 156 | + updater.idle() |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + main() |
0 commit comments