[WIP] Issue #4

This commit is contained in:
REDNBLACK 2016-11-16 22:42:00 +03:00
parent 058924da3b
commit fa9f8d0654
2 changed files with 79 additions and 17 deletions

View File

@ -1,8 +1,10 @@
from telegram import Update
import random
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Handler
from src.entity.chat import Chat
from src.domain.command import Command
from src.entity.word import Word
class CommandHandler(Handler):
@ -14,10 +16,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 +28,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 +39,17 @@ class CommandHandler(Handler):
command = Command(chat=chat, message=update.message)
try:
self.commands[command.name](command)
self.commands[command.name](bot, command)
except (IndexError, ValueError):
self.message_sender.reply(command, 'Invalid command! Type /help')
def __start_command(self, command):
def __start_command(self, bot, command):
self.message_sender.reply(command, 'Hi! :3')
def __help_command(self, command):
def __help_command(self, bot, 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:
@ -60,10 +63,18 @@ 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')
def __ping_command(self, bot, command):
answers = [
'echo',
'pong',
'ACK',
'reply',
'pingback'
]
def __set_chance_command(self, command):
self.message_sender.reply(command, random.choice(answers))
def __set_chance_command(self, bot, command):
try:
random_chance = int(command.args[0])
@ -75,9 +86,55 @@ 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):
def __get_chance_command(self, bot, command):
self.message_sender.reply(command, 'Current chance: {}'.format(command.chat.random_chance))
def __get_stats_command(self, command):
def __get_stats_command(self, bot, command):
self.message_sender.reply(command, 'Pairs: {}'.format(command.chat.pairs().count()))
def __moderate_command(self, bot, command):
try:
def is_admin():
user_id = command.message.from_user.id
admin_ids = list(map(
lambda member: member.user.id,
bot.get_chat_administrators(chat_id=command.chat.telegram_id)
))
return user_id in admin_ids
def generate_keyboard(words):
custom_keyboard = []
for word in words:
custom_keyboard.append([
InlineKeyboardButton(text=word.word, callback_data='nothing'),
InlineKeyboardButton(text='🔲', callback_data=word.id)
])
custom_keyboard.append([
InlineKeyboardButton(text='Cancel', callback_data='cancel'),
InlineKeyboardButton(text='OK', callback_data='remove_all_marked_words')
])
return InlineKeyboardMarkup(custom_keyboard)
if not is_admin():
return self.message_sender.reply(command, 'You don\'t have admin privileges!')
search_word = command.args[0]
found_words = Word.where('word', 'like', search_word + '%') \
.order_by('word', 'asc') \
.limit(10) \
.get()
if len(found_words) == 0:
self.message_sender.reply(command, 'No words found!')
else:
self.message_sender.send_reply_markup(command,
message="Mark all words to delete and press OK, "
"or press Cancel to close this window",
reply_markup=generate_keyboard(found_words))
except (IndexError, ValueError):
self.message_sender.reply(command, 'Usage: /moderate <word>')

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,