def twilio_call_end(request, calllog_id): call_log = get_object_or_404(CallLog, id=int(calllog_id)) call_log.status = "ended" call_log.log["end"] = dict(request.POST) call_log.log["end"]["_request"] = get_request_log_info(request) call_log.save() # empty response resp = TwilioResponse() return resp
def twilio_call_end(request, call_id): report = get_object_or_404(WhipReport, id=int(call_id)) report.call_status = "ended" report.call_log["end"] = dict(request.POST) report.call_log["end"]["_request"] = get_request_log_info(request) report.report_result = WhipReportResult.not_entered report.save() # empty response resp = TwilioResponse() return resp
def twilio_call_transfer_ended(request, calllog_id): call_log = get_object_or_404(CallLog, id=int(calllog_id)) call_log.status = "connection-ended" call_log.log["finished"] = dict(request.POST) call_log.log["finished"]["_request"] = get_request_log_info(request) call_log.save() resp = TwilioResponse() resp.say( "Your call to Congress has ended. Thank you for being a good citizen. Goodbye." ) return resp
def twilio_call_transfer_end(request, call_id): report = get_object_or_404(WhipReport, id=int(call_id)) report.call_status = "connection-ended" report.call_log["finished"] = dict(request.POST) report.call_log["finished"]["_request"] = get_request_log_info(request) report.save() resp = TwilioResponse() resp.say( "Your call to Congress has ended. Thank you for being a great citizen. Goodbye." ) return resp
def twilio_call_input(request, calllog_id): call_log = get_object_or_404(CallLog, id=int(calllog_id)) digit = request.POST["Digits"] call_log.log["input"] = dict(request.POST) call_log.log["input"]["_request"] = get_request_log_info(request) resp = TwilioResponse() if digit != "1": # basically an abuse report call_log.log["input"]["response"] = "did-not-request-call" resp.say( "We apologize for the inconvenience. Call 202-558-7227 or visit w w w dot gov track dot u s to report abuse. Good bye." ) resp.hangup() elif settings.DEBUG: resp.say("Site is in debug mode. Call cancelled.") resp.hangup() else: phone = "+1" + "".join( c for c in call_log.target.phone if unicode.isdigit(c)) call_log.log["input"]["response"] = "continue" call_log.log["input"]["transfer_to"] = phone resp.say("Okay. Hold on.") resp.dial( phone, action=request.build_absolute_uri( "/poll/_twilio/call-transfer-end/" + str(call_log.id)), timeout=30, callerId=request.POST["To"], record=True, ) call_log.status = "connecting" call_log.save() return resp
def twilio_call_start(request, call_id): report = get_object_or_404(WhipReport, id=int(call_id)) report.call_status = "picked-up" report.call_log["start"] = dict(request.POST) report.call_log["start"]["_request"] = get_request_log_info(request) report.save() resp = TwilioResponse() resp.say("Hello from Gov Track.") g = resp.gather( action=build_twilio_callback_url(request, report, "call-input"), numDigits=1, timeout=20, ) g.say( "Press one to be connected to the office of %s %s. Press two if you did not request this call. Or simply hang up if you do not want your call to be connected." % (report.target.get_title(), report.target.person.lastname)) resp.say("Oooo too slow. We're going to hang up now.") return resp
def twilio_call_start(request, calllog_id): call_log = get_object_or_404(CallLog, id=int(calllog_id)) call_log.status = "picked-up" call_log.log["start"] = dict(request.POST) call_log.log["start"]["_request"] = get_request_log_info(request) call_log.save() resp = TwilioResponse() resp.say("Hello from Gov Track.") g = resp.gather( action=request.build_absolute_uri("/poll/_twilio/call-input/" + str(call_log.id)), numDigits=1, timeout=20, ) g.say( "Press one to be connected to the office of %s %s. Press two if you did not request this call. Or simply hang up if you do not want your call to be connected." % (call_log.target.get_title(), call_log.target.person.lastname)) resp.say("Oooo too slow. We're going to hang up now.") return resp
def twiml(self, tid, play=False): """Generate a TwiML, either to list tracks, or to play one""" ## Get JSON data to generate the TwiML data = open("%s/%s.json" % (sap.twiml_path, tid), 'r').read() json = loads(data) ## Twilio response response = TwilioResponse() ## If we need to play the track, create a TwiML that just <Play> if play: ## Correct digit, play + list when it's done if int(play) <= len(json['tracks']): response.play(json['tracks'][play]['preview']) self.gather(response, json, tid) ## Else, list tracks again else: response.say("Sorry! Wrong digit, please try again.") self.gather(response, json, tid) ## Otherwise, create a TwiML w/ <Gather> to get user input else: response.say("Hello! Here are a few tracks from %s" % (json['artist'])) self.gather(response, json, tid) ## Render Twilio response return str(response)
def incoming_sms(request, username): try: user = User.objects.get(username=username) except User.DoesNotExist: return HttpResponse(status=404) r = TwilioResponse() # This request is unauthenticated; we'll need to fetch the user's # store directly rather than looking it up via the auth cookie. store = models.TaskStore.get_for_user(user) if not store.twilio_auth_token: log_args = ( "Incoming SMS for %s, but no auth token specified.", user, user, ) logger.warning(*log_args) store.log_error(*log_args) return HttpResponse(status=404) if store.sms_whitelist: incoming_number = re.sub('[^0-9]', '', request.POST['From']) valid_numbers = [ re.sub('[^0-9]', '', n) for n in store.sms_whitelist.split('\n') ] if incoming_number not in valid_numbers: log_args = ( "Incoming SMS for %s, but phone number %s is not " "in the whitelist.", user, incoming_number, ) store.log_error(*log_args) logger.warning( *log_args, extra={ 'data': { 'incoming_number': incoming_number, 'whitelist': valid_numbers, } } ) return HttpResponseForbidden() try: validator = RequestValidator(store.twilio_auth_token) url = request.build_absolute_uri() signature = request.META['HTTP_X_TWILIO_SIGNATURE'] except (AttributeError, KeyError) as e: log_args = ( "Incoming SMS for %s, but error encountered while " "attempting to build request validator: %s.", user, e, ) logger.exception(*log_args) store.log_error(*log_args) return HttpResponseForbidden() if not validator.validate(url, request.POST, signature): log_args = ( "Incoming SMS for %s, but validator rejected message.", user, ) logger.warning(*log_args) store.log_error(*log_args) return HttpResponseForbidden() with git_checkpoint(store, "Incoming SMS", sync=True): from_ = request.POST['From'] body = request.POST['Body'] task_info = body[4:] if not body.lower().startswith('add'): if store.sms_replies >= store.REPLY_ERROR: r.sms("Bad Request: Unknown command.") log_args = ( "Incoming SMS from %s had no recognized command: '%s'." % ( from_, body, ), ) logger.warning(*log_args) store.log_error(*log_args) elif not task_info: log_args = ( "Incoming SMS from %s had no content." % ( from_, body, ), ) logger.warning(*log_args) store.log_error(*log_args) if store.sms_replies >= store.REPLY_ERROR: r.sms("Bad Request: Empty task.") else: task_uuid = str(uuid.uuid4()) task_args = ( ['add'] + shlex_without_quotes(store.sms_arguments) + shlex_without_quotes(task_info) ) task_args.append('uuid:%s' % task_uuid) result = store.client._execute_safe(*task_args) stdout, stderr = result if store.sms_replies >= store.REPLY_ALL: r.sms("Added.") log_args = ( "Added task %s via SMS from %s; message '%s'; " "automatic args: '%s';" "response: '%s'." % ( task_uuid, from_, body, store.sms_arguments, stdout, ), ) logger.info(*log_args) store.log_message(*log_args) return HttpResponse(str(r), content_type='application/xml')