コード例 #1
0
ファイル: messages.py プロジェクト: 3taps/MessageMe
def send(session, topic_id, sender_name=None, message=None):
    """ Send a message.
    """
    raise UnauthorizedException() # Disable for now.

    logger.debug("core.api.messages.send(" +
                 'session=%s, topic_id=%s, sender_name=%s, message=%s)'
                 % (repr(session), repr(topic_id), repr(sender_name),
                    repr(message)))

    try:
        if session == None or topic_id == None or message == None:
            raise InvalidParametersException()

        sessionHandler.validate(session)

        sender = sessionHandler.get_user(session)

        try:
            topic = Topic.objects.get(id=topic_id)
        except Topic.DoesNotExist:
            raise NoSuchTopicException()

        if not topic.active:
            raise InactiveTopicException()

        recipient    = topic.user
        phone_number = recipient.phone_number

        rateLimiter.ensure_phone_number_below_rate_limit(phone_number)

        if messageHandler.handle_special_message(sender, recipient, topic,
                                                 message):
            msg = None
        else:
            msg = messageHandler.send_message(sender, recipient, topic,
                                              sender_name, message)
            rateLimiter.sms_message_sent_to(phone_number)

        if msg != None:
            return msg.to_dict()
        else:
            return None
    except RateLimitReachedException:
        raise
    except:
        traceback.print_exc()
        raise
コード例 #2
0
ファイル: twilio_gateway.py プロジェクト: 3taps/MessageMe
def receive_sms(from_phone, to_phone, sms_message):
    """ Respond to an incoming SMS message from Twilio.

        'from_phone' is the phone number the SMS message was sent from,
        'to_phone' is the phone number the message was sent to, and
        'sms_message' is the text of the incoming SMS message.

        We process the SMS message, attempting to link it to an existing SMS
        channel so we can identify the recipient of the message.

        Upon completion, we return a string to use as a reply to send back to
        the 'from_phone', or None if no reply should be sent.

        Note that we raise an InvalidPhoneNumberException if either phone
        number cannot be parsed.
    """
    logger.debug('twilio.receive_sms(' +
                 'from_phone="%s", to_phone="%s", sms_message="%s")'
                 % (from_phone, to_phone, sms_message))

    # Convert the phone numbers to E.164 format.

    from_phone = utils.format_phone_number(from_phone)
    to_phone   = utils.format_phone_number(to_phone)

    # Find the User record associated with the sender's phone number.

    try:
        sender = User.objects.get(phone_number=from_phone)
    except User.DoesNotExist:
        return settings.SMS_TEXT_UNKNOWN_USER

    # Find an SMS channel which uses the phone number that the message was sent
    # to, and which was sending messages to the given user.

    possible_channels = TwilioSMSChannel.objects.filter(
                                phone_number__number=to_phone,
                                sender=sender)

    if len(possible_channels) == 0:
        return settings.SMS_TEXT_CONVERSATION_CLOSED
    if len(possible_channels) > 1:
        return settings.SMS_TEXT_UNABLE_TO_PROCESS_REPLY # Should never happen.

    found_channel = possible_channels[0]

    # If we get here, we've found the SMS channel that was sending messages to
    # this user.  This tells us the conversation this message was about, and
    # hence the topic.

    conversation = found_channel.conversation
    topic        = conversation.topic

    # If the conversation has been stopped, don't try to forward the reply.
    # Instead, return an appropriate message back to the sender.

    if conversation.stopped:
        return settings.SMS_TEXT_CONVERSATION_CLOSED

    # Calculate the recipient for the reply.  This is the other party in the
    # conversation.

    if sender == conversation.user_1:
        recipient = conversation.user_2
    else:
        recipient = conversation.user_1

    # If we're going to be forwarding this message via SMS, check that the
    # recipient hasn't reached their rate limit.

    if recipient.phone_number not in [None, ""] and recipient.verified:
        if not rateLimiter.is_phone_number_below_rate_limit(
                                                    recipient.phone_number):
            return settings.SMS_TEXT_RATE_LIMIT_EXCEEDED

    # Process the incoming SMS.

    from messageme.core.lib import messageHandler

    if not messageHandler.handle_special_message(sender, recipient, topic,
                                                 sms_message):
        # Pass an ordinary SMS message on to the recipient.
        msg = messageHandler.send_message(sender, recipient, topic, None,
                                          sms_message)

    return None # Don't send a message back to the 'from_phone' number.