Ejemplo n.º 1
0
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['entities'] = MessageEntity.de_list(data.get('entities'))
        data['forward_from'] = User.de_json(data.get('forward_from'))
        data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'))
        data['forward_date'] = Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = Message.de_json(data.get('reply_to_message'))
        data['edit_date'] = Message._fromtimestamp(data.get('edit_date'))
        data['audio'] = Audio.de_json(data.get('audio'))
        data['document'] = Document.de_json(data.get('document'))
        data['photo'] = PhotoSize.de_list(data.get('photo'))
        data['sticker'] = Sticker.de_json(data.get('sticker'))
        data['video'] = Video.de_json(data.get('video'))
        data['voice'] = Voice.de_json(data.get('voice'))
        data['contact'] = Contact.de_json(data.get('contact'))
        data['location'] = Location.de_json(data.get('location'))
        data['venue'] = Venue.de_json(data.get('venue'))
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'))
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'))
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'))
        data['pinned_message'] = Message.de_json(data.get('pinned_message'))

        return Message(**data)
Ejemplo n.º 2
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Message, cls).de_json(data, bot)

        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['entities'] = MessageEntity.de_list(data.get('entities'), bot)
        data['forward_from'] = User.de_json(data.get('forward_from'), bot)
        data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'), bot)
        data['forward_date'] = from_timestamp(data.get('forward_date'))
        data['reply_to_message'] = Message.de_json(data.get('reply_to_message'), bot)
        data['edit_date'] = from_timestamp(data.get('edit_date'))
        data['audio'] = Audio.de_json(data.get('audio'), bot)
        data['document'] = Document.de_json(data.get('document'), bot)
        data['game'] = Game.de_json(data.get('game'), bot)
        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['sticker'] = Sticker.de_json(data.get('sticker'), bot)
        data['video'] = Video.de_json(data.get('video'), bot)
        data['voice'] = Voice.de_json(data.get('voice'), bot)
        data['video_note'] = VideoNote.de_json(data.get('video_note'), bot)
        data['contact'] = Contact.de_json(data.get('contact'), bot)
        data['location'] = Location.de_json(data.get('location'), bot)
        data['venue'] = Venue.de_json(data.get('venue'), bot)
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'), bot)
        data['new_chat_members'] = User.de_list(data.get('new_chat_members'), bot)
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'), bot)
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'), bot)
        data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)
        data['invoice'] = Invoice.de_json(data.get('invoice'), bot)
        data['successful_payment'] = SuccessfulPayment.de_json(data.get('successful_payment'), bot)

        return cls(bot=bot, **data)
Ejemplo n.º 3
0
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['entities'] = MessageEntity.de_list(data.get('entities'))
        data['forward_from'] = User.de_json(data.get('forward_from'))
        data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'))
        data['forward_date'] = Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = Message.de_json(
            data.get('reply_to_message'))
        data['audio'] = Audio.de_json(data.get('audio'))
        data['document'] = Document.de_json(data.get('document'))
        data['photo'] = PhotoSize.de_list(data.get('photo'))
        data['sticker'] = Sticker.de_json(data.get('sticker'))
        data['video'] = Video.de_json(data.get('video'))
        data['voice'] = Voice.de_json(data.get('voice'))
        data['contact'] = Contact.de_json(data.get('contact'))
        data['location'] = Location.de_json(data.get('location'))
        data['venue'] = Venue.de_json(data.get('venue'))
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'))
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'))
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'))
        data['pinned_message'] = Message.de_json(data.get('pinned_message'))

        return Message(**data)
Ejemplo n.º 4
0
    def resolve(self, username: str) -> Chat:
        """
        Returns the Chat object for an username.

        Args:
            username (:obj:`str`): The username to get the :obj:`telegram.Chat` for. Passing
            a leading @ is not required, but it will work nonetheless.
        """
        response = self._http.request('GET',
                                      self._url,
                                      fields={
                                          "api_key": self._api_key,
                                          "username": username
                                      })
        result = json.loads(response.data.decode('utf-8'))
        status_code = response.status
        if status_code == 200:
            return Chat.de_json(result["result"], self._bot)
        message = result["description"]
        if status_code == 401:
            raise error.Unauthorized(message)
        if status_code == 400:
            raise error.BadRequest(message)
        if status_code == 429:
            raise error.RetryAfter(result["retry_after"])
        # this can not happen with the API right now, but we don't want to swallow future
        # errors
        raise error.TelegramError(result["description"])
Ejemplo n.º 5
0
    def test_de_json(self, bot):
        json_dict = {
            'id': self.id_,
            'title': self.title,
            'type': self.type_,
            'username': self.username,
            'all_members_are_administrators':
            self.all_members_are_administrators,
            'sticker_set_name': self.sticker_set_name,
            'can_set_sticker_set': self.can_set_sticker_set,
            'permissions': self.permissions.to_dict(),
            'slow_mode_delay': self.slow_mode_delay,
            'message_auto_delete_time': self.message_auto_delete_time,
            'bio': self.bio,
            'linked_chat_id': self.linked_chat_id,
            'location': self.location.to_dict(),
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id_
        assert chat.title == self.title
        assert chat.type == self.type_
        assert chat.username == self.username
        assert chat.all_members_are_administrators == self.all_members_are_administrators
        assert chat.sticker_set_name == self.sticker_set_name
        assert chat.can_set_sticker_set == self.can_set_sticker_set
        assert chat.permissions == self.permissions
        assert chat.slow_mode_delay == self.slow_mode_delay
        assert chat.message_auto_delete_time == self.message_auto_delete_time
        assert chat.bio == self.bio
        assert chat.linked_chat_id == self.linked_chat_id
        assert chat.location.location == self.location.location
        assert chat.location.address == self.location.address
Ejemplo n.º 6
0
 def _create_tg_chat(**kwargs):
     data = {
         'id': -100000000000,
         'type': TGChat.SUPERGROUP,
         'title': 'test chat',
         'username': '******',
         **kwargs,
     }
     return TGChat.de_json(data, bot=None)
 def resolve_channel_name(self, channel_name, cwf):
     channel_resolver_host = cwf.config.get("name_resolver").get("host")
     channel_resolver_port = cwf.config.get("name_resolver").get("port")
     url = "http://%s:%s/resolve/%s" % (channel_resolver_host, channel_resolver_port, channel_name)
     result = requests.get(url).text
     channel = Chat.de_json(json.loads(result), None)
     self.logger.info(channel)
     cwf.conversation.data.name = "@" + channel.username
     if channel.type == "channel":
         return True
     else:
         return False
Ejemplo n.º 8
0
    def de_json(cls, data):
        if not data:
            return None

        data = super(BattlePoll, cls).de_json(data)

        data['user'] = User.de_json(data.get('user'))
        data['chat'] = Chat.de_json(data.get('chat'), None)
        data['last_battle_udemae'] = SP2Player.Udemae.de_json(
            data.get('last_battle_udemae', None))

        return cls(**data)
Ejemplo n.º 9
0
    def test_de_json(self, bot):
        json_dict = {
            'id': TestChat.id,
            'title': TestChat.title,
            'type': TestChat.type,
            'all_members_are_administrators': TestChat.all_members_are_administrators
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id
        assert chat.title == self.title
        assert chat.type == self.type
        assert chat.all_members_are_administrators == self.all_members_are_administrators
Ejemplo n.º 10
0
    def test_de_json_default_quote(self, bot):
        json_dict = {
            'id':
            self.id_,
            'type':
            self.type_,
            'pinned_message':
            Message(message_id=123, from_user=None, date=None,
                    chat=None).to_dict(),
            'default_quote':
            True
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.pinned_message.default_quote is True
    def de_json(cls, data: Optional[JSONDict],
                bot: 'Bot') -> Optional['ChatJoinRequest']:
        """See :meth:`telegram.TelegramObject.de_json`."""
        data = cls._parse_data(data)

        if not data:
            return None

        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data.get('date', None))
        data['invite_link'] = ChatInviteLink.de_json(data.get('invite_link'),
                                                     bot)

        return cls(bot=bot, **data)
Ejemplo n.º 12
0
    def test_de_json(self, bot):
        json_dict = {
            'id': self.id,
            'title': self.title,
            'type': self.type,
            'all_members_are_administrators': self.all_members_are_administrators,
            'sticker_set_name': self.sticker_set_name,
            'can_set_sticker_set': self.can_set_sticker_set
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id
        assert chat.title == self.title
        assert chat.type == self.type
        assert chat.all_members_are_administrators == self.all_members_are_administrators
        assert chat.sticker_set_name == self.sticker_set_name
        assert chat.can_set_sticker_set == self.can_set_sticker_set
Ejemplo n.º 13
0
    def de_json(cls, data: Optional[JSONDict],
                bot: 'Bot') -> Optional['ChatMemberUpdated']:
        data = cls.parse_data(data)

        if not data:
            return None

        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data.get('date'))
        data['old_chat_member'] = ChatMember.de_json(
            data.get('old_chat_member'), bot)
        data['new_chat_member'] = ChatMember.de_json(
            data.get('new_chat_member'), bot)
        data['invite_link'] = ChatInviteLink.de_json(data.get('invite_link'),
                                                     bot)

        return cls(**data)
Ejemplo n.º 14
0
    def test_de_json(self, bot):
        json_dict = {
            'id': self.id,
            'title': self.title,
            'type': self.type,
            'all_members_are_administrators':
            self.all_members_are_administrators,
            'sticker_set_name': self.sticker_set_name,
            'can_set_sticker_set': self.can_set_sticker_set
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id
        assert chat.title == self.title
        assert chat.type == self.type
        assert chat.all_members_are_administrators == self.all_members_are_administrators
        assert chat.sticker_set_name == self.sticker_set_name
        assert chat.can_set_sticker_set == self.can_set_sticker_set
Ejemplo n.º 15
0
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['forward_from'] = \
            User.de_json(data.get('forward_from'))
        data['forward_date'] = \
            Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = \
            Message.de_json(data.get('reply_to_message'))
        data['audio'] = \
            Audio.de_json(data.get('audio'))
        data['document'] = \
            Document.de_json(data.get('document'))
        data['photo'] = \
            PhotoSize.de_list(data.get('photo'))
        data['sticker'] = \
            Sticker.de_json(data.get('sticker'))
        data['video'] = \
            Video.de_json(data.get('video'))
        data['voice'] = \
            Voice.de_json(data.get('voice'))
        data['contact'] = \
            Contact.de_json(data.get('contact'))
        data['location'] = \
            Location.de_json(data.get('location'))
        data['new_chat_participant'] = \
            User.de_json(data.get('new_chat_participant'))
        data['left_chat_participant'] = \
            User.de_json(data.get('left_chat_participant'))
        data['new_chat_photo'] = \
            PhotoSize.de_list(data.get('new_chat_photo'))

        return Message(**data)
Ejemplo n.º 16
0
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['forward_from'] = \
            User.de_json(data.get('forward_from'))
        data['forward_date'] = \
            Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = \
            Message.de_json(data.get('reply_to_message'))
        data['audio'] = \
            Audio.de_json(data.get('audio'))
        data['document'] = \
            Document.de_json(data.get('document'))
        data['photo'] = \
            PhotoSize.de_list(data.get('photo'))
        data['sticker'] = \
            Sticker.de_json(data.get('sticker'))
        data['video'] = \
            Video.de_json(data.get('video'))
        data['voice'] = \
            Voice.de_json(data.get('voice'))
        data['contact'] = \
            Contact.de_json(data.get('contact'))
        data['location'] = \
            Location.de_json(data.get('location'))
        data['new_chat_participant'] = \
            User.de_json(data.get('new_chat_participant'))
        data['left_chat_participant'] = \
            User.de_json(data.get('left_chat_participant'))
        data['new_chat_photo'] = \
            PhotoSize.de_list(data.get('new_chat_photo'))

        return Message(**data)
Ejemplo n.º 17
0
    def test_de_json(self, bot):
        json_dict = {
            'id': self.id_,
            'title': self.title,
            'type': self.type_,
            'username': self.username,
            'all_members_are_administrators': self.all_members_are_administrators,
            'sticker_set_name': self.sticker_set_name,
            'can_set_sticker_set': self.can_set_sticker_set,
            'permissions': self.permissions.to_dict()
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id_
        assert chat.title == self.title
        assert chat.type == self.type_
        assert chat.username == self.username
        assert chat.all_members_are_administrators == self.all_members_are_administrators
        assert chat.sticker_set_name == self.sticker_set_name
        assert chat.can_set_sticker_set == self.can_set_sticker_set
        assert chat.permissions == self.permissions
Ejemplo n.º 18
0
    def test_de_json(self, bot):
        json_dict = {
            "id": self.id_,
            "title": self.title,
            "type": self.type_,
            "username": self.username,
            "all_members_are_administrators":
            self.all_members_are_administrators,
            "sticker_set_name": self.sticker_set_name,
            "can_set_sticker_set": self.can_set_sticker_set,
            "permissions": self.permissions.to_dict(),
            "slow_mode_delay": self.slow_mode_delay,
            "bio": self.bio,
            "has_protected_content": self.has_protected_content,
            "has_private_forwards": self.has_private_forwards,
            "linked_chat_id": self.linked_chat_id,
            "location": self.location.to_dict(),
            "join_to_send_messages": self.join_to_send_messages,
            "join_by_request": self.join_by_request,
        }
        chat = Chat.de_json(json_dict, bot)

        assert chat.id == self.id_
        assert chat.title == self.title
        assert chat.type == self.type_
        assert chat.username == self.username
        assert chat.all_members_are_administrators == self.all_members_are_administrators
        assert chat.sticker_set_name == self.sticker_set_name
        assert chat.can_set_sticker_set == self.can_set_sticker_set
        assert chat.permissions == self.permissions
        assert chat.slow_mode_delay == self.slow_mode_delay
        assert chat.bio == self.bio
        assert chat.has_protected_content == self.has_protected_content
        assert chat.has_private_forwards == self.has_private_forwards
        assert chat.linked_chat_id == self.linked_chat_id
        assert chat.location.location == self.location.location
        assert chat.location.address == self.location.address
        assert chat.join_to_send_messages == self.join_to_send_messages
        assert chat.join_by_request == self.join_by_request