def to_tg_handler(sender, **kwargs): """Handle signals sent to Telegram. This will involve sending messages through the Telegram bot. Args: phone (str): Phone number that sent the message. message (str): The message received """ phone = kwargs.get('phone') message = kwargs.get('message', '') # Check if known contact contact = get_contact(phone) chat_id = SETTINGS['owner'] if not contact: # Unknown sender output = 'Message from #unknown\n' output += 'Phone number: %s\n' % phone output += '---------\n' output += message logger.info('received message from unknown number: %s' % phone) else: group = db_get_group(contact) if not group: # Known sender output = 'Message from #%s\n' % contact output += '---------\n' output += message else: # Contact is bound to group chat_id = group output = message logger.info('received message from %s' % contact) # Deliver message through Telegram for chunk in tgutil.split_string(output, 3000): tgbot.send_message(chat_id, chunk)
def unbind(message): """Unbind a contact from his group. Message has the following format: /unbind <name> Args: message: Received Telegram message. """ if message.chat.id != SETTINGS['owner']: tgbot.reply_to(message, 'You are not the owner of this bot') return # Get name and phone name = telebot.util.extract_arguments(message.text) if not name: tgbot.reply_to(message, 'Syntax: /unbind <name>') return # Ensure contact exists if not get_phone(name): tgbot.reply_to(message, 'No contact found with that name') return # Check if it already exists group = db_get_group(name) if not group: tgbot.reply_to(message, 'Contact was not bound to a group') return # Add to database db_set_group(name, None) tgbot.reply_to(message, 'Unbound from group')
def to_tg_handler(sender, **kwargs): """Handle signals sent to Telegram. This will involve sending messages through the Telegram bot. Args: phone (str): Phone number that sent the message. message (str): The message received media (boolean): True or False """ phone = kwargs.get('phone') message = kwargs.get('message') media = kwargs.get('media') # Check if known contact contact = get_contact(phone) chat_id = SETTINGS['owner'] if media: participant_id, message_url = message.split("=|=|=") # Media Messages if not contact: output = 'Media from #unknown\n' output += 'Phone number: %s\n' % phone output += 'Participant ID: %s\n' % participant_id else: group = db_get_group(contact) if not group: output = 'Media from #%s\n' % contact output += 'Participant ID: %s\n' % participant_id else: # Contact is bound to group chat_id = group output = "Media from %s\n" % participant_id if message_url.startswith("LOCATION=|=|="): locstr, lat, lng = message_url.split("=|=|=") tgbot.send_message(chat_id, output) tgbot.send_location(chat_id, lat, lng) # vcard can be handled in a similar manner elif message_url.startswith("VCARDCONTACT=|=|="): constr, name, cdata = message_url.split("=|=|=") # TODO: but How? else: mime = magic.Magic(mime=True) mime_type = mime.from_file(message_url) if "image" in mime_type: tgbot.send_photo(chat_id, open(message_url, 'rb'), caption=output) elif "video" in mime_type: tgbot.send_video(chat_id, open(message_url, "rb"), caption=output, supports_streaming=True) else: tgbot.send_document(chat_id, open(message_url, 'rb'), caption=output) os.remove(message_url) else: # Text Messages if not contact: # Unknown sender output = 'Message from #unknown\n' output += 'Phone number: %s\n' % phone output += '---------\n' output += message logger.info('received message from unknown number: %s' % phone) else: group = db_get_group(contact) if not group: # Known sender output = 'Message from #%s\n' % contact output += '---------\n' output += message else: # Contact is bound to group chat_id = group output = message logger.info('received message from %s' % contact) # Deliver message through Telegram for chunk in tgutil.split_string(output, 3000): tgbot.send_message(chat_id, chunk)
def to_tg_handler(sender, **kwargs): """Handle signals sent to Telegram. This will involve sending messages through the Telegram bot. Args: phone (str): Phone number that sent the message. message (str): The message received media (boolean): True or False """ phone = kwargs.get('phone') message = kwargs.get('message') media: DataMedia = kwargs.get('media') # Check if known contact contact = get_contact(phone) chat_id = db_get_group(contact) if not chat_id: chat_id = SETTINGS['owner'] if media: # Media Messages type: str = media.get_type() path: str = media.get_args()[0] caption: str = media.get_kwargs()['caption'] caption = replace_phone_with_name(caption) if type == "image": tgbot.bot.send_photo(chat_id, open(path, 'rb'), caption=caption) elif type == "video": tgbot.bot.send_video(chat_id, open(path, "rb"), caption=caption, supports_streaming=True) else: tgbot.bot.send_document(chat_id, open(path, 'rb'), caption=caption) else: message = replace_phone_with_name(message) # Text Messages if not contact: # Unknown sender output = 'Message from #unknown\n' output += 'Phone number: %s\n' % phone output += '---------\n' output += message logger.info('received message from unknown number: %s' % phone) else: group = db_get_group(contact) if not group: # Known sender output = 'Message from #%s\n' % contact output += '---------\n' output += message else: # Contact is bound to group chat_id = group output = message logger.info('received message from %s' % contact) # Deliver message through Telegram for chunk in split_string(output, 3000): tgbot.bot.send_message(chat_id, chunk)