def incoming_message(request):
    data = request.POST
    if data['secret'] != 'ZFG4P3KLHTRLXT4DPFAZWC6QUMMKANX0':
        return HttpResponseForbidden
    try:
        msg = Message.get(telerivet_id=data['id'])
        # we've already received this message. return 200 ok
        return {}
    except Process.DoesNotExist:
        msg = Message(
            telerivet_id=data['id'],
            sms_to=data['to_number'],
            sms_from=data['from_number'],
            sms_body=data['content'],
            sms_status=data['status'],
            sms_direction=data['direction'],
            raw_data=data
        )
    try:
        convo = Conversation.get(external_number=data['from_number'], is_active=True)
        if convo.telerivet_number != data['to_number']:
            convo.telerivet_number = data['to_number']
    except Process.DoesNotExist:
        convo = Conversation(
            external_number=data['from_number'],
            telerivet_number=data['to_number'],
            is_active=True
        )
    convo.save()

    msg.conversation = convo
    msg.save()
def respond_to_message(message, content, close_convo=False):
    convo = message.conversation
    if close_convo:
        convo.is_active = False
        convo.save()
    msg = Message(
        sms_to=message.sms_from,
        sms_body=content,
        conversation=convo
    )
    msg.save()
    return send_msg(msg)
def send_new_message(to, content):
    try:
        convo = Conversation.get(external_number=to)
    except Process.DoesNotExist:
        convo = Conversation(
            external_number=to,
            is_active=True
        )
        convo.save()
    msg = Message(
        sms_to=to,
        sms_body=content,
        conversation=convo
    )
    msg.save()
    return send_msg(msg)
 def action(self):
     pending_messages = Message.filter(sent=False, sms_direction='')
     for message in pending_messages:
         try:
             new_msg = send_new_message(message.sms_to, message.sms_body)
         except:
             # something happened and we got an error... do nothing with this message.
             pass
def outgoing_message(request):
    data = request.POST
    if data['secret'] != 'BTOIJaktfp6SopAQBQdGXFE7vBPPl5':
        return HttpResponseForbidden
    try:
        msg = Message.get(telerivet_id=data['id'])
        # we sent this message. let's update.
        # only a destination number and content are required to send a message
        msg.sms_from = data['from_number']
        msg.sms_status = data['status']
        msg.sms_direction = data['direction']
        msg.raw_data = data
    except Process.DoesNotExist:
        # this outgoing message wasn't sent via our api.
        # create new
        msg = Message(
            telerivet_id=data['id'],
            sms_to=data['to_number'],
            sms_from=data['from_number'],
            sms_body=data['content'],
            sms_status=data['status'],
            sms_direction=data['direction'],
            sent=True,
            date_sent=data['time_created'],
            raw_data=data
        )
    try:
        convo = Conversation.get(external_number=data['from_number'], is_active=True)
        if convo.telerivet_number != data['to_number']:
            convo.telerivet_number = data['to_number']
    except Process.DoesNotExist:
        convo = Conversation(
            external_number=data['from_number'],
            telerivet_number=data['to_number'],
            is_active=True
        )
    convo.save()

    msg.conversation = convo
    msg.save()
def generate_outgoing_message(to, content):
    message = Message()
    message.sms_to = to
    message.sms_body = content
    message.save()