Пример #1
0
    def get_chat_administrators(self, conversation_id):
        result = self.server_request('getChatAdministrators',
                                     {'chat_id': conversation_id},
                                     ignore_errors=True)

        admins = []
        if result and 'administrators' in result:
            for member in result['administrators']:
                user = User(member['user_id'])

                request = self.client.get_user(user.id)
                request.wait()
                raw_user = request.update

                if raw_user:
                    user.is_bot = raw_user['type']['@type'] == 'userTypeBot'
                    if 'first_name' in raw_user:
                        user.first_name = str(raw_user['first_name'])
                    if 'last_name' in raw_user:
                        user.last_name = str(raw_user['last_name'])
                    if 'username' in raw_user:
                        user.username = str(raw_user['username'])

                admins.append(user)

        return admins
Пример #2
0
    def get_chat_administrators(self, conversation_id):
        params = {
            "chat_id": conversation_id
        }
        result = self.api_request('getChatAdministrators', params)

        admins = []
        if 'result' in result:
            for member in result.result:
                user = User(member.user.id, member.user.first_name)
                user.is_bot = member.user.is_bot
                if 'last_name' in member.user:
                    user.last_name = member.user.last_name
                if 'username' in member.user:
                    user.username = member.user.username
                admins.append(user)
        return admins
Пример #3
0
    def convert_message(self, msg):
        id = msg.message_id
        if msg.chat.id > 0:
            conversation = Conversation(msg.chat.id, msg.chat.first_name)
        else:
            conversation = Conversation(msg.chat.id, msg.chat.title)

        if 'from' in msg:
            sender = User(msg['from'].id, msg['from'].first_name)

            if 'last_name' in msg['from']:
                sender.last_name = msg['from'].last_name

            if 'username' in msg['from']:
                sender.username = msg['from'].username

            if 'is_bot' in msg['from']:
                sender.is_bot = msg['from'].is_bot

        else:
            sender = Conversation(msg.chat.id, msg.chat.title)

        # Gets the type of the message
        extra = {}

        if 'text' in msg:
            type = 'text'
            content = msg.text

            if 'entities' in msg:
                for entity in msg.entities:
                    if entity.type == 'url':
                        if 'urls' not in extra:
                            extra['urls'] = []
                        extra['urls'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'text_link':
                        if 'urls' not in extra:
                            extra['urls'] = []
                        extra['urls'].append(entity.url)

                    elif entity.type == 'mention':
                        if 'mentions' not in extra:
                            extra['mentions'] = []
                        extra['mentions'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'text_mention':
                        if 'mentions' not in extra:
                            extra['mentions'] = []
                        extra['mentions'].append(entity.user.id)

                    elif entity.type == 'hashtag':
                        if 'hashtags' not in extra:
                            extra['hashtags'] = []
                        extra['hashtags'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'cashtag':
                        if 'cashtags' not in extra:
                            extra['cashtags'] = []
                        extra['cashtags'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'bot_command':
                        if 'commands' not in extra:
                            extra['commands'] = []
                        extra['commands'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'email':
                        if 'emails' not in extra:
                            extra['emails'] = []
                        extra['emails'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

                    elif entity.type == 'phone_number':
                        if 'phone_numbers' not in extra:
                            extra['phone_numbers'] = []
                        extra['phone_numbers'].append(
                            msg.text[entity.offset:entity.offset + entity.length])

        elif 'audio' in msg:
            type = 'audio'
            content = msg.audio.file_id
            extra['duration'] = msg.audio.duration
            if 'performer' in msg.audio:
                extra['performer'] = msg.audio.performer
            if 'title' in msg.audio:
                extra['title'] = msg.audio.title
            if 'mime_type' in msg.audio:
                extra['mime_type'] = msg.audio.mime_type
            if 'file_size' in msg.audio:
                extra['file_size'] = msg.audio.file_size

        elif 'document' in msg:
            type = 'document'
            content = msg.document.file_id
            if 'thumb' in msg.document:
                extra['thumbnail'] = msg.document.thumb.file_id
            if 'file_name' in msg.document:
                extra['file_name'] = msg.document.file_name
            if 'mime_type' in msg.document:
                extra['mime_type'] = msg.document.mime_type
            if 'file_size' in msg.document:
                extra['file_size'] = msg.document.file_size

        elif 'game' in msg:
            type = 'game'
            content = {
                'title': msg.game.title,
                'description': msg.game.description,
                'photo': msg.game.photo[-1].file_id,
            }

            if 'text' in msg.game:
                extra['text'] = msg.game.text

        elif 'photo' in msg:
            type = 'photo'
            content = msg.photo[-1].file_id
            if 'width' in msg.photo[-1]:
                extra['width'] = msg.photo[-1].width
            if 'height' in msg.photo[-1]:
                extra['height'] = msg.photo[-1].height
            if 'file_size' in msg.photo[-1]:
                extra['file_size'] = msg.photo[-1].file_size

        elif 'sticker' in msg:
            type = 'sticker'
            content = msg.sticker.file_id
            if 'width' in msg.sticker:
                extra['width'] = msg.sticker.width
            if 'height' in msg.sticker:
                extra['height'] = msg.sticker.height
            if 'file_size' in msg.sticker:
                extra['file_size'] = msg.sticker.file_size
            if 'emoji' in msg.sticker:
                extra['emoji'] = msg.sticker.emoji
            if 'thumb' in msg.sticker:
                extra['thumbnail'] = msg.sticker.thumb.file_id

        elif 'video' in msg:
            type = 'video'
            content = msg.video.file_id
            if 'width' in msg.video:
                extra['width'] = msg.video.width
            if 'height' in msg.video:
                extra['height'] = msg.video.height
            if 'file_size' in msg.video:
                extra['file_size'] = msg.video.file_size
            if 'duration' in msg.video:
                extra['duration'] = msg.video.duration
            if 'thumb' in msg.video:
                extra['thumbnail'] = msg.video.thumb.file_id
            if 'mime_type' in msg.video:
                extra['mime_type'] = msg.video.mime_type

        elif 'voice' in msg:
            type = 'voice'
            content = msg.voice.file_id
            if 'file_size' in msg.voice:
                extra['file_size'] = msg.voice.file_size
            if 'duration' in msg.voice:
                extra['duration'] = msg.voice.duration
            if 'mime_type' in msg.voice:
                extra['mime_type'] = msg.voice.mime_type

        elif 'contact' in msg:
            type = 'contact'
            content = msg.contact.phone_number
            if 'first_name' in msg.contact:
                extra['first_name'] = msg.contact.first_name
            if 'last_name' in msg.contact:
                extra['last_name'] = msg.contact.last_name
            if 'user_id' in msg.contact:
                extra['user_id'] = msg.contact.user_id

        elif 'location' in msg:
            type = 'location'
            content = {
                'longitude': msg.location.longitude,
                'latitude': msg.location.latitude
            }
        elif 'venue' in msg:
            type = 'venue'
            content = {
                'longitude': msg.venue.location.longitude,
                'latitude': msg.venue.location.latitude
            }
            if 'title' in msg.venue:
                extra['title'] = msg.venue.title
            if 'address' in msg.venue:
                extra['address'] = msg.venue.address
            if 'foursquare_id' in msg.venue:
                extra['foursquare_id'] = msg.venue.foursquare_id

        elif 'video_note' in msg:
            type = 'video_note'
            content = msg.video_note.file_id
            if 'length' in msg.video_note:
                extra['length'] = msg.video_note.length
            if 'duration' in msg.video_note:
                extra['duration'] = msg.video_note.duration
            if 'thumb' in msg.video_note:
                extra['thumb'] = msg.video_note.thumb
            if 'file_size' in msg.video_note:
                extra['file_size'] = msg.video_note.file_size

        elif 'poll' in msg:
            type = 'poll'
            content = msg.poll.question
            if 'id' in msg.poll:
                extra['poll_id'] = msg.poll.id
            if 'options' in msg.poll:
                extra['poll_options'] = msg.poll.options
            if 'is_closed' in msg.poll:
                extra['poll_is_closed'] = msg.poll.is_closed

        elif 'new_chat_member' in msg:
            type = 'notification'
            content = 'new_chat_member'
            extra = {
                'user': User(msg.new_chat_member.id, msg.new_chat_member.first_name)
            }

            if 'last_name' in msg.new_chat_member:
                extra['user'].last_name = msg.new_chat_member.last_name
            if 'username' in msg.new_chat_member:
                extra['user'].username = msg.new_chat_member.username

        elif 'left_chat_member' in msg:
            type = 'notification'
            content = 'left_chat_member'
            extra = {
                'user': User(msg.left_chat_member.id, msg.left_chat_member.first_name)
            }

            if 'last_name' in msg.left_chat_member:
                extra['user'].last_name = msg.left_chat_member.last_name
            if 'username' in msg.left_chat_member:
                extra['user'].username = msg.left_chat_member.username

        elif 'new_chat_photo' in msg:
            type = 'notification'
            content = 'new_chat_photo'
            extra = {
                'photo': msg.new_chat_photo[-1].file_id
            }

        elif 'delete_chat_photo' in msg:
            type = 'notification'
            content = 'delete_chat_photo'

        elif 'group_chat_created' in msg:
            type = 'notification'
            content = 'group_chat_created'

        elif 'supergroup_chat_created' in msg:
            type = 'notification'
            content = 'supergroup_chat_created'

        elif 'channel_chat_created' in msg:
            type = 'notification'
            content = 'channel_chat_created'

        elif 'migrate_to_chat_id' in msg:
            type = 'notification'
            content = 'upgrade_to_supergroup'
            extra = {
                'chat_id': msg.migrate_to_chat_id,
                'from_chat_id': msg.chat.id
            }

        elif 'pinned_message' in msg:
            type = 'notification'
            content = 'pinned_message'
            extra = {
                'message': self.convert_message(msg.pinned_message)
            }

        elif 'dice' in msg:
            type = 'dice'
            content = msg.dice.value
            extra = {
                'emoji': msg.dice.emoji
            }

        else:
            logging.error(msg)
            type = None
            content = None
            extra = None

        if 'caption' in msg:
            extra['caption'] = msg.caption

        date = msg.date

        if 'reply_to_message' in msg:
            reply = self.convert_message(msg.reply_to_message)
        else:
            reply = None

        return Message(id, conversation, sender, content, type, date, reply, extra)