/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
telegram_bot_examples/show_alert.py
62 строки
1 KB
gil9red
Refactoring. Using black code style
28 июн 2023, 11:16
28 июн 2023, 11:16
4e3e19f
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" # pip install python-telegram-bot from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import ( MessageHandler, CommandHandler, Filters, CallbackContext, CallbackQueryHandler, ) from common import get_logger, log_func, start_bot, run_main log = get_logger(__file__) @log_func(log) def on_start(update: Update, _: CallbackContext): message = update.effective_message reply_markup = InlineKeyboardMarkup( [ [ InlineKeyboardButton("dog", callback_data="dog"), InlineKeyboardButton("cat", callback_data="cat"), InlineKeyboardButton("other", callback_data="other"), ], ] ) message.reply_text(f"Hello {message.chat.first_name}!", reply_markup=reply_markup) @log_func(log) def on_select(update: Update, _: CallbackContext): query = update.callback_query if query.data == "cat": query.answer(text="You chose cat!", show_alert=True) elif query.data == "dog": query.answer(text="You chose dog!", show_alert=True) else: query.answer("Nothing") def main(): handlers = [ CommandHandler("start", on_start), MessageHandler(Filters.text, on_start), CallbackQueryHandler(on_select), ] start_bot(log, handlers) if __name__ == "__main__": run_main(main, log)