small updates

readability/code style mostly
This commit is contained in:
Katya Demidova 2016-11-06 21:08:36 +03:00
parent 01c79c8f6b
commit 31076224f8
3 changed files with 42 additions and 18 deletions

View File

@ -10,11 +10,7 @@ from src.message import Message
class Bot:
# TODO Move and use
# messages = [
# 'Я кот, нееште меня!',
# 'Не обижайте пиздюка!',
# 'Ты няшка :3',
# 'Всем по котейке!',
# 'Юра, го бильярд',
# 'Бога нет.',
# ]

View File

@ -15,7 +15,9 @@ class Word(Model):
@staticmethod
def learn(words):
existing_words = Word.where_in('word', words).get().pluck('word').all()
new_words = list(set([word for word in words if word not in existing_words]))
new_words = {word for word in words}.difference(existing_words)
if len(new_words):
for word in new_words:
Word.create(word=word)

View File

@ -7,17 +7,19 @@ import src.domain.pair as pair
class Message:
def __init__(self, bot, message, config):
self.bot = bot
self.message = message
self.config = config
self.chat = chat.Chat.get_chat(message)
self.bot = bot
self.message = message
self.config = config
self.chat = chat.Chat.get_chat(message)
self.chat.migrate_to_chat_id = message.migrate_to_chat_id
if self.has_text():
logging.debug("[chat %s %s bare_text] %s" %
(self.chat.chat_type, self.chat.telegram_id, self.message.text))
self.text = message.text
self.words = self.__get_words()
(self.chat.chat_type,
self.chat.telegram_id,
self.message.text))
self.text = message.text
self.words = self.__get_words()
self.command = self.__get_command() if self.text[0] == '/' else None
def process(self):
@ -29,25 +31,36 @@ class Message:
return self.__process_message()
def has_text(self):
"""Returns True if the message has text.
"""
return self.message.text != ''
def is_editing(self):
"""Returns True if the message was edited.
"""
return self.message.edit_date 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):
anchors = self.config['bot']['anchors'].split(',')
return self.has_text() and \
(any(x in self.message.text.split(' ') for x in self.config['bot']['anchors'].split(',')))
(any(x in self.message.text.split(' ') for x in anchors))
def is_private(self):
"""Returns True if the message is private.
"""
return self.message.chat.type == 'private'
def is_reply_to_bot(self):
"""Returns True if the message is a reply to bot.
"""
reply_to_message = getattr(self.message, 'reply_to_message', None)
from_user = getattr(reply_to_message, 'from_user', None)
user_name = getattr(from_user, 'username', None)
from_user = getattr(reply_to_message, 'from_user', None)
user_name = getattr(from_user, 'username', None)
return user_name == self.config['bot']['name']
@ -55,16 +68,24 @@ class Message:
return random.randint(0, 100) < getattr(self.chat, 'random_chance', 5)
def is_command(self):
"""Returns True if the message is a command (`/start`, `/do_stuff`).
"""
return self.command is not None
def __answer(self, message):
logging.debug("[Chat %s %s answer] %s" %
(self.chat.chat_type, self.chat.telegram_id, message))
(self.chat.chat_type,
self.chat.telegram_id,
message))
self.bot.sendMessage(chat_id=self.chat.telegram_id, text=message)
def __reply(self, message):
logging.debug("[Chat %s %s reply] %s" %
(self.chat.chat_type, self.chat.telegram_id, message))
(self.chat.chat_type,
self.chat.telegram_id,
message))
self.bot.sendMessage(chat_id=self.chat.telegram_id,
reply_to_message_id=self.message.message_id,
text=message)
@ -82,11 +103,16 @@ class Message:
def __get_words(self):
text = list(self.text)
for entity in self.message.entities:
text[entity.offset:entity.length] = ' ' * entity.length
result = list(filter(None, map(lambda x: x.lower(), ''.join(text).split(' '))))
logging.debug("[chat %s %s get_words] %s" %
(self.chat.chat_type, self.chat.telegram_id, result))
(self.chat.chat_type,
self.chat.telegram_id,
result))
return result