Merge branch 'issue#4'

This commit is contained in:
REDNBLACK 2016-11-23 23:58:41 +03:00
commit d85b362f7a
3 changed files with 115 additions and 11 deletions

View File

@ -1,8 +1,11 @@
import random
from telegram import Update
from telegram.ext import Handler
from src.entity.chat import Chat
from src.domain.command import Command
from src.entity.chat import Chat
from .moderate_command import ModerateCommand
class CommandHandler(Handler):
@ -14,10 +17,11 @@ class CommandHandler(Handler):
'ping': self.__ping_command,
'set_chance': self.__set_chance_command,
'get_chance': self.__get_chance_command,
'get_stats': self.__get_stats_command
'get_stats': self.__get_stats_command,
'moderate': self.__moderate_command
}
self.message_sender = message_sender
def check_update(self, update):
if isinstance(update, Update) and update.message:
message = update.message
@ -25,7 +29,7 @@ class CommandHandler(Handler):
return message.text and message.text.startswith('/') and Command.parse_name(message) in self.commands
else:
return False
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher, update)
@ -36,17 +40,20 @@ class CommandHandler(Handler):
command = Command(chat=chat, message=update.message)
try:
self.commands[command.name](command)
if command.name == 'moderate':
self.commands['moderate'](bot, command)
else:
self.commands[command.name](command)
except (IndexError, ValueError):
self.message_sender.reply(command, 'Invalid command! Type /help')
def __start_command(self, command):
self.message_sender.reply(command, 'Hi! :3')
def __help_command(self, command):
self.message_sender.reply(
command,
"""Add me to your group and let me listen to your chat for a while.
"""Add me to your group and let me listen to your chat for a while.
When I learn enough word pairs, I'll start bringing fun and absurdity to your conversations.
Available commands:
@ -61,7 +68,15 @@ In 12 hours, I'll forget everything that have been learned in your chat, so you
)
def __ping_command(self, command):
self.message_sender.reply(command, 'pong')
answers = [
'echo',
'pong',
'ACK',
'reply',
'pingback'
]
self.message_sender.reply(command, random.choice(answers))
def __set_chance_command(self, command):
try:
@ -75,9 +90,26 @@ In 12 hours, I'll forget everything that have been learned in your chat, so you
self.message_sender.reply(command, 'Set chance to: {}'.format(random_chance))
except (IndexError, ValueError):
self.message_sender.reply(command, 'Usage: /set_chance 1-50.')
def __get_chance_command(self, command):
self.message_sender.reply(command, 'Current chance: {}'.format(command.chat.random_chance))
def __get_stats_command(self, command):
self.message_sender.reply(command, 'Pairs: {}'.format(command.chat.pairs().count()))
def __moderate_command(self, bot, command):
try:
moderate = ModerateCommand(bot, command)
if not moderate.is_admin():
return self.message_sender.reply(command, 'You don\'t have admin privileges!')
if len(command.args) == 2:
moderate.remove_word(command.args[1])
else:
self.message_sender.reply(command, moderate.find_similar_words(command.args[0]))
except (IndexError, ValueError):
self.message_sender.reply(command, """Usage:
/moderate <word> for search
/moderate <word> <word_id> for deletion""")

View File

@ -0,0 +1,67 @@
from src.entity.pair import Pair
from src.entity.reply import Reply
from src.entity.word import Word
class ModerateCommand:
def __init__(self, bot, entity):
self.bot = bot
self.entity = entity
def __formatted_view(self, words):
list = []
for k, v in words.items():
list.append("- %s : %d" % (v, k))
return '\n'.join(list)
def __find_pairs(self, chat_id, word_ids):
return Pair.where('chat_id', chat_id) \
.where(
Pair.query().where_in('first_id', word_ids)
.or_where_in('second_id', word_ids)
) \
.get()
def __find_current_chat_words(self, search_word):
found_words = Word.where('word', 'like', search_word + '%') \
.order_by('word', 'asc') \
.limit(10) \
.lists('word', 'id')
if len(found_words) == 0:
return []
to_keep = []
for pair in self.__find_pairs(self.entity.chat.id, list(found_words.keys())):
if pair.first_id in found_words:
to_keep.append(pair.first_id)
if pair.second_id in found_words:
to_keep.append(pair.second_id)
to_keep = set(to_keep)
return dict((k, found_words[k]) for k in found_words if k in to_keep)
def is_admin(self):
user_id = self.entity.message.from_user.id
admin_ids = list(map(
lambda member: member.user.id,
self.bot.get_chat_administrators(chat_id=self.entity.chat.telegram_id)
))
return user_id in admin_ids
def remove_word(self, word_id):
pairs_ids = self.__find_pairs(self.entity.chat.id, [word_id]).lists('id')
Pair.where_in('id', pairs_ids).delete()
Reply.where_in('pair_id', pairs_ids).delete()
def find_similar_words(self, word):
found_words = self.__find_current_chat_words(word)
if len(found_words) == 0:
return 'No words found!'
return self.__formatted_view(found_words)

View File

@ -29,6 +29,11 @@ class MessageSender:
reply_to_message_id=entity.message.message_id,
text=message)
def send_reply_markup(self, entity, message, reply_markup):
self.bot.send_message(chat_id=entity.chat.telegram_id,
text=message,
reply_markup=reply_markup)
def send_action(self, entity, action):
logging.debug("[Chat %s %s send_action] %s" %
(entity.chat.chat_type,