Esempio n. 1
0
 def get_me(self):
     try:
         msg = self.sender.get_self()
         return User(msg.peer_id, msg.first_name, None, msg.username)
     except Exception as e:
         catch_exception(e, self.bot)
         return None
Esempio n. 2
0
 async def on_ready():
     self.bot.info = User(self.client.user.id, self.client.user.name, self.client.user.discriminator,
                          self.client.user.name + '#' + self.client.user.discriminator, self.client.user.bot)
     status = '{}help'.format(self.bot.config.prefix)
     activity = discord.Activity(
         type=discord.ActivityType.listening, name=status)
     await self.client.change_presence(activity=activity)
Esempio n. 3
0
    def convert_message(self, msg):
        try:
            # logging.info(msg)
            id = msg.id
            extra = {}
            content = msg.content
            type = 'text'
            date = time()
            logging.info('%s - %s' %
                         (time(), mktime(msg.created_at.timetuple())))
            reply = None

            sender = User(msg.author.id, msg.author.name, '#' + msg.author.discriminator,
                          msg.author.name + '#' + msg.author.discriminator, msg.author.bot)

            conversation = Conversation(msg.channel.id)

            if hasattr(msg.channel, 'name'):
                conversation.id = -msg.channel.id
                conversation.title = msg.channel.name

            elif hasattr(msg.channel, 'recipient'):
                conversation.id = msg.channel.recipient.id
                conversation.title = msg.channel.recipient.name

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

        except Exception as e:
            logging.error(e)
            catch_exception(e, self.bot)
Esempio n. 4
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
Esempio n. 5
0
    def convert_inline(self, msg):
        id = msg.id
        conversation = Conversation(msg['from'].id, msg['from'].first_name)

        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

        content = msg.query
        type = 'inline_query'
        date = None
        reply = None
        extra = {'offset': msg.offset}

        return Message(id, conversation, sender, content, type, date, reply, extra)
Esempio n. 6
0
 def get_chat_administrators(self, conversation_id):
     if int(conversation_id) > 0:
         return False
     channel = self.client.get_channel(positive(conversation_id))
     admins = []
     for member in channel.members:
         perms = channel.permissions_for(member)
         if perms.administrator:
             admins.append(User(member.id, member.name, '#' + member.discriminator, member.name + '#' + member.discriminator, member.bot))
     logging.info(admins)
     return admins
Esempio n. 7
0
        async def on_member_join(self, member):
            guild = member.guild
            if guild.system_channel is not None:
                sender = User(member.id, member.name, '#' + member.discriminator,
                          member.name + '#' + member.discriminator, member.bot)
                conversation = Conversation(guild.system_channel.id, guild.system_channel.name)
                extra = {
                    'user': sender
                }

                self.bot.outbox.put(Message(0, conversation, sender, 'new_chat_member', 'notification', time(), None, extra))
    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
Esempio n. 9
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

        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':
                        extra['urls'] = []
                        extra['urls'].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 '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)}

        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)
Esempio n. 10
0
 def get_me(self):
     r = self.api_request('getMe')
     return User(r.result.id, r.result.first_name, None, r.result.username)
Esempio n. 11
0
 def get_me(self):
     result = self.client.get_me()
     result.wait()
     me = result.update
     return User(me['id'], me['first_name'], me['last_name'],
                 me['username'], me['type']['@type'] == 'userTypeBot')
Esempio n. 12
0
    def convert_message(self, msg):
        try:
            # logging.info(msg)
            id = msg['id']
            extra = {}

            raw_chat = self.server_request('getChat',
                                           {'chat_id': msg['chat_id']})

            conversation = Conversation(int(msg['chat_id']))
            if raw_chat and 'title' in raw_chat:
                conversation.title = raw_chat['title']

            if 'user_id' in msg['sender']:
                raw_sender = self.server_request(
                    'getUser', {'user_id': msg['sender']['user_id']})

                sender = User(int(msg['sender']['user_id']))
                if 'first_name' in raw_sender:
                    sender.first_name = str(raw_sender['first_name'])
                if 'last_name' in raw_sender:
                    sender.last_name = str(raw_sender['last_name'])
                if 'username' in raw_sender:
                    sender.username = str(raw_sender['username'])

            else:
                sender = User(conversation.id, conversation.title)

            if msg['content']['@type'] == 'messageText':
                content = msg['content']['text']['text']
                type = 'text'

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

                        elif entity['type'][
                                '@type'] == 'textEntityTypeMention':
                            if 'mentions' not in extra:
                                extra['mentions'] = []
                            extra['mentions'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

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

                        elif entity['type'][
                                '@type'] == 'textEntityTypeHashtag':
                            if 'hashtags' not in extra:
                                extra['hashtags'] = []
                            extra['hashtags'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

                        elif entity['type'][
                                '@type'] == 'textEntityTypeCashtag':
                            if 'cashtags' not in extra:
                                extra['cashtags'] = []
                            extra['cashtags'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

                        elif entity['type'][
                                '@type'] == 'textEntityTypeBotCommand':
                            if 'commands' not in extra:
                                extra['commands'] = []
                            extra['commands'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

                        elif entity['type'][
                                '@type'] == 'textEntityTypeEmailAddress':
                            if 'emails' not in extra:
                                extra['emails'] = []
                            extra['emails'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

                        elif entity['type'][
                                '@type'] == 'textEntityTypePhoneNumber':
                            if 'phone_numbers' not in extra:
                                extra['phone_numbers'] = []
                            extra['phone_numbers'].append(
                                content[entity['offset']:entity['offset'] +
                                        entity['length']])

            elif msg['content']['@type'] == 'messagePhoto':
                content = msg['content']['photo']['sizes'][0]['photo'][
                    'remote']['id']
                type = 'photo'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageAnimation':
                content = msg['content']['animation']['animation']['remote'][
                    'id']
                type = 'animation'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageDocument':
                content = msg['content']['document']['document']['remote'][
                    'id']
                type = 'document'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageAudio':
                content = msg['content']['audio']['audio']['remote']['id']
                type = 'audio'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageVideo':
                content = msg['content']['video']['video']['remote']['id']
                type = 'video'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageVoiceNote':
                content = msg['content']['voice_note']['voice']['remote']['id']
                type = 'voice'
                if msg['content']['caption']:
                    extra['caption'] = msg['content']['caption']

            elif msg['content']['@type'] == 'messageSticker':
                content = msg['content']['sticker']['sticker']['remote']['id']
                type = 'sticker'

            elif msg['content']['@type'] == 'messageChatAddMembers':
                content = 'new_chat_member'
                type = 'notification'

                request = self.client.get_user(
                    msg['content']['member_user_ids'][0])
                request.wait()
                raw_user = request.update

                extra = {
                    'user': User(int(msg['content']['member_user_ids'][0]))
                }
                if raw_user:
                    if 'first_name' in raw_user:
                        extra['user'].first_name = str(raw_user['first_name'])
                    if 'last_name' in raw_user:
                        extra['user'].last_name = str(raw_user['last_name'])
                    if 'username' in raw_user:
                        extra['user'].username = str(raw_user['username'])

            elif msg['content']['@type'] == 'messageChatJoinByLink':
                content = 'new_chat_member'
                type = 'notification'

                extra = {'user': sender}

            elif msg['content']['@type'] == 'messageChatDeleteMember':
                content = 'left_chat_member'
                type = 'notification'

                request = self.client.get_user(msg['content']['user_id'])
                request.wait()
                raw_user = request.update

                extra = {'user': User(int(msg['content']['user_id']))}
                if raw_user:
                    if 'first_name' in raw_user:
                        extra['user'].first_name = str(raw_user['first_name'])
                    if 'last_name' in raw_user:
                        extra['user'].last_name = str(raw_user['last_name'])
                    if 'username' in raw_user:
                        extra['user'].username = str(raw_user['username'])

            elif msg['content']['@type'] == 'messageUnsupported':
                content = 'Message content that is not supported by the client'
                type = 'unsupported'

            else:
                logging.info('UNSUPPORTED MESSAGE TYPE: {}'.format(
                    msg['content']['@type']))
                content = msg['content']['@type']
                type = 'unsupported'

            reply = None
            if 'reply_to_message_id' in msg and msg['reply_to_message_id'] > 0:
                reply = self.get_message(msg['chat_id'],
                                         msg['reply_to_message_id'])

            if 'forward_info' in msg and msg['forward_info']:
                extra['from_chat_id'] = msg['forward_info']['from_chat_id']
                extra['from_message_id'] = msg['forward_info'][
                    'from_message_id']

                if 'chat_id' in msg['forward_info']['origin']:
                    extra['from_chat_id'] = msg['forward_info']['origin'][
                        'chat_id']

                if 'message_id' in msg['forward_info']['origin']:
                    extra['from_message_id'] = msg['forward_info']['origin'][
                        'message_id']

                if 'sender_user_id' in msg['forward_info']['origin']:
                    extra['from_user_id'] = msg['forward_info']['origin'][
                        'sender_user_id']

            if 'via_bot_user_id' in msg and msg['via_bot_user_id'] > 0:
                extra['via_bot_user_id'] = msg['via_bot_user_id']

            if 'restriction_reason' in msg and msg['restriction_reason']:
                extra['restriction_reason'] = msg['restriction_reason']

            if 'reply_markup' in msg and msg['reply_markup']:
                extra['reply_markup'] = msg['reply_markup']

            date = msg['date']

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

        except Exception as e:
            logging.error('convert_message exception: {}'.format(e))
            catch_exception(e, self.bot)
Esempio n. 13
0
    def convert_message(self, msg):
        id = msg['id']
        if msg.receiver.type == 'user':
            conversation = Conversation(msg.sender.peer_id)
            conversation.title = msg.sender.first_name
        else:
            if msg.receiver.type == 'channel':
                conversation = Conversation(- int('100' + str(msg.receiver.peer_id)))
            else:
                conversation = Conversation(- int(msg.receiver.peer_id))
            conversation.title = msg.receiver.title

        if msg.sender.type == 'user':
            sender = User(int(msg.sender.peer_id))
            if 'first_name' in msg.sender:
                sender.first_name = msg.sender.first_name
            if 'last_name' in msg.sender:
                sender.last_name = msg.sender.last_name
            if 'username' in msg.sender:
                sender.username = msg.sender.username
        else:
            if msg.sender.type == 'channel':
                sender = Conversation(- int('100' + str(msg.sender.peer_id)))
            else:
                sender = Conversation(- int(msg.sender.peer_id))
            sender.first_name = msg.sender.title

        date = msg.date

        # Gets the type of the message
        if 'text' in msg:
            type = 'text'
            content = msg.text
            extra = None
        elif 'media' in msg:
            type = msg.media.type
            content = msg.id
            if 'caption' in msg.media:
                extra = msg.media.caption
            else:
                extra = None
        elif msg.event == 'service':
            type = 'service'
            if msg.action.type == 'chat_del_user':
                content = 'left_user'
                extra = msg.action.user.peer_id
            elif msg.action.type == 'chat_add_user':
                content = 'join_user'
                extra = msg.action.user.peer_id
            elif msg.action.type == 'chat_add_user_link':
                content = 'join_user'
                extra = msg.sender.peer_id
            else:
                type = None
                content = None
                extra = None
        else:
            type = None
            content = None
            extra = None

        # Generates another message object for the original message if the reply.
        if 'reply_id' in msg:
            reply_msg = self.sender.message_get(msg.reply_id)
            reply = self.convert_message(reply_msg)

        else:
            reply = None

        return Message(id, conversation, sender, content, type, date, reply, extra)
Esempio n. 14
0
 def get_me(self):
     msg = self.sender.get_self()
     return User(msg.peer_id, msg.first_name, None, msg.username)
Esempio n. 15
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)

        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

        # 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':
                        extra['urls'] = []
                        extra['urls'].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 '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)
            }

        else:
            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)
Esempio n. 16
0
    def convert_message(self, msg):
        id = msg['id']
        if msg.receiver.type == 'user':
            conversation = Conversation(msg.sender.peer_id)
            conversation.title = msg.sender.first_name
        else:
            if msg.receiver.type == 'channel':
                conversation = Conversation(-int('100' +
                                                 str(msg.receiver.peer_id)))
            else:
                conversation = Conversation(-int(msg.receiver.peer_id))
            conversation.title = msg.receiver.title

        if msg.sender.type == 'user':
            sender = User(int(msg.sender.peer_id))
            sender.first_name = msg.sender.first_name
            if 'first_name' in msg.sender:
                sender.first_name = msg.sender.first_name
            if 'last_name' in msg.sender:
                sender.last_name = msg.sender.last_name
            if 'username' in msg.sender:
                sender.username = msg.sender.username
        else:
            if msg.sender.type == 'channel':
                sender = Conversation(-int('100' + str(msg.sender.peer_id)))
            else:
                sender = Conversation(-int(msg.sender.peer_id))
            sender.title = msg.sender.title

        date = msg.date

        # Gets the type of the message
        if 'text' in msg:
            type = 'text'
            content = msg.text
            extra = None
        elif 'media' in msg:
            type = msg.media.type
            content = msg.id
            if 'caption' in msg.media:
                extra = msg.media.caption
            else:
                extra = None
        elif msg.event == 'service':
            type = 'service'
            if msg.action.type == 'chat_del_user':
                content = 'left_user'
                extra = msg.action.user.peer_id
            elif msg.action.type == 'chat_add_user':
                content = 'join_user'
                extra = msg.action.user.peer_id
            elif msg.action.type == 'chat_add_user_link':
                content = 'join_user'
                extra = msg.sender.peer_id
            else:
                type = None
                content = None
                extra = None
        else:
            type = None
            content = None
            extra = None

        # Generates another message object for the original message if the reply.
        if 'reply_id' in msg:
            reply_msg = self.sender.message_get(msg.reply_id)
            reply = self.convert_message(reply_msg)

        else:
            reply = None

        return Message(id, conversation, sender, content, type, date, reply,
                       extra)
Esempio n. 17
0
 def get_me(self):
     return User(0, self.bot.name, None, self.bot.name)