def test_reply(self, mocked_send_message: Mock): mocked_send_message.return_value = TelegramMessage.from_dict({ 'message_id': 42, 'from': {'id': 142, 'is_bot': True, 'first_name': 'bot_name', 'username': '******'}, 'chat': {'id': 42, 'type': 'private'}, 'date': 1, 'text': 'text'}) self.chat.reply( text="Reply" ) mocked_send_message.assert_called_with( chat_id=self.chat.chat_id, text="Reply", parse_mode=None, ) user = User.objects.first() message = Message.objects.first() self.assertEqual(message.chat, self.chat) self.assertEqual(message.from_user, user)
def test_reply(self, mocked_send_message: Mock): mocked_send_message.return_value = TelegramMessage.from_dict({ "message_id": 42, "from": { "id": 142, "is_bot": True, "first_name": "bot_name", "username": "******", }, "chat": { "id": 42, "type": "private" }, "date": 1, "text": "text", }) self.chat.reply(text="Reply") mocked_send_message.assert_called_with( chat_id=self.chat.chat_id, text="Reply", parse_mode=None, ) user = User.objects.first() message = Message.objects.first() self.assertEqual(message.chat, self.chat) self.assertEqual(message.from_user, user)
def test_message_with_text(self): source = { "update_id": 10000, "message": { "date": 1441645532, "chat": { "last_name": "Test Lastname", "id": 1111111, "type": "private", "first_name": "Test Firstname", "username": "******", }, "message_id": 1365, "from": { "last_name": "Test Lastname", "is_bot": False, "id": 1111111, "first_name": "Test Firstname", "username": "******", }, "text": "/start", }, } update = Update.from_dict(source=source) self.assertEqual(update.update_id, 10000) self.assertEqual( update, Update( update_id=10000, message=Message( message_id=1365, text="/start", date=timezone.datetime(2015, 9, 7, 17, 5, 32, tzinfo=timezone.utc), chat=Chat( id=1111111, type="private", username="******", first_name="Test Firstname", last_name="Test Lastname", ), from_user=User( id=1111111, is_bot=False, username="******", first_name="Test Firstname", last_name="Test Lastname", ), ), ), )
def test_edited_channel_post(self): source = { "update_id": 10000, "edited_channel_post": { "message_id": 16, "sender_chat": { "id": -1001, "title": "test_channel", "type": "channel", }, "chat": { "id": -1001, "title": "test_channel", "type": "channel" }, "date": 1615492954, "edit_date": 1615493064, "text": "post3", }, } update = Update.from_dict(source=source) self.assertEqual(update.update_id, 10000) self.assertEqual( update, Update( update_id=10000, edited_channel_post=Message( message_id=16, text="post3", date=timezone.datetime(2021, 3, 11, 20, 2, 34, tzinfo=timezone.utc), edit_date=timezone.datetime(2021, 3, 11, 20, 4, 24, tzinfo=timezone.utc), chat=Chat( id=-1001, type="channel", title="test_channel", ), sender_chat=Chat( id=-1001, type="channel", title="test_channel", ), ), ), )
def test_edited_channel_post(self): source = {'update_id': 10000, 'edited_channel_post': { 'message_id': 16, 'sender_chat': {'id': -1001, 'title': 'test_channel', 'type': 'channel'}, 'chat': {'id': -1001, 'title': 'test_channel', 'type': 'channel'}, 'date': 1615492954, 'edit_date': 1615493064, 'text': 'post3' }} update = Update.from_dict(source=source) self.assertEqual(update.update_id, 10000) self.assertEqual( update, Update( update_id=10000, edited_channel_post=Message( message_id=16, text="post3", date=timezone.datetime( 2021, 3, 11, 20, 2, 34, tzinfo=timezone.utc ), edit_date=timezone.datetime( 2021, 3, 11, 20, 4, 24, tzinfo=timezone.utc ), chat=Chat( id=-1001, type="channel", title="test_channel", ), sender_chat=Chat( id=-1001, type="channel", title="test_channel", ), ) ) )
def from_telegram(self, bot: Bot, telegram_message: types.Message, direction: str): """Create a model instance from a telegram type instance. Args: bot: The bot the message belongs to. telegram_message: The telegram Message. direction: The message direction. Either Message.DIRECTION_IN or Either Message.DIRECTION_OUT for incoming/outgoing message. Returns: model instance. """ defaults = telegram_message.to_dict() defaults["direction"] = direction _update_defaults(telegram_message, defaults, "entities") _update_defaults(telegram_message, defaults, "animation") _update_defaults(telegram_message, defaults, "audio") _update_defaults(telegram_message, defaults, "document") _update_defaults(telegram_message, defaults, "photo") _update_defaults(telegram_message, defaults, "sticker") _update_defaults(telegram_message, defaults, "video") _update_defaults(telegram_message, defaults, "video_note") _update_defaults(telegram_message, defaults, "voice") _update_defaults(telegram_message, defaults, "caption_entities") _update_defaults(telegram_message, defaults, "contact") _update_defaults(telegram_message, defaults, "dice") _update_defaults(telegram_message, defaults, "game") _update_defaults(telegram_message, defaults, "poll") _update_defaults(telegram_message, defaults, "venue") _update_defaults(telegram_message, defaults, "location") _update_defaults(telegram_message, defaults, "new_chat_photo") _update_defaults(telegram_message, defaults, "invoice") _update_defaults(telegram_message, defaults, "successful_payment") _update_defaults(telegram_message, defaults, "passport_data") _update_defaults(telegram_message, defaults, "proximity_alert_triggered") _update_defaults(telegram_message, defaults, "reply_markup") chat = Chat.objects.from_telegram(bot=bot, telegram_chat=telegram_message.chat) defaults["chat"] = chat if telegram_message.from_user: user = User.objects.from_telegram(telegram_message.from_user) defaults["from_user"] = user if telegram_message.reply_to_message: defaults["reply_to_message"] = self.get_message( telegram_message.reply_to_message ) if telegram_message.left_chat_member: user = User.objects.from_telegram(telegram_message.left_chat_member) defaults["left_chat_member"] = user if telegram_message.new_chat_members: defaults.pop("new_chat_members") if telegram_message.sender_chat: sender_chat = Chat.objects.from_telegram( bot=bot, telegram_chat=telegram_message.sender_chat ) defaults["sender_chat"] = sender_chat defaults.pop("message_id") message, created = self.update_or_create( message_id=telegram_message.message_id, defaults=defaults ) if created and telegram_message.new_chat_members: members = [ User.objects.from_telegram(telegram_user) for telegram_user in telegram_message.new_chat_members ] for member in members: message.new_chat_members.add(member) return message
def from_telegram(self, bot: Bot, telegram_message: types.Message, direction: str): """Create a model instance from a telegram type instance. Args: bot: The bot the message belongs to. telegram_message: The telegram Message. direction: The message direction. Either Message.DIRECTION_IN or Either Message.DIRECTION_OUT for incoming/outgoing message. Returns: model instance. """ defaults = telegram_message.to_dict() defaults['direction'] = direction _update_defaults(telegram_message, defaults, 'entities') _update_defaults(telegram_message, defaults, 'animation') _update_defaults(telegram_message, defaults, 'audio') _update_defaults(telegram_message, defaults, 'document') _update_defaults(telegram_message, defaults, 'photo') _update_defaults(telegram_message, defaults, 'sticker') _update_defaults(telegram_message, defaults, 'video') _update_defaults(telegram_message, defaults, 'video_note') _update_defaults(telegram_message, defaults, 'voice') _update_defaults(telegram_message, defaults, 'caption_entities') _update_defaults(telegram_message, defaults, 'contact') _update_defaults(telegram_message, defaults, 'dice') _update_defaults(telegram_message, defaults, 'game') _update_defaults(telegram_message, defaults, 'poll') _update_defaults(telegram_message, defaults, 'venue') _update_defaults(telegram_message, defaults, 'location') _update_defaults(telegram_message, defaults, 'new_chat_photo') _update_defaults(telegram_message, defaults, 'invoice') _update_defaults(telegram_message, defaults, 'successful_payment') _update_defaults(telegram_message, defaults, 'passport_data') _update_defaults(telegram_message, defaults, 'proximity_alert_triggered') _update_defaults(telegram_message, defaults, 'reply_markup') chat = Chat.objects.from_telegram(bot=bot, telegram_chat=telegram_message.chat) defaults['chat'] = chat if telegram_message.from_user: user = User.objects.from_telegram(telegram_message.from_user) defaults['from_user'] = user if telegram_message.reply_to_message: defaults['reply_to_message'] = self.get_message( telegram_message.reply_to_message) if telegram_message.left_chat_member: user = User.objects.from_telegram( telegram_message.left_chat_member) defaults['left_chat_member'] = user if telegram_message.new_chat_members: defaults.pop('new_chat_members') if telegram_message.sender_chat: sender_chat = Chat.objects.from_telegram( bot=bot, telegram_chat=telegram_message.sender_chat) defaults['sender_chat'] = sender_chat defaults.pop('message_id') message, created = self.update_or_create( message_id=telegram_message.message_id, defaults=defaults) if created and telegram_message.new_chat_members: members = [ User.objects.from_telegram(telegram_user) for telegram_user in telegram_message.new_chat_members ] for member in members: message.new_chat_members.add(member) return message