from django.db import models from telegram import Update, InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardMarkup, \ InlineKeyboardButton from telegram.ext import Dispatcher, CallbackContext, InlineQueryHandler, CallbackQueryHandler from bots.models import TelegramBotModuleConfig class SpoilerMessage(models.Model): text = models.TextField() class SpoilerBotModuleConfig(TelegramBotModuleConfig): MODULE_NAME = 'Spoiler' def inline_handler(self, update: Update, ctx: CallbackContext): sm = SpoilerMessage.objects.create(text=update.inline_query.query) update.inline_query.answer(results=[InlineQueryResultArticle( id=str(sm.pk), title='Send spoiler', input_message_content=InputTextMessageContent('This is a spoiler'), reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton('Read content', callback_data='read {}'.format(sm.pk))]]) )]) def read_handler(self, update: Update, ctx: CallbackContext): _, smid = update.callback_query.data.split() try: sm = SpoilerMessage.objects.get(pk=smid) except SpoilerMessage.DoesNotExist: return update.callback_query.answer('Message does not exist') update.callback_query.answer(sm.text, show_alert=True) def build_dispatcher(self, dispatcher: Dispatcher): dispatcher.add_handler(InlineQueryHandler(self.inline_handler)) dispatcher.add_handler(CallbackQueryHandler(self.read_handler, pattern=r'^read \d+$')) return dispatcher