2016-11-16 20:42:00 +01:00
|
|
|
import random
|
2016-11-29 15:23:06 +01:00
|
|
|
import logging
|
2016-11-22 23:11:15 +01:00
|
|
|
|
|
|
|
from telegram import Update
|
2016-11-12 13:37:14 +01:00
|
|
|
from telegram.ext import Handler
|
|
|
|
|
2016-11-22 23:11:15 +01:00
|
|
|
from src.domain.command import Command
|
2016-12-01 22:01:04 +01:00
|
|
|
from src.entity.pair import Pair
|
2016-11-23 21:54:22 +01:00
|
|
|
from .moderate_command import ModerateCommand
|
2016-11-12 13:37:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CommandHandler(Handler):
|
2016-12-01 22:01:04 +01:00
|
|
|
def __init__(self, chance_manager):
|
2016-11-12 13:37:14 +01:00
|
|
|
super(CommandHandler, self).__init__(self.handle)
|
|
|
|
self.commands = {
|
|
|
|
'start': self.__start_command,
|
|
|
|
'help': self.__help_command,
|
|
|
|
'ping': self.__ping_command,
|
|
|
|
'set_chance': self.__set_chance_command,
|
|
|
|
'get_chance': self.__get_chance_command,
|
2016-11-16 20:42:00 +01:00
|
|
|
'get_stats': self.__get_stats_command,
|
|
|
|
'moderate': self.__moderate_command
|
2016-11-12 13:37:14 +01:00
|
|
|
}
|
2016-12-01 22:01:04 +01:00
|
|
|
self.chance_manager = chance_manager
|
2016-11-16 20:42:00 +01:00
|
|
|
|
2016-11-12 13:37:14 +01:00
|
|
|
def check_update(self, update):
|
|
|
|
if isinstance(update, Update) and update.message:
|
|
|
|
message = update.message
|
|
|
|
|
|
|
|
return message.text and message.text.startswith('/') and Command.parse_name(message) in self.commands
|
|
|
|
else:
|
|
|
|
return False
|
2016-11-16 20:42:00 +01:00
|
|
|
|
2016-11-12 13:37:14 +01:00
|
|
|
def handle_update(self, update, dispatcher):
|
|
|
|
optional_args = self.collect_optional_args(dispatcher, update)
|
|
|
|
|
|
|
|
return self.callback(dispatcher.bot, update, **optional_args)
|
|
|
|
|
|
|
|
def handle(self, bot, update):
|
2016-12-01 22:01:04 +01:00
|
|
|
command = Command(update.message)
|
2016-11-12 13:37:14 +01:00
|
|
|
|
2016-11-12 23:55:48 +01:00
|
|
|
try:
|
2016-11-29 15:23:06 +01:00
|
|
|
self.commands[command.name](bot, command)
|
2016-11-12 13:37:14 +01:00
|
|
|
except (IndexError, ValueError):
|
2016-11-29 15:23:06 +01:00
|
|
|
self.__reply(bot, command, 'Invalid command! Type /help')
|
|
|
|
|
|
|
|
def __reply(self, bot, command, message):
|
|
|
|
logging.debug("[Chat %s %s command] %s: %s" %
|
2016-12-01 22:01:04 +01:00
|
|
|
(command.chat_type,
|
|
|
|
command.chat_id,
|
2016-11-29 15:23:06 +01:00
|
|
|
command.name,
|
|
|
|
message))
|
|
|
|
|
2016-12-01 22:01:04 +01:00
|
|
|
bot.send_message(chat_id=command.chat_id,
|
2016-11-29 15:23:06 +01:00
|
|
|
reply_to_message_id=command.message.message_id,
|
|
|
|
text=message)
|
|
|
|
|
|
|
|
def __start_command(self, bot, command):
|
|
|
|
self.__reply(bot, command, 'Hi! :3')
|
|
|
|
|
|
|
|
def __help_command(self, bot, command):
|
|
|
|
self.__reply(
|
|
|
|
bot,
|
2016-11-12 23:55:48 +01:00
|
|
|
command,
|
2016-11-16 20:42:00 +01:00
|
|
|
"""Add me to your group and let me listen to your chat for a while.
|
2016-11-12 13:37:14 +01:00
|
|
|
When I learn enough word pairs, I'll start bringing fun and absurdity to your conversations.
|
|
|
|
|
|
|
|
Available commands:
|
|
|
|
• /ping,
|
|
|
|
• /get_stats: get the number of word pairs I've learned in this chat,
|
|
|
|
• /set_chance: set the chance that I'll reply to a random message (must be in range 1-50, default: 5),
|
|
|
|
• /get_chance: get the current chance of my random reply.
|
|
|
|
|
|
|
|
If you get tired of me, you can kick me from the group.
|
|
|
|
In 12 hours, I'll forget everything that have been learned in your chat, so you can add me again and teach me new things!
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
def __ping_command(self, bot, command):
|
2016-11-16 20:42:00 +01:00
|
|
|
answers = [
|
|
|
|
'echo',
|
|
|
|
'pong',
|
|
|
|
'ACK',
|
|
|
|
'reply',
|
|
|
|
'pingback'
|
|
|
|
]
|
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
self.__reply(bot, command, random.choice(answers))
|
2016-11-12 13:37:14 +01:00
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
def __set_chance_command(self, bot, command):
|
2016-11-12 13:37:14 +01:00
|
|
|
try:
|
2016-12-01 22:01:04 +01:00
|
|
|
chance = int(command.args[0])
|
2016-11-12 13:37:14 +01:00
|
|
|
|
2016-12-01 22:01:04 +01:00
|
|
|
if chance < 1 or chance > 50:
|
2016-11-12 13:37:14 +01:00
|
|
|
raise ValueError
|
|
|
|
|
2016-12-01 22:01:04 +01:00
|
|
|
self.chance_manager.set_chance(chat_id=command.chat_id, chance=chance)
|
2016-11-12 13:37:14 +01:00
|
|
|
|
2016-12-01 22:01:04 +01:00
|
|
|
self.__reply(bot, command, 'Set chance to: {}'.format(chance))
|
2016-11-12 13:37:14 +01:00
|
|
|
except (IndexError, ValueError):
|
2016-11-29 15:23:06 +01:00
|
|
|
self.__reply(bot, command, 'Usage: /set_chance 1-50.')
|
2016-11-16 20:42:00 +01:00
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
def __get_chance_command(self, bot, command):
|
2016-12-01 22:01:04 +01:00
|
|
|
self.__reply(bot, command, 'Current chance: {}'
|
|
|
|
.format(self.chance_manager.get_chance(command.chat_id)))
|
2016-11-16 20:42:00 +01:00
|
|
|
|
2016-11-29 15:23:06 +01:00
|
|
|
def __get_stats_command(self, bot, command):
|
2016-12-01 22:01:04 +01:00
|
|
|
self.__reply(bot, command, 'Pairs: {}'
|
|
|
|
.format(Pair.where('chat_id', command.chat_id).count()))
|
2016-11-16 20:42:00 +01:00
|
|
|
|
|
|
|
def __moderate_command(self, bot, command):
|
|
|
|
try:
|
2016-11-23 21:54:22 +01:00
|
|
|
moderate = ModerateCommand(bot, command)
|
|
|
|
|
|
|
|
if not moderate.is_admin():
|
2016-11-29 15:23:06 +01:00
|
|
|
return self.__reply(bot, command, 'You don\'t have admin privileges!')
|
2016-11-16 20:42:00 +01:00
|
|
|
|
2016-11-22 23:11:15 +01:00
|
|
|
if len(command.args) == 2:
|
2016-11-23 21:54:22 +01:00
|
|
|
moderate.remove_word(command.args[1])
|
2016-11-16 20:42:00 +01:00
|
|
|
else:
|
2016-11-29 15:23:06 +01:00
|
|
|
self.__reply(bot, command, moderate.find_similar_words(command.args[0]))
|
2016-11-16 20:42:00 +01:00
|
|
|
except (IndexError, ValueError):
|
2016-11-29 15:23:06 +01:00
|
|
|
self.__reply(bot, command, """Usage:
|
2016-11-22 23:11:15 +01:00
|
|
|
/moderate <word> for search
|
|
|
|
/moderate <word> <word_id> for deletion""")
|
2016-11-16 20:42:00 +01:00
|
|
|
|