add feedback module
This commit is contained in:
parent
ffc9c42370
commit
f397545412
@ -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')),
|
||||
],
|
||||
),
|
||||
]
|
@ -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]
|
||||
|
48
bots/modules/feedback.py
Normal file
48
bots/modules/feedback.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user