def handle_foursquare_task(foursquare_task_object, sender):
    foursquare_client = FoursquareClient()
    twilio_client = TwilioClient()

    if not foursquare_task_object['query']:
        twilio_client.send_sms(settigns.TWILIO_NUMBER, sender, SMS_TEMPLATES['MALFORMED_QUERY']['foursquare'])
        return HttpResponseBadRequest("Query malformed")

    task_namespace = _get_task_namespace_object(foursquare_task_object['flags'])

    query_with_options = {'query':foursquare_task_object['query'], 'near':task_namespace.near[0]}
    venues = foursquare_client.query_foursquare_venues(query_with_options, task_namespace.depth[0])
    if venues:
        for venue in venues:
            contact = venue.get('contact')
            location = venue.get('location')
            if location.get('formattedAddress'):
                sms_body = SMS_TEMPLATES['QUERY_RESPONSE']['foursquare'].format(' '.join(location.get('formattedAddress', 'No Address')) + ', ' + contact.get('phone','No phone'))
                twilio_client.send_sms(settings.TWILIO_NUMBER, sender, sms_body)
            else:
                sms_body = SMS_TEMPLATES['QUERY_RESPONSE']['foursquare'].format(location.get('address','No Address') + ', at ' + location.get('crossStreet','No cross street') + ', ' + contact.get('phone','No phone'))
                twilio_client.send_sms(settings.TWILIO_NUMBER, sender, sms_body)
        return HttpResponse("Success")
    else:
        twilio_client.send_sms(settings.TWILIO_NUMBER, sender, SMS_TEMPLATES['NO_RESPONSE']['foursquare'])
        return HttpResponseNotFound("Nothing returned for that query")
示例#2
0
def task(request):
    incoming_text = Text.objects.create(
        body = request.POST.get('Body', 'No message body').lower(),
        message_sid = request.POST.get('MessageSid'),
        from_number = request.POST.get('From'),
        to_number = request.POST.get('To'),
    )
    twilio_client = TwilioClient()
    # Split out the pre-flag text, this will indicate the task to accomplish and any required keywords
    parsed_task = _parse_task_parts(incoming_text)
    #Handle foursquare task
    if parsed_task['task_type'] == 'foursquare':
        return foursquare_wrapper.handle_foursquare_task(parsed_task, incoming_text.from_number)
    else:
        twilio_client.send_sms(settings.TWILIO_NUMBER, incoming_text.from_number, SMS_TEMPLATES['WRONG_TASK_TYPE'])
        return HttpResponseBadRequest("That task isn't supported yet. Just foursquare for now!")
    return HttpResponse('All branches should be handled, we shouldn\'t get here.')