def test_invalid_command(self): message = MagicMock(reply_to_message=None, text="reply_foo") update = MagicMock(callback_query=None, message=message) assert validate_components(update) == ( f'Invalid command: "reply_foo".\nCommands start with "/".', "send-message", )
def test_unknown_command_not_whitelisted(self): message = MagicMock(reply_to_message=None, text="/command_foo 123 4") update = MagicMock(callback_query=None, message=message) update.message.chat.type = "private" assert validate_components(update) == ( f'Unknown command: "command_foo".\n' f"Available commands: \n• /start", 400, )
def test_photo(self): update = MagicMock( callback_query=None, message=MagicMock( text=None, left_chat_member=None, new_chat_title=None, new_chat_members=None, ), ) assert validate_components(update) == ("skip-debug", 1337)
def test_unknown_command_whitelisted(self): message = MagicMock(reply_to_message=None, text="/command_foo 123 4") update = MagicMock(callback_query=None, message=message) update.message.chat.type = "private" update.message.chat_id = constants.GOOGLE_CLOUD_WHITELIST["private"][0] assert validate_components(update) == ( 'Unknown command: "command_foo".\n' "Available commands: \n• /analyze_sentiment\n• " "/start\n• /translate\n• /games\n• /randomize", 400, )
def test_new_chat_members(self): update = MagicMock( callback_query=None, message=MagicMock( text=None, left_chat_member=None, new_chat_title=None, new_chat_members=["foo", "bar"], ), ) with mock.patch("core.handlers.parse_name") as parse_mock: parse_mock.side_effect = update.message.new_chat_members assert validate_components(update) == ("Welcome foo, bar!", 400)
def test_no_message_text_unhandled(self): update = MagicMock( callback_query=None, message=MagicMock( text=None, left_chat_member=None, new_chat_title=None, new_chat_members=None, new_chat_photo=None, group_chat_created=None, supergroup_chat_created=None, channel_chat_created=None, pinned_message=None, photo=None, document=None, animation=None, ), ) with pytest.raises(ValueError) as e: validate_components(update) assert ( e.value.args[0] == f"No message text. Update: {update.to_dict()}")
def test_reply_to_message(self): update = MagicMock(callback_query=None, message=MagicMock(text="reply_foo")) assert validate_components(update) == ("skip-debug", 1337)
def test_new_chat_title(self): update = MagicMock( callback_query=None, message=MagicMock(text=None, left_chat_member=None), ) assert validate_components(update) == ("🎉", 400)
def test_left_chat_member(self): update = MagicMock(callback_query=None) update.message.text = None assert validate_components(update) == ("😢", 400)
def test_channel_post(self): update = MagicMock(callback_query=None, message=None, edited_message=None) assert validate_components(update) == ("skip-debug", 1337)
def test_return_actual_command(self, command): message = MagicMock(reply_to_message=None, text=f"/{command}") update = MagicMock(callback_query=None, message=message) assert validate_components(update) == (command, "valid-command")
def test_callback_query(self): update = MagicMock() assert validate_components(update) == ( "inline", update.callback_query.data, )
def webhook(): json = request.get_json() if not json: raise ValueError("No payload") bot = telegram.Bot(token=TOKEN) update = telegram.Update.de_json(json, bot) command_text, status_code = handlers.validate_components(update) if command_text == "inline": chat_id = update.callback_query.message.chat_id if status_code in ["more", "back", "end"]: return getattr(inline, status_code)(update) if status_code.startswith("games_"): status_code = status_code.replace("games_", "") return inline.refresh_data(update, Games(chat_id, status_code).get()) return inline.refresh_data(update, getattr(local_data, status_code)()) if status_code == 1337: if command_text == "skip-debug": return "ok" text = f"{command_text}.\nUpdate: {update.to_dict()}" return utils.send_message(bot, text=text) chat_id = update.message.chat.id if status_code != "valid-command": return utils.send_message(bot, text=command_text, chat_id=chat_id) if command_text == "start": return inline.start(update) if command_text == "games" and not update.message.text.split(" ")[1:]: return inline.start(update, games=True) if command_text in GOOGLE_CLOUD_COMMANDS: chat_type = update.message.chat.type if str(chat_id) not in GOOGLE_CLOUD_WHITELIST[chat_type]: return utils.send_message(bot, "Unauthorized", chat_id) arg = " ".join(update.message.text.split(" ")[1:]) if command_text == "translate": return utils.send_message(bot, translate_text(arg), chat_id) if command_text == "analyze_sentiment": return utils.send_message(bot, analyze_sentiment(arg), chat_id) if command_text in GAME_COMMANDS: chat_type = update.message.chat.type if str(chat_id) not in GOOGLE_CLOUD_WHITELIST[chat_type]: return utils.send_message(bot, "Unauthorized", chat_id) if command_text == "games": args = update.message.text.split(" ")[1:] if len(args) not in (2, 3) or len(args) == 2 and args[1] != "new": return utils.send_message( bot, parse_global( title="Syntax", stats=[ "/games => scores", "/games <game_name> new => new game", "/games <game_name> new_player <player_name>" " => new player", "/games <game_name> + <player_name>" " => increase <player_name>'s score by 1", "/games <game_name> - <player_name>" " => decrease <player_name>'s score by 1", ], items={}, ), chat_id, ) name, *args = args games = Games(chat_id, name) if len(args) == 1: return utils.send_message(bot, games.new_game(), chat_id) return utils.send_message(bot, games.update(*args), chat_id) if command_text == "randomize": args = update.message.text.split(" ")[1:] if len(args) not in range(2, 51): return utils.send_message( bot, "Must contain a list of 2-50 items separated by space", chat_id, ) random.shuffle(args) return utils.send_message( bot, "\n".join(f"{i+1}. {item}" for i, item in enumerate(args)), chat_id, ) raise ValueError(f"Unhandled command: {command_text}, {status_code}")