Skip to content

Commit f5c57cd

Browse files
authored
new inlinekeyboard example (python-telegram-bot#355)
1 parent c51c222 commit f5c57cd

File tree

1 file changed

+19
-78
lines changed

1 file changed

+19
-78
lines changed

examples/inlinekeyboard.py

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,49 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
#
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.
65
# This program is dedicated to the public domain under the CC0 license.
76

87
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
1310

1411
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
15-
level=logging.DEBUG)
12+
level=logging.INFO)
1613

17-
# Define the different states a chat can be in
18-
MENU, AWAIT_CONFIRMATION, AWAIT_INPUT = range(3)
1914

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')],
2518

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')]]
3320

21+
reply_markup = InlineKeyboardMarkup(keyboard)
3422

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)
4024

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())
4625

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):
6527
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)
8732

8833

8934
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.")
9136

9237

9338
def error(bot, update, error):
9439
logging.warning('Update "%s" caused error "%s"' % (update, error))
9540

41+
9642
# Create the Updater and pass it your bot's token.
9743
updater = Updater("TOKEN")
9844

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))
10647
updater.dispatcher.add_handler(CommandHandler('help', help))
10748
updater.dispatcher.add_error_handler(error)
10849

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy