add markov to cyberlina

This commit is contained in:
2019-11-27 22:23:57 +03:00
parent 6e256ae3a6
commit 61a8028ffe
2 changed files with 93 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, C
from telegram.utils.helpers import mention_html
from bots.models import TelegramBotModuleConfig
from bots.modules._mtg import MTG
class CyberLinaBotModuleConfig(TelegramBotModuleConfig):
@@ -24,6 +25,8 @@ class CyberLinaBotModuleConfig(TelegramBotModuleConfig):
already_ran = JSONField(default='[]')
welcome_reactions = JSONField(default='[]')
inline_reactions = JSONField(default='[]')
_mtg_data = JSONField(default={}, blank=True, null=True)
mtg_train = models.TextField(null=True, blank=True)
MODULE_NAME = 'Киберлиночка'
CUSTOM_WIDGETS = {
@@ -34,8 +37,13 @@ class CyberLinaBotModuleConfig(TelegramBotModuleConfig):
'already_ran': JSONEditor(),
'welcome_reactions': JSONEditor(),
'inline_reactions': JSONEditor(),
'_mtg_data': JSONEditor(),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mtg = MTG(self._mtg_data)
def message_handler(self, update: Update, ctx: CallbackContext):
if not update.effective_chat or not update.effective_user:
return
@@ -89,7 +97,12 @@ class CyberLinaBotModuleConfig(TelegramBotModuleConfig):
id=uuid4(),
title='Не нажимай >_<',
input_message_content=InputTextMessageContent(choice(self.inline_reactions))
)
),
InlineQueryResultArticle(
id=uuid4(),
title='ФлаБеПроЛейка',
input_message_content=InputTextMessageContent(choice(self.mtg.generate_text()))
),
]
update.inline_query.answer(results, cache_time=0)
@@ -99,6 +112,13 @@ class CyberLinaBotModuleConfig(TelegramBotModuleConfig):
dispatcher.add_handler(InlineQueryHandler(self.inline_query_handler))
return dispatcher
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if self.mtg_train:
self.mtg.train_from_text(self.mtg_train)
self._mtg_data = self.mtg.save()
self.mtg_train = None
super().save(force_insert, force_update, using, update_fields)
class CyberLinaChat(models.Model):
config = models.ForeignKey(CyberLinaBotModuleConfig, on_delete=models.CASCADE, related_name='chats')