From f3975454127d680a542ac4662c9b537b2bc2091b Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Sat, 20 Mar 2021 15:21:27 +0300 Subject: [PATCH] add feedback module --- ...duleconfig_feedbackchat_feedbackmessage.py | 40 ++++++++++++++++ bots/modules/__init__.py | 3 +- bots/modules/feedback.py | 48 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 bots/migrations/0004_feedbackbotmoduleconfig_feedbackchat_feedbackmessage.py create mode 100644 bots/modules/feedback.py diff --git a/bots/migrations/0004_feedbackbotmoduleconfig_feedbackchat_feedbackmessage.py b/bots/migrations/0004_feedbackbotmoduleconfig_feedbackchat_feedbackmessage.py new file mode 100644 index 0000000..1f27871 --- /dev/null +++ b/bots/migrations/0004_feedbackbotmoduleconfig_feedbackchat_feedbackmessage.py @@ -0,0 +1,40 @@ +# Generated by Django 3.1.7 on 2021-03-20 12:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('bots', '0003_robotbotmoduleconfig'), + ] + + operations = [ + migrations.CreateModel( + name='FeedbackBotModuleConfig', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('forward_chat_id', models.BigIntegerField()), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='FeedbackChat', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('chat_id', models.BigIntegerField()), + ], + ), + migrations.CreateModel( + name='FeedbackMessage', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('forwarded_chat_id', models.BigIntegerField(db_index=True)), + ('forwarded_message_id', models.BigIntegerField(db_index=True)), + ('chat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='bots.feedbackchat')), + ], + ), + ] diff --git a/bots/modules/__init__.py b/bots/modules/__init__.py index 983e999..07b188c 100644 --- a/bots/modules/__init__.py +++ b/bots/modules/__init__.py @@ -1,3 +1,4 @@ +from .feedback import FeedbackBotModuleConfig from .overlay import OverlayBotModuleConfig from .channel_helper import ChannelHelperBotModuleConfig, QueuedItem from .echo import EchoBotModuleConfig @@ -7,4 +8,4 @@ from .robot import RobotBotModuleConfig from .spoiler import SpoilerBotModuleConfig BOT_MODULES = [EchoBotModuleConfig, ChannelHelperBotModuleConfig, OverlayBotModuleConfig, CyberLinaBotModuleConfig, - PingBotModuleConfig, SpoilerBotModuleConfig, RobotBotModuleConfig] + PingBotModuleConfig, SpoilerBotModuleConfig, RobotBotModuleConfig, FeedbackBotModuleConfig] diff --git a/bots/modules/feedback.py b/bots/modules/feedback.py new file mode 100644 index 0000000..34eebaa --- /dev/null +++ b/bots/modules/feedback.py @@ -0,0 +1,48 @@ +from django.db import models +from telegram import Update +from telegram.ext import Dispatcher, MessageHandler, Filters, CallbackContext + +from bots.models import TelegramBotModuleConfig + + +class FeedbackChat(models.Model): + chat_id = models.BigIntegerField() + + +class FeedbackMessage(models.Model): + chat = models.ForeignKey(FeedbackChat, on_delete=models.CASCADE, related_name='messages') + forwarded_chat_id = models.BigIntegerField(db_index=True) + forwarded_message_id = models.BigIntegerField(db_index=True) + + +class FeedbackBotModuleConfig(TelegramBotModuleConfig): + forward_chat_id = models.BigIntegerField() + + MODULE_NAME = 'Feedback' + + def build_dispatcher(self, dispatcher: Dispatcher): + dispatcher.add_handler(MessageHandler(Filters.chat(chat_id=self.forward_chat_id) & Filters.reply, self.handle_internal_message)) + dispatcher.add_handler(MessageHandler(Filters.private & ~Filters.chat(chat_id=self.forward_chat_id), self.handle_external_message)) + + def _get_chat(self, update: Update): + chat, _ = FeedbackChat.objects.get_or_create(chat_id=update.effective_chat.id) + return chat + + def handle_internal_message(self, update: Update, ctx: CallbackContext): + try: + message = FeedbackMessage.objects.get(forwarded_chat_id=update.effective_message.chat_id, + forwarded_message_id=update.effective_message.reply_to_message.message_id) + update.message.copy(message.chat.chat_id) + FeedbackMessage.objects.create(chat=message.chat, forwarded_chat_id=update.effective_message.chat_id, + forwarded_message_id=update.effective_message.message_id) + except FeedbackMessage.DoesNotExist: + update.effective_message.reply_text('I don\'t know in which chat to forward this message') + + def handle_external_message(self, update: Update, ctx: CallbackContext): + chat = self._get_chat(update) + # last_message = FeedbackMessage.objects.filter(chat=chat).last() + msg = update.message.forward(self.forward_chat_id) + # msg = update.message.copy(self.forward_chat_id, + # reply_to_message_id=last_message.forwarded_message_id if last_message else None) + FeedbackMessage.objects.create(chat=chat, forwarded_chat_id=self.forward_chat_id, + forwarded_message_id=msg.message_id)