Esempio n. 1
0
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)

    if not contact:
        # Unknown sender
        output = 'Message from #unknown\n'
        output += 'Phone number: %s\n' % phone

        logger.info('received message from unknown number: %s' % phone)

    else:
        # Known sender
        output = 'Message from #%s\n' % contact

        logger.info('received message from %s' % contact)

    output += '---------\n'
    output += message

    # Deliver message through Telegram
    for chunk in tgutil.split_string(output, 3000):
        tgbot.send_message(SETTINGS['owner'], chunk)
Esempio n. 2
0
def to_wa_handler(sender, **kwargs):
    """Handle signals sent to Whatsapp.

    This will involve sending messages through the Whatsapp bot.

    Args:
        contact (str): Name of the contact to send the message to.
        message (str): The message to send
    """
    contact = kwargs.get('contact')
    message = kwargs.get('message')

    # Check if known contact
    phone = get_phone(contact)

    if not phone:
        # Abort
        tgbot.send_message(SETTINGS['owner'],
                           'Unknown contact: "%s"' % contact)

        return

    logger.info('sending message to %s (%s)' % (contact, phone))

    wabot.send_msg(phone=phone, message=message)
Esempio n. 3
0
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)