From 219e28b42055c9d0700eb46e402c0d61a25d730f Mon Sep 17 00:00:00 2001 From: owl Date: Fri, 24 Jan 2025 03:54:34 +0700 Subject: [PATCH] add button --- bot.py | 25 +++++++++++++++++++++++-- tgbot/shit/handlers.py | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index e5bccbf..cd3c86a 100644 --- a/bot.py +++ b/bot.py @@ -1,9 +1,10 @@ from telegram import Update -from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler +from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler, \ + CallbackContext, CallbackQueryHandler import logging from tgbot.config import TOKEN -from tgbot.shit.handlers import handle_xitter, handle_red_ebalo, handle_hentai +from tgbot.shit.handlers import handle_xitter, handle_red_ebalo, handle_hentai, handle_cute_button from tgbot.shit.render import render_text_on_image, image_to_string from io import BytesIO @@ -12,6 +13,7 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s logger = logging.getLogger(__name__) + async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id=update.effective_chat.id, text='WE ARE A PARTY OF UTTER MADNESS, ' @@ -37,6 +39,20 @@ async def image2string(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text(s) +async def callback_query_handler(update: Update, context: CallbackContext): + query = update.callback_query + + if query.data == "show_cute_popup": + await context.bot.answer_callback_query( + callback_query_id=query.id, + text="Hey! This button is for you! Thank you for clicking it, you are awesome!", + show_alert=True + ) + + else: + await query.answer() + + async def handle_inline(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.inline_query.query if not query: @@ -50,6 +66,10 @@ async def handle_inline(update: Update, context: ContextTypes.DEFAULT_TYPE): await handle_hentai(update, context) return + if query.startswith("button!"): + await handle_cute_button(update, context) + return + await handle_red_ebalo(update, context) @@ -64,6 +84,7 @@ def main(): application.add_handler(MessageHandler(filters.PHOTO, image2string)) application.add_handler(picture_handler) application.add_handler(InlineQueryHandler(handle_inline)) + application.add_handler(CallbackQueryHandler(callback_query_handler)) application.run_polling() diff --git a/tgbot/shit/handlers.py b/tgbot/shit/handlers.py index 57fc634..8a49762 100644 --- a/tgbot/shit/handlers.py +++ b/tgbot/shit/handlers.py @@ -1,6 +1,9 @@ +import uuid + import requests from pybooru import Danbooru -from telegram import Update +from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, \ + InputTextMessageContent from telegram.ext import ContextTypes from tgbot.config import STICKER_SHITHOLE, BOORU_API_URL, INLINE_QUERY_CACHE_SECONDS, DANBOORU_API_KEY @@ -53,6 +56,21 @@ async def handle_red_ebalo(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.answer_inline_query(update.inline_query.id, results) +async def handle_cute_button(update: Update, context: ContextTypes.DEFAULT_TYPE): + + button = InlineKeyboardButton("Click me!", callback_data="show_cute_popup") + keyboard = InlineKeyboardMarkup([[button]]) + + result = InlineQueryResultArticle( + id=str(uuid.uuid4()), + title="Cute button!", + input_message_content=InputTextMessageContent("Here is a post with a cute button:"), + reply_markup=keyboard + ) + + await context.bot.answer_inline_query(update.inline_query.id, [result]) + + async def handle_hentai(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.inline_query.query.replace("hentai", "").replace("—", "--")