2016-11-10 15:21:08 +01:00
|
|
|
import random
|
2016-11-14 20:03:19 +01:00
|
|
|
import re
|
2016-11-12 23:55:48 +01:00
|
|
|
from .abstract_entity import AbstractEntity
|
2016-11-10 15:21:08 +01:00
|
|
|
from src.utils import deep_get_attr
|
2016-11-12 16:36:23 +01:00
|
|
|
from src.config import config
|
2016-11-10 15:21:08 +01:00
|
|
|
|
|
|
|
|
2016-11-12 23:55:48 +01:00
|
|
|
class Message(AbstractEntity):
|
2016-12-01 22:01:04 +01:00
|
|
|
def __init__(self, chance, message):
|
|
|
|
super(Message, self).__init__(message)
|
|
|
|
|
|
|
|
self.chance = chance
|
2016-11-10 15:21:08 +01:00
|
|
|
|
|
|
|
if self.has_text():
|
|
|
|
self.text = message.text
|
|
|
|
else:
|
|
|
|
self.text = ''
|
|
|
|
|
|
|
|
def has_text(self):
|
|
|
|
"""Returns True if the message has text.
|
|
|
|
"""
|
2016-11-12 18:57:37 +01:00
|
|
|
return self.message.text.strip() != ''
|
2016-11-10 15:21:08 +01:00
|
|
|
|
|
|
|
def is_sticker(self):
|
|
|
|
"""Returns True if the message is a sticker.
|
|
|
|
"""
|
|
|
|
return self.message.sticker is not None
|
|
|
|
|
|
|
|
def has_entities(self):
|
|
|
|
"""Returns True if the message has entities (attachments).
|
|
|
|
"""
|
|
|
|
return self.message.entities is not None
|
|
|
|
|
|
|
|
def has_anchors(self):
|
|
|
|
"""Returns True if the message contains at least one anchor from anchors config.
|
|
|
|
"""
|
2016-11-27 16:34:21 +01:00
|
|
|
anchors = config.getlist('bot', 'anchors')
|
2016-11-12 19:31:40 +01:00
|
|
|
return self.has_text() and any(a in self.message.text.split(' ') for a in anchors)
|
2016-11-10 15:21:08 +01:00
|
|
|
|
|
|
|
def is_reply_to_bot(self):
|
|
|
|
"""Returns True if the message is a reply to bot.
|
|
|
|
"""
|
|
|
|
user_name = deep_get_attr(self.message, 'reply_to_message.from_user.username')
|
|
|
|
|
2016-11-12 14:56:42 +01:00
|
|
|
return user_name == config['bot']['name']
|
2016-11-10 15:21:08 +01:00
|
|
|
|
|
|
|
def is_random_answer(self):
|
|
|
|
"""Returns True if reply chance for this chat is high enough
|
|
|
|
"""
|
2016-12-01 22:01:04 +01:00
|
|
|
return random.randint(0, 100) < self.chance
|
2016-11-10 15:21:08 +01:00
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
def should_answer(self):
|
|
|
|
return self.has_anchors() \
|
|
|
|
or self.is_private() \
|
|
|
|
or self.is_reply_to_bot() \
|
|
|
|
or self.is_random_answer()
|