diff options
| -rw-r--r-- | proof_of_concept/telegram_keyboard_buttons.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/proof_of_concept/telegram_keyboard_buttons.py b/proof_of_concept/telegram_keyboard_buttons.py new file mode 100644 index 0000000..6259961 --- /dev/null +++ b/proof_of_concept/telegram_keyboard_buttons.py @@ -0,0 +1,94 @@ +''' +Proof of concept to send messages to bot, and get reply using keyboard buttons. + +Based on: +https://docs.python-telegram-bot.org/en/stable/examples.inlinekeyboard.html +and a bit of: +https://docs.python-telegram-bot.org/en/stable/examples.echobot.html +''' + + +import logging + +import yaml +from telegram import InlineKeyboardButton, ReplyKeyboardMarkup, Update + +from telegram.ext import ( + Application, + CommandHandler, + ContextTypes, + MessageHandler, + filters, +) + +logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO) +# set higher logging level for httpx to avoid all GET and POST requests being logged +logging.getLogger("httpx").setLevel(logging.WARNING) +logger = logging.getLogger(__name__) + + +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send message on `/start`.""" + + # Get user that sent /start and log their name + user = update.message.from_user + logger.info("User %s started the conversation.", user.first_name) + + # Build InlineKeyboard where each button has a displayed text + # and a string as callback_data + + # The keyboard is a list of button rows, where each row is in turn + + # a list (hence `[[...]]`). + + keyboard = [ + [ + InlineKeyboardButton("/1", callback_data='/2'), + InlineKeyboardButton("/2", callback_data='/2'), + ] + ] + + reply_markup = ReplyKeyboardMarkup(keyboard) + + # Send message with text and appended InlineKeyboard + + await update.message.reply_text("Start handler, Choose a route", reply_markup=reply_markup) + + # Tell ConversationHandler that we're in state `FIRST` now + + return None + + +async def one(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /1 is issued.""" + + await update.message.reply_text("First") + + +async def two(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /2 is issued.""" + + await update.message.reply_text("Second") + + +def main() -> None: + """Run the bot.""" + + # Create the Application and pass it your bot's token. + with open("src/config.yml", "r") as file: + config = yaml.safe_load(file) + + application = Application.builder().token(config["telegram"]["token"]).build() + + # Add command handlers + application.add_handler(CommandHandler("1", one)) + application.add_handler(CommandHandler("2", two)) + # Everything except the commands match start + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, start)) + # Run the bot until the user presses Ctrl-C + + application.run_polling(allowed_updates=Update.ALL_TYPES) + + +if __name__ == "__main__": + main() |
