本文整理匯總了Python中telegram.KeyboardButton方法的典型用法代碼示例。如果您正苦於以下問題:Python telegram.KeyboardButton方法的具體用法?Python telegram.KeyboardButton怎麽用?Python telegram.KeyboardButton使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類telegram
的用法示例。
在下文中一共展示了telegram.KeyboardButton方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: start
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def start(bot, update):
"""
Shows an welcome message and help info about the available commands.
"""
me = bot.get_me()
# Welcome message
msg = _("Hello!\n")
msg += _("I'm {0} and I came here to help you.\n").format(me.first_name)
msg += _("What would you like to do?\n\n")
msg += _("/support - Opens a new support ticket\n")
msg += _("/settings - Settings of your account\n\n")
# Commands menu
main_menu_keyboard = [[telegram.KeyboardButton('/support')],
[telegram.KeyboardButton('/settings')]]
reply_kb_markup = telegram.ReplyKeyboardMarkup(main_menu_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
# Send the message with menu
bot.send_message(chat_id=update.message.chat_id,
text=msg,
reply_markup=reply_kb_markup)
示例2: settings
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def settings(bot, update):
"""
Configure the messages language using a custom keyboard.
"""
# Languages message
msg = _("Please, choose a language:\n")
msg += "en_US - English (US)\n"
msg += "pt_BR - Português (Brasil)\n"
# Languages menu
languages_keyboard = [
[telegram.KeyboardButton('en_US - English (US)')],
[telegram.KeyboardButton('pt_BR - Português (Brasil)')]
]
reply_kb_markup = telegram.ReplyKeyboardMarkup(languages_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
# Sends message with languages menu
bot.send_message(chat_id=update.message.chat_id,
text=msg,
reply_markup=reply_kb_markup)
示例3: __add_credit_menu
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def __add_credit_menu(self):
"""Add more credit to the account."""
log.debug("Displaying __add_credit_menu")
# Create a payment methods keyboard
keyboard = list()
# Add the supported payment methods to the keyboard
# Cash
keyboard.append([telegram.KeyboardButton(self.loc.get("menu_cash"))])
# Telegram Payments
if configloader.config["Credit Card"]["credit_card_token"] != "":
keyboard.append([telegram.KeyboardButton(self.loc.get("menu_credit_card"))])
# Keyboard: go back to the previous menu
keyboard.append([telegram.KeyboardButton(self.loc.get("menu_cancel"))])
# Send the keyboard to the user
self.bot.send_message(self.chat.id, self.loc.get("conversation_payment_method"),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
# Wait for a reply from the user
selection = self.__wait_for_specific_message([self.loc.get("menu_cash"), self.loc.get("menu_credit_card"), self.loc.get("menu_cancel")],
cancellable=True)
# If the user has selected the Cash option...
if selection == self.loc.get("menu_cash"):
# Go to the pay with cash function
self.bot.send_message(self.chat.id,
self.loc.get("payment_cash", user_cash_id=self.user.identifiable_str()))
# If the user has selected the Credit Card option...
elif selection == self.loc.get("menu_credit_card"):
# Go to the pay with credit card function
self.__add_credit_cc()
# If the user has selected the Cancel option...
elif isinstance(selection, CancelSignal):
# Send him back to the previous menu
return
示例4: __add_credit_cc
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def __add_credit_cc(self):
"""Add money to the wallet through a credit card payment."""
log.debug("Displaying __add_credit_cc")
# Create a keyboard to be sent later
presets = list(map(lambda s: s.strip(" "), configloader.config["Credit Card"]["payment_presets"].split('|')))
keyboard = [[telegram.KeyboardButton(str(utils.Price(preset, self.loc)))] for preset in presets]
keyboard.append([telegram.KeyboardButton(self.loc.get("menu_cancel"))])
# Boolean variable to check if the user has cancelled the action
cancelled = False
# Loop used to continue asking if there's an error during the input
while not cancelled:
# Send the message and the keyboard
self.bot.send_message(self.chat.id, self.loc.get("payment_cc_amount"),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
# Wait until a valid amount is sent
selection = self.__wait_for_regex(r"([0-9]+(?:[.,][0-9]+)?|" + self.loc.get("menu_cancel") + r")", cancellable=True)
# If the user cancelled the action
if isinstance(selection, CancelSignal):
# Exit the loop
cancelled = True
continue
# Convert the amount to an integer
value = utils.Price(selection, self.loc)
# Ensure the amount is within the range
if value > utils.Price(int(configloader.config["Credit Card"]["max_amount"]), self.loc):
self.bot.send_message(self.chat.id,
self.loc.get("error_payment_amount_over_max", max_amount=utils.Price(configloader.config["Credit Card"]["max_amount"], self.loc)))
continue
elif value < utils.Price(int(configloader.config["Credit Card"]["min_amount"]), self.loc):
self.bot.send_message(self.chat.id,
self.loc.get("error_payment_amount_under_min", min_amount=utils.Price(configloader.config["Credit Card"]["min_amount"], self.loc)))
continue
break
# If the user cancelled the action...
else:
# Exit the function
return
# Issue the payment invoice
self.__make_payment(amount=value)
示例5: __products_menu
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def __products_menu(self):
"""Display the admin menu to select a product to edit."""
log.debug("Displaying __products_menu")
# Get the products list from the db
products = self.session.query(db.Product).filter_by(deleted=False).all()
# Create a list of product names
product_names = [product.name for product in products]
# Insert at the start of the list the add product option, the remove product option and the Cancel option
product_names.insert(0, self.loc.get("menu_cancel"))
product_names.insert(1, self.loc.get("menu_add_product"))
product_names.insert(2, self.loc.get("menu_delete_product"))
# Create a keyboard using the product names
keyboard = [[telegram.KeyboardButton(product_name)] for product_name in product_names]
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id, self.loc.get("conversation_admin_select_product"),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
# Wait for a reply from the user
selection = self.__wait_for_specific_message(product_names, cancellable=True)
# If the user has selected the Cancel option...
if isinstance(selection, CancelSignal):
# Exit the menu
return
# If the user has selected the Add Product option...
elif selection == self.loc.get("menu_add_product"):
# Open the add product menu
self.__edit_product_menu()
# If the user has selected the Remove Product option...
elif selection == self.loc.get("menu_delete_product"):
# Open the delete product menu
self.__delete_product_menu()
# If the user has selected a product
else:
# Find the selected product
product = self.session.query(db.Product).filter_by(name=selection, deleted=False).one()
# Open the edit menu for that specific product
self.__edit_product_menu(product=product)
示例6: __delete_product_menu
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def __delete_product_menu(self):
log.debug("Displaying __delete_product_menu")
# Get the products list from the db
products = self.session.query(db.Product).filter_by(deleted=False).all()
# Create a list of product names
product_names = [product.name for product in products]
# Insert at the start of the list the Cancel button
product_names.insert(0, self.loc.get("menu_cancel"))
# Create a keyboard using the product names
keyboard = [[telegram.KeyboardButton(product_name)] for product_name in product_names]
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id, self.loc.get("conversation_admin_select_product_to_delete"),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
# Wait for a reply from the user
selection = self.__wait_for_specific_message(product_names, cancellable=True)
if isinstance(selection, CancelSignal):
# Exit the menu
return
else:
# Find the selected product
product = self.session.query(db.Product).filter_by(name=selection, deleted=False).one()
# "Delete" the product by setting the deleted flag to true
product.deleted = True
self.session.commit()
# Notify the user
self.bot.send_message(self.chat.id, self.loc.get("success_product_deleted"))
示例7: __user_menu
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def __user_menu(self):
"""Function called from the run method when the user is not an administrator.
Normal bot actions should be placed here."""
log.debug("Displaying __user_menu")
# Loop used to returning to the menu after executing a command
while True:
# Create a keyboard with the user main menu
keyboard = [[telegram.KeyboardButton(self.loc.get("menu_order"))],
[telegram.KeyboardButton(self.loc.get("menu_order_status"))],
[telegram.KeyboardButton(self.loc.get("menu_add_credit"))],
[telegram.KeyboardButton(self.loc.get("menu_language"))],
[telegram.KeyboardButton(self.loc.get("menu_help")),
telegram.KeyboardButton(self.loc.get("menu_bot_info"))]]
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id,
self.loc.get("conversation_open_user_menu",
credit=utils.Price(self.user.credit, self.loc)),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
# Wait for a reply from the user
selection = self.__wait_for_specific_message([
self.loc.get("menu_order"),
self.loc.get("menu_order_status"),
self.loc.get("menu_add_credit"),
self.loc.get("menu_language"),
self.loc.get("menu_help"),
self.loc.get("menu_bot_info"),
])
# After the user reply, update the user data
self.update_user()
# If the user has selected the Order option...
if selection == self.loc.get("menu_order"):
# Open the order menu
self.__order_menu()
# If the user has selected the Order Status option...
elif selection == self.loc.get("menu_order_status"):
# Display the order(s) status
self.__order_status()
# If the user has selected the Add Credit option...
elif selection == self.loc.get("menu_add_credit"):
# Display the add credit menu
self.__add_credit_menu()
# If the user has selected the Language option...
elif selection == self.loc.get("menu_language"):
# Display the language menu
self.__language_menu()
# If the user has selected the Bot Info option...
elif selection == self.loc.get("menu_bot_info"):
# Display information about the bot
self.__bot_info()
# If the user has selected the Help option...
elif selection == self.loc.get("menu_help"):
# Go to the Help menu
self.__help_menu()
示例8: keyboard
# 需要導入模塊: import telegram [as 別名]
# 或者: from telegram import KeyboardButton [as 別名]
def keyboard(bot, update):
user = update.effective_user # type: Optional[User]
conn_id = con_sql.get_connected_chat(user.id)
if conn_id and not conn_id == False:
btn1 = "/disconnect - Disconnect from chat"
btn2 = ""
btn3 = ""
else:
if con_sql.get_history(user.id):
history = con_sql.get_history(user.id)
try:
chat_name1 = dispatcher.bot.getChat(history.chat_id1).title
except:
chat_name1 = ""
try:
chat_name2 = dispatcher.bot.getChat(history.chat_id2).title
except:
chat_name2 = ""
try:
chat_name3 = dispatcher.bot.getChat(history.chat_id3).title
except:
chat_name3 = ""
if chat_name1:
btn1 = "/connect {} - {}".format(history.chat_id1, chat_name1)
else:
btn1 = "/connect - Connect to the chat"
if chat_name2:
btn2 = "/connect {} - {}".format(history.chat_id2, chat_name2)
else:
btn2 = ""
if chat_name3:
btn3 = "/connect {} - {}".format(history.chat_id3, chat_name3)
else:
btn3 = ""
#TODO: Remove except garbage
update.effective_message.reply_text("keyboard updated",
reply_markup=ReplyKeyboardMarkup([[
KeyboardButton("/help - Bot Help"),
KeyboardButton("/donate - Donate"),
KeyboardButton("/notes - Notes")],
[KeyboardButton(btn1)],
[KeyboardButton(btn2)],
[KeyboardButton(btn3)]]))