|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | # |
4 | | -# Basic example for a bot that awaits an answer from the user. It's built upon |
5 | | -# the state_machine_bot.py example |
| 4 | +# Basic example for a bot that uses inline keyboards. |
6 | 5 | # This program is dedicated to the public domain under the CC0 license. |
7 | 6 |
|
8 | 7 | import logging |
9 | | -from telegram import Emoji, ForceReply, InlineKeyboardButton, \ |
10 | | - InlineKeyboardMarkup |
11 | | -from telegram.ext import Updater, CommandHandler, MessageHandler, \ |
12 | | - CallbackQueryHandler, Filters |
| 8 | +from telegram import InlineKeyboardButton, InlineKeyboardMarkup |
| 9 | +from telegram.ext import Updater, CommandHandler, CallbackQueryHandler |
13 | 10 |
|
14 | 11 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
15 | | - level=logging.DEBUG) |
| 12 | + level=logging.INFO) |
16 | 13 |
|
17 | | -# Define the different states a chat can be in |
18 | | -MENU, AWAIT_CONFIRMATION, AWAIT_INPUT = range(3) |
19 | 14 |
|
20 | | -# Python 2 and 3 unicode differences |
21 | | -try: |
22 | | - YES, NO = (Emoji.THUMBS_UP_SIGN.decode('utf-8'), Emoji.THUMBS_DOWN_SIGN.decode('utf-8')) |
23 | | -except AttributeError: |
24 | | - YES, NO = (Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN) |
| 15 | +def start(bot, update): |
| 16 | + keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'), |
| 17 | + InlineKeyboardButton("Option 2", callback_data='2')], |
25 | 18 |
|
26 | | -# States are saved in a dict that maps chat_id -> state |
27 | | -state = dict() |
28 | | -# Sometimes you need to save data temporarily |
29 | | -context = dict() |
30 | | -# This dict is used to store the settings value for the chat. |
31 | | -# Usually, you'd use persistence for this (e.g. sqlite). |
32 | | -values = dict() |
| 19 | + [InlineKeyboardButton("Option 3", callback_data='3')]] |
33 | 20 |
|
| 21 | + reply_markup = InlineKeyboardMarkup(keyboard) |
34 | 22 |
|
35 | | -# Example handler. Will be called on the /set command and on regular messages |
36 | | -def set_value(bot, update): |
37 | | - chat_id = update.message.chat_id |
38 | | - user_id = update.message.from_user.id |
39 | | - user_state = state.get(chat_id, MENU) |
| 23 | + bot.sendMessage(update.message.chat_id, text="Please choose:", reply_markup=reply_markup) |
40 | 24 |
|
41 | | - if user_state == MENU: |
42 | | - state[user_id] = AWAIT_INPUT # set the state |
43 | | - bot.sendMessage(chat_id, |
44 | | - text="Please enter your settings value", |
45 | | - reply_markup=ForceReply()) |
46 | 25 |
|
47 | | - |
48 | | -def entered_value(bot, update): |
49 | | - chat_id = update.message.chat_id |
50 | | - user_id = update.message.from_user.id |
51 | | - chat_state = state.get(user_id, MENU) |
52 | | - |
53 | | - # Check if we are waiting for input |
54 | | - if chat_state == AWAIT_INPUT: |
55 | | - state[user_id] = AWAIT_CONFIRMATION |
56 | | - |
57 | | - # Save the user id and the answer to context |
58 | | - context[user_id] = update.message.text |
59 | | - reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton(YES, callback_data=YES), |
60 | | - InlineKeyboardButton(NO, callback_data=NO)]]) |
61 | | - bot.sendMessage(chat_id, text="Are you sure?", reply_markup=reply_markup) |
62 | | - |
63 | | - |
64 | | -def confirm_value(bot, update): |
| 26 | +def button(bot, update): |
65 | 27 | query = update.callback_query |
66 | | - chat_id = query.message.chat_id |
67 | | - user_id = query.from_user.id |
68 | | - text = query.data |
69 | | - user_state = state.get(user_id, MENU) |
70 | | - user_context = context.get(user_id, None) |
71 | | - |
72 | | - # Check if we are waiting for confirmation and the right user answered |
73 | | - if user_state == AWAIT_CONFIRMATION: |
74 | | - del state[user_id] |
75 | | - del context[user_id] |
76 | | - bot.answerCallbackQuery(query.id, text="Ok!") |
77 | | - if text == YES: |
78 | | - values[user_id] = user_context |
79 | | - bot.editMessageText(text="Changed value to %s." % values[user_id], |
80 | | - chat_id=chat_id, |
81 | | - message_id=query.message.message_id) |
82 | | - else: |
83 | | - bot.editMessageText(text="Alright, value is still %s." % |
84 | | - values.get(user_id, 'not set'), |
85 | | - chat_id=chat_id, |
86 | | - message_id=query.message.message_id) |
| 28 | + |
| 29 | + bot.editMessageText(text="Selected option: %s" % query.data, |
| 30 | + chat_id=query.message.chat_id, |
| 31 | + message_id=query.message.message_id) |
87 | 32 |
|
88 | 33 |
|
89 | 34 | def help(bot, update): |
90 | | - bot.sendMessage(update.message.chat_id, text="Use /set to test this bot.") |
| 35 | + bot.sendMessage(update.message.chat_id, text="Use /start to test this bot.") |
91 | 36 |
|
92 | 37 |
|
93 | 38 | def error(bot, update, error): |
94 | 39 | logging.warning('Update "%s" caused error "%s"' % (update, error)) |
95 | 40 |
|
| 41 | + |
96 | 42 | # Create the Updater and pass it your bot's token. |
97 | 43 | updater = Updater("TOKEN") |
98 | 44 |
|
99 | | -# The command |
100 | | -updater.dispatcher.add_handler(CommandHandler('set', set_value)) |
101 | | -# The answer |
102 | | -updater.dispatcher.add_handler(MessageHandler([Filters.text], entered_value)) |
103 | | -# The confirmation |
104 | | -updater.dispatcher.add_handler(CallbackQueryHandler(confirm_value)) |
105 | | -updater.dispatcher.add_handler(CommandHandler('start', help)) |
| 45 | +updater.dispatcher.add_handler(CommandHandler('start', start)) |
| 46 | +updater.dispatcher.add_handler(CallbackQueryHandler(button)) |
106 | 47 | updater.dispatcher.add_handler(CommandHandler('help', help)) |
107 | 48 | updater.dispatcher.add_error_handler(error) |
108 | 49 |
|
|
0 commit comments