Last active
June 9, 2022 23:13
-
-
Save nonchris/2e5841353860093c8a74bca05d7e10d1 to your computer and use it in GitHub Desktop.
Telegram Bot Dialogue Keyboard (minimal example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This code shows how to create a custom keyboard for a telegram bot | |
Using https://github.com/python-telegram-bot/python-telegram-bot | |
""" | |
from telegram.ext import Updater | |
from telegram.ext import CommandHandler | |
from telegram.ext import MessageHandler, Filters | |
from telegram import Bot | |
from telegram import KeyboardButton | |
from telegram import ReplyKeyboardMarkup | |
API_Key = #your key | |
updater = Updater(API_Key, use_context=True) | |
dispatcher = updater.dispatcher | |
bot = Bot(token=API_Key) | |
#button 1 as object | |
bt1 = KeyboardButton("button1") | |
#generates keyboard from list of buttons, button2 as string defined | |
keyboard = ReplyKeyboardMarkup([[bt1, "button2"]]) | |
#sends keyboard when user says /hi | |
def say_hello(update, context): | |
context.bot.send_message(text= "hi", chat_id=update.effective_chat.id,\ | |
reply_markup=keyboard) | |
hello_handler = CommandHandler("hi", say_hello) | |
dispatcher.add_handler(hello_handler) | |
updater.start_polling() | |
updater.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment