From 9fa029d896de2a25da22a535d6eb8f9dd67b5c61 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Fri, 11 Dec 2020 13:37:12 +0300 Subject: [PATCH] add spoiler bot --- ...4_spoilerbotmoduleconfig_spoilermessage.py | 29 +++++++++++++++ bots/modules/__init__.py | 3 +- bots/modules/spoiler.py | 36 +++++++++++++++++++ requirements.txt | 8 +++-- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 bots/migrations/0024_spoilerbotmoduleconfig_spoilermessage.py create mode 100644 bots/modules/spoiler.py diff --git a/bots/migrations/0024_spoilerbotmoduleconfig_spoilermessage.py b/bots/migrations/0024_spoilerbotmoduleconfig_spoilermessage.py new file mode 100644 index 0000000..8dbb462 --- /dev/null +++ b/bots/migrations/0024_spoilerbotmoduleconfig_spoilermessage.py @@ -0,0 +1,29 @@ +# Generated by Django 3.0rc1 on 2020-12-11 10:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bots', '0023_overlaybotmoduleconfig_send_avatar_on_start'), + ] + + operations = [ + migrations.CreateModel( + name='SpoilerBotModuleConfig', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='SpoilerMessage', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.TextField()), + ], + ), + ] diff --git a/bots/modules/__init__.py b/bots/modules/__init__.py index 3988a00..95f2bb4 100644 --- a/bots/modules/__init__.py +++ b/bots/modules/__init__.py @@ -3,6 +3,7 @@ from .channel_helper import ChannelHelperBotModuleConfig, QueuedItem, ChannelHel from .echo import EchoBotModuleConfig from .cyberlina import CyberLinaBotModuleConfig from .ping import PingBotModuleConfig +from .spoiler import SpoilerBotModuleConfig BOT_MODULES = [EchoBotModuleConfig, ChannelHelperBotModuleConfig, OverlayBotModuleConfig, CyberLinaBotModuleConfig, - PingBotModuleConfig] + PingBotModuleConfig, SpoilerBotModuleConfig] diff --git a/bots/modules/spoiler.py b/bots/modules/spoiler.py new file mode 100644 index 0000000..56d08d6 --- /dev/null +++ b/bots/modules/spoiler.py @@ -0,0 +1,36 @@ +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, 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 diff --git a/requirements.txt b/requirements.txt index fdcf8a0..c6c6ad4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ amqp==2.3.2 +APScheduler==3.6.3 asgiref==3.2.3 asn1crypto==0.24.0 async-generator==1.10 @@ -16,6 +17,7 @@ chardet==3.0.4 constantly==15.1.0 cryptography==2.8 daphne==2.4.0 +decorator==4.4.2 Django==3.0rc1 django-bootstrap4==1.0.1 django-crispy-forms==1.8.1 @@ -49,7 +51,6 @@ oauthlib==3.0.1 packaging==19.2 Pillow==5.4.1 priority==1.3.0 -psycopg2-binary==2.7.6.1 pyaes==1.6.1 pyasn1==0.4.8 pyasn1-modules==0.2.7 @@ -63,8 +64,8 @@ PySocks==1.6.8 pyTelegramBotAPI==3.6.6 python-anticaptcha==0.3.1 python-crontab==2.3.6 -python-dateutil==2.7.5 -python-telegram-bot==12.2.0 +python-dateutil==2.8.1 +python-telegram-bot==13.1 python-twitter==3.5 pytz==2018.9 PyYAML==3.13 @@ -80,6 +81,7 @@ TgCrypto==1.1.1 tornado==6.0.3 Twisted==19.10.0 txaio==18.8.1 +tzlocal==2.1 Unidecode==1.1.1 urllib3==1.24.1 vine==1.2.0