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)
def answer_inline_query(self, msg, results, offset=None): self.outbox.put( Message(None, msg.conversation, self.info, results, 'inline_results', extra={"offset": offset}))
def send_message(self, msg, content, type='text', reply=None, extra=None): message = Message(None, msg.conversation, self.info, content, type, reply=reply, extra=extra) self.outbox.put(message)
def send_admin_alert(self, text): message = Message(None, Conversation(self.config.admin_conversation_id, 'Admin'), self.info, text, extra={ 'format': 'HTML', 'preview': False }) self.outbox.put(message)
def forward_message(self, msg, id): self.outbox.put( Message(None, msg.conversation, self.info, msg.content, 'forward', extra={ "message": msg.id, "conversation": id }))
def send_alert(self, text): message = Message(None, Conversation(self.config.alerts_conversation_id, 'Alerts'), self.info, '<pre>%s</pre>' % text, extra={ 'format': 'HTML', 'preview': False }) self.outbox.put(message)
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 send_alert(self, text, language='python'): message = Message(None, Conversation(self.config.alerts_conversation_id, 'Alerts'), self.info, '<code class="language-{}">{}</code>'.format( language, text), extra={ 'format': 'HTML', 'preview': False }) self.outbox.put(message)
def send_random_message_to_conversation(self, topic='?'): try: if hasattr(self.bot.trans.plugins, 'autopost'): strings = self.bot.trans.plugins.autopost.strings[topic] i = randint(0, len(strings) - 1) text = strings[i] for cid in self.get_conversations_to_post(topic): m = Message(None, Conversation(int(cid)), None, None) self.bot.send_message(m, text, extra={'format': 'HTML'}) except Exception as e: logging.info('Exception found: ' + str(e))
def cron(self): self.reminders.load_database() while len(self.reminders.list ) > 0 and self.reminders.list[0].alarm < time(): reminder = self.reminders.list[0] text = '<i>%s</i>\n - %s' % (reminder.text, reminder.first_name) if reminder.username: text += ' (@%s)' % reminder.username m = Message(None, Conversation(reminder.chat_id), None, None) self.bot.send_message(m, text, extra={'format': 'HTML'}) self.reminders.list.remove(reminder) self.sort_reminders()
def cron(self): self.bot.reminders = wait_until_received('reminders/' + self.bot.name) if not 'list' in self.bot.reminders or not self.bot.reminders.list: self.bot.reminders['list'] = [] while len(self.bot.reminders['list'] ) > 0 and self.bot.reminders.list[0].alarm < time(): reminder = self.bot.reminders.list[0] text = '<i>%s</i>\n - %s' % (reminder.text, reminder.first_name) if reminder.username: text += ' (@%s)' % reminder.username m = Message(None, Conversation(reminder.chat_id), None, None) self.bot.send_message(m, text, extra={'format': 'HTML'}) self.bot.reminders.list.remove(reminder) set_data('reminders/%s' % self.bot.name, self.bot.reminders) self.sort_reminders()
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)
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)
def answer_inline_query(self, msg, results, extra={}): self.outbox.put( Message(msg.id, msg.conversation, self.info, json.dumps(results), 'inline_results', extra))
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)
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)