def test_len(self): command_cache = CommandCache(30) command_cache.set(Command("/listaa hae"), "") command_cache.set(Command("/listaa hae2"), "") command_cache.set(Command("/listaa hae3"), "") command_cache.set(Command("/listaa hae3"), "") self.assertEquals(len(command_cache), 3)
def processing(): try: event = json.loads(request.data) if event['type'] == 'confirmation': return CONFIG.confirmation command = Command(VkBotEvent(event)) except Exception as e: errors.error(str(e)) abort(400) else: command.send_msg() return 'ok'
def on_conference_message(self, group_id, peer_id, type, message): author_name = "[%s]: " % self.conference_peer_get_name( group_id, peer_id) log_message = author_name + message group_name = self.conference_get_title(group_id) self.db.log_message(group_name, log_message) is_message_from_chatbot = self.conference_peer_number_is_ours( group_id, peer_id) if self.config[ "enable_irc_sync_feature"] and not is_message_from_chatbot: irc_channel = self.irc.get_bridged_irc_channel(group_name) if irc_channel != "": self.irc.send_message(irc_channel, log_message) response = Command.parse_channel(self, message, group_name) if response != "": response_messages = self.split_message(response) try: for response_message in response_messages: self.conference_send_message( group_id, 0, response_message.encode("utf-8")) except: print("ERROR: couldn't send a group message")
def test_cache_cleaning(self): command1 = Command("/listaa hae") command2 = Command("/listaa hae2") command_cache = CommandCache(1) command_cache.set(command1, "") command_cache.set(command2, "") self.assertEquals(len(command_cache), 2) time.sleep(1.5) command_cache.is_command_cached(command1) self.assertEquals(len(command_cache), 1) command_cache.is_command_cached(command2) self.assertEquals(len(command_cache), 0)
def test_parsing_with_two_params(self): command = Command("/listaa liha kauppa") self.assertEqual(len(command.params), 2) self.assertEqual(command.params[0], "liha") self.assertEqual(command.params[1], "kauppa") self.assertTrue(isinstance(command.params, list)) self.assertTrue(isinstance(command.command, str))
def on_friend_message(self, friend_id, message_type, message): response = Command.parse(self, message, friend_id) if response != "": response_messages = self.split_message(response) try: for response_message in response_messages: self.friend_send_message(friend_id, message_type, response_message) except: print("ERROR: couldn't send message to a friend")
def test_is_valid_command(self): self.assertTrue(Command.is_valid("/listaa")) self.assertTrue(Command.is_valid("/listaa haku")) self.assertTrue(Command.is_valid("/listaa haku paku")) self.assertTrue(Command.is_valid("/listaa 2")) self.assertFalse(Command.is_valid("listaa 2")) self.assertFalse(Command.is_valid("/"))
def handle_updates(self, updates): """Handle updates coming from the Telegram API Arguments: updates {dict} -- Update dictionary coming from Telegram API """ for update in updates["result"]: try: text = update["message"]["text"] chat = update["message"]["chat"]["id"] if not Command.is_valid(text): continue command = Command(text) if command.command in ['/list', '/listaa', '/reseptit']: titles = self.list_receipts(command) self.send_message("Reseptit\n" + str(titles), chat) elif command.command in ['/about', '/minusta']: self.send_message(self.aboutTmp, chat, parse_mode=ParseMode.MARKDOWN) elif text in ['/help', '/?', '/ohje']: self.send_message(self.ohjeTmp, chat, parse_mode=ParseMode.MARKDOWN) elif "/get" in text or "/hae" in text: receipt = self.get_receipt(text) self.send_message(receipt, chat) elif text == "/start": self.send_message(self.welcomeTmp, chat, parse_mode=ParseMode.MARKDOWN) else: self.send_message('meep', chat) except KeyError: continue
del logfile # GOAL: # Localization from bot.locals.default import LOCAL # GOAL: # load Command format from bot.command import Command COMMAND = Command({ 'START' : 'start', 'PASSWORD' : 'pass', 'HELP' : 'help', 'LEECH' : 'leech', 'CANCEL_LEECH' : 'cancel', 'LEECH_LIST' : 'list' }) # GOAL: # set status from time import time from bot.status import Status STATUS = Status({ 'START_TIME' : time(), 'ARIA2_API' : None })
# Localization LOCAL = __import__(name='bot.locals.' + CONFIG.LOCAL, fromlist=['LOCAL']).LOCAL # GOAL: # load Command format from bot.command import Command COMMAND = Command( { 'START': 'start', 'PASSWORD': '******', 'HELP': 'help', 'LEECH': 'leech', 'CANCEL_LEECH': 'cancel', 'LEECH_LIST': 'list', 'UPLOAD_AS_DOC': 'upload_as_doc', 'UPLOAD_AS_ZIP': 'upload_as_zip', 'SET_THUMBNAIL': 'set_thumbnail', 'RESET_THUMBNAIL': 'reset_thumbnail', 'SET_TRACKER': 'set_tracker' }, 'COMMAND_') # GOAL: # set status from time import time from bot.status import Status STATUS = Status({ 'START_TIME': time(),
def test_equality(self): command = Command("/listaa haku") command2 = Command("/listaa haku") self.assertEqual(command, command2)
def test_str(self): self.assertEqual(str(Command('/listaa liha')), "Command: /listaa - Params: ['liha']")
def test_not_command(self): command = Command("listaa") self.assertEqual(len(command.params), 0) self.assertTrue(isinstance(command.params, list)) self.assertTrue(isinstance(command.command, str))
def test_parsing_with_zero_params(self): command = Command("/listaa") self.assertEqual(len(command.params), 0) self.assertTrue(isinstance(command.params, list)) self.assertTrue(isinstance(command.command, str))
def register_command(self, command: Command): self.commands[command.get_name()] = command