jessicacardoso

telebot-buttons

Feb 17th, 2020
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import telebot
  2. from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
  3.  
  4.  
  5. TELEGRAM_TOKEN = '<TOKEN>'
  6.  
  7. bot = telebot.TeleBot(TELEGRAM_TOKEN)
  8.  
  9. # Menu contendo as opções Botão 1 e Botão 2
  10. def options():
  11.     markup = InlineKeyboardMarkup()
  12.     markup.row_width = 2 # Quantos botões haverão no máximo por linha
  13.     # adicionar botões na estrutura
  14.     # primeiro argumento o que o usuário olha
  15.     # callback_data (string que você utilizará no seu programa)
  16.     markup.add(InlineKeyboardButton("Botão 1", callback_data="op1"),
  17.                InlineKeyboardButton("Botão 2", callback_data="op2"))
  18.     return markup
  19.  
  20. # Verifica qual botão foi pressionado
  21. @bot.callback_query_handler(func=lambda call: True)
  22. def callback_query(call):
  23.     # Identificadores para saber qual mensagem de qual chat editar
  24.     cid = call.message.chat.id
  25.     mid = call.message.message_id
  26.  
  27.     if call.data == "op1":
  28.         bot.edit_message_text("Você selecionou a opção do Botão 1", chat_id=cid, message_id= mid)
  29.     elif call.data == "op2":
  30.         bot.edit_message_text("Você selecionou a opção do Botão 2", chat_id=cid, message_id= mid)
  31.  
  32. # Mensagem para iniciar o bot
  33. @bot.message_handler(commands=['start'])
  34. def start(message):
  35.     bot.reply_to(message, "Use o comando /bottons para escolher um botão.")
  36.  
  37. # Mensagem que será editada tem o reply_markup com as opções.
  38. @bot.message_handler(commands=['bottons'])
  39. def message_handler(message):
  40.     bot.send_message(message.chat.id, "Pressione um botão para editar essa mensagem.", reply_markup=options())
  41.  
  42. bot.polling(none_stop=True)
Add Comment
Please, Sign In to add comment