@csrf_exempt
def receive(request):
    if (request.method == 'POST'):
        from_number = request.POST['From']
        to_number = request.POST['To']
        message = request.POST['Body']
        existing_conversations = Conversation.objects.filter(external=from_number, internal=to_number)
        if existing_conversations:
            conversation = existing_conversations[0]
        else:
            owners = PhoneOwnership.objects.filter(number=to_number)
            if owners:
                conversation = Conversation(external=from_number, internal=to_number, user=owners[0].user)
                conversation.save()
            else:
                return HttpResponse('Bad request: phone number not owned', status=400)
        messages = Message(msg_content=message, outward=False, conversation=conversation)
        messages.save()
        return HttpResponse('Message received', status=200)
    else:
        return HttpResponse('Method not allowed', status=405)
@csrf_exempt
def receive(request):
    if (request.method == 'POST'):
        from_number = request.POST['From']
        to_number = request.POST['To']
        message = request.POST['Body']
        existing_conversations = Conversation.objects.filter(external=from_number, internal=to_number)
        if existing_conversations:
            conversation = existing_conversations[0]
        else:
            owners = PhoneOwnership.objects.filter(number=to_number)
            if owners:
                conversation = Conversation(external=from_number, internal=to_number, user=owners[0].user)
                conversation.save()
            else:
                return HttpResponse('Bad request: phone number not owned', status=400)
        messages = Message(msg_content=message, outward=False, conversation=conversation)
        messages.save()
        return HttpResponse('Message received', status=200)
    else:
        return HttpResponse('Method not allowed', status=405)