Skip to content

Instantly share code, notes, and snippets.

@nonchris
Last active June 9, 2022 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonchris/2e5841353860093c8a74bca05d7e10d1 to your computer and use it in GitHub Desktop.
Save nonchris/2e5841353860093c8a74bca05d7e10d1 to your computer and use it in GitHub Desktop.
Telegram Bot Dialogue Keyboard (minimal example)
"""
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