コード例 #1
0
def require_valid_query(query: str) -> None:
    query_mode, rest = get_query_mode(query)
    handler = MODE_HANDLERS.get(query_mode)  # Raise KeyError if not here
    if handler is None:
        raise KeyError(
            f"No entiendo esos parametros: '{query}', {Phrase.get_random_phrase()}."
        )
コード例 #2
0
ファイル: ping.py プロジェクト: JavierLuna/cunhaobot
def _send_chapas(bot: Bot, tasks: Iterable[ScheduledTask]) -> None:
    errors = []
    for task in tasks:
        try:
            query_mode, rest = get_query_mode(task.query)
            resuls_fn = MODE_HANDLERS.get(query_mode)
            result = next(iter(resuls_fn(rest)), None)
            if result is None:
                bot.send_message(
                    task.chat_id,
                    f"No he encontrado nada con los parametros '{task.query}', así que "
                    f"aqui tienes algo parecido, {Phrase.get_random_phrase()}."
                )
                bot.send_message(task.chat_id, LongPhrase.get_random_phrase())
                continue
            if query_mode == AUDIO_MODE:
                bot.send_voice(task.chat_id, result.voice_url)
            else:
                bot.send_message(task.chat_id,
                                 result.input_message_content.message_text)
        except Exception as e:
            logger.exception("Error sending chapa")
            errors.append((task.datastore_id, e, str(e)))

    if errors:
        bot.send_message(
            curators_chat_id,
            f"{Phrase.get_random_phrase()}s, mandando chapas he tenido estos errores: {errors}."
        )
コード例 #3
0
    def test_empty(self):
        query = ''
        expected = SHORT_MODE, ''

        actual = get_query_mode(query)

        self.assertEqual(actual, expected)
コード例 #4
0
    def test_special_characters(self):
        query = 'wtf!!'
        expected = '', ''

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #5
0
    def test_audio_word(self):
        query = f'{AUDIO_MODE_WORDS[0]} whatever this is not defined yet'
        expected = AUDIO_MODE, 'whatever this is not defined yet'

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #6
0
    def test_text(self):
        query = 'something'
        expected = LONG_MODE, query

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #7
0
    def test_long_word(self):
        query = f'{LONG_MODE_WORDS[0]} whatever this is not defined yet'
        expected = LONG_MODE, 'whatever this is not defined yet'

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #8
0
    def test_short_word_number_and_finisher(self):
        query = f'{SHORT_MODE_WORDS[0]} 10 this is a test'
        expected = SHORT_MODE, '10 this is a test'

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #9
0
    def test_number_and_finisher(self):
        query = '10 this is a test'
        expected = SHORT_MODE, '10 this is a test'

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #10
0
    def test_only_number_and_whitespace(self):
        query = '  1  '
        expected = SHORT_MODE, '1'

        actual = get_query_mode(query)

        self.assertEqual(expected, actual)
コード例 #11
0
    def test_only_number(self):
        query = '1'
        expected = SHORT_MODE, '1'

        actual = get_query_mode(query)

        self.assertEqual(actual, expected)