telegram_bots/bots/modules/spoiler.py

37 lines
1.5 KiB
Python
Raw Normal View History

2020-12-11 10:37:12 +00:00
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'),
2020-12-11 10:41:12 +00:00
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton('Read content', callback_data='read {}'.format(sm.pk))]])
2020-12-11 10:37:12 +00:00
)])
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')
2020-12-11 10:45:26 +00:00
update.callback_query.answer(sm.text, show_alert=True)
2020-12-11 10:37:12 +00:00
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