def testSayLoop(self): """should say hello monkey and loop 3 times""" r = twilio.Response() r.append(twilio.Say("Hello Monkey", loop=3)) r = self.strip(r) self.assertEquals( r, '<Response><Say loop="3">Hello Monkey</Say></Response>')
def testPlayHello(self): """should play hello monkey""" r = twilio.Response() r.append(twilio.Play("http://hellomonkey.mp3")) r = self.strip(r) self.assertEqual( r, "<Response><Play>http://hellomonkey.mp3</Play></Response>")
def get_reject_response(self): response = twilio.Response() response.append(twilio.Reject(reason=twilio.Reject.BUSY)) return HttpResponse(u"""<?xml version="1.0" encoding="UTF-8"?>""" + unicode(response), mimetype='text/xml')
def post(self): # validate it is in fact coming from twilio if config.ACCOUNT_SID == self.request.get('AccountSid'): logging.debug( "PHONE request was confirmed to have come from Twilio.") else: logging.error("was NOT VALID. It might have been spoofed!") self.response.out.write("Illegal caller") return # setup the response to get the recording from the caller r = twilio.Response() g = r.append( twilio.Gather(action=config.URL_BASE + "phone/listenforbus", method=twilio.Gather.GET, timeout=10, finishOnKey="#")) g.append(twilio.Say("Welcome to SMS My Bus!")) g.append( twilio.Say( "Enter the bus number using the keypad. Press the pound key to submit.", voice=twilio.Say.MAN, language=twilio.Say.ENGLISH, loop=1)) self.response.out.write(r)
def get(self): # validate it is in fact coming from twilio if config.ACCOUNT_SID == self.request.get('AccountSid'): logging.debug( "PHONE request was confirmed to have come from Twilio.") else: logging.error("was NOT VALID. It might have been spoofed!") self.response.out.write(errorResponse("Illegal caller")) return routeID = self.request.get('Digits') if len(routeID) == 1: routeID = "0" + routeID memcache.add(self.request.get('AccountSid'), routeID) # setup the response to get the recording from the caller r = twilio.Response() g = r.append( twilio.Gather(action=config.URL_BASE + "phone/listenforstop", method=twilio.Gather.GET, timeout=5, numDigits=4, finishOnKey="#")) g.append( twilio.Say( "Enter the four digit stop number using the keypad. Press the pound key to submit.", voice=twilio.Say.MAN, language=twilio.Say.ENGLISH, loop=1)) logging.debug("now asking the caller to enter their stop number...") self.response.out.write(r)
def testRedirectMethod(self): r = twilio.Response() r.append(twilio.Redirect(url="example.com", method="POST")) r = self.strip(r) self.assertEquals( r, '<Response><Redirect method="POST">example.com</Redirect></Response>' )
def testRecordTranscribeCallback(self): """should record with a transcribe and transcribeCallback""" r = twilio.Response() r.append(twilio.Record(transcribeCallback="example.com")) r = self.strip(r) self.assertEquals( r, '<Response><Record transcribeCallback="example.com"/></Response>')
def testRecordActionMethod(self): """should record with an action and a get method""" r = twilio.Response() r.append(twilio.Record(action="example.com", method="GET")) r = self.strip(r) self.assertEquals( r, '<Response><Record action="example.com" method="GET"/></Response>')
def testAddNumberConvience(self): """add a number to a dial, convience method""" r = twilio.Response() d = r.addDial() d.addNumber("1231231234") r = self.strip(r) self.assertEquals( r, '<Response><Dial><Number>1231231234</Number></Dial></Response>')
def index(): response = ["<?xml version='1.0' encoding='utf-8' ?>\n"] r = twilio.Response() s = twilio.Say( 'Hello, Thank you for calling 10 jen. 10 jen develops and provides enterprise grade support for Mongo D B.' ) r.append(s) return r.__repr__()
def testRedirectMethodGetParams(self): r = twilio.Response() r.append( twilio.Redirect(url="example.com?id=34&action=hey", method="POST")) r = self.strip(r) self.assertEquals( r, '<Response><Redirect method="POST">example.com?id=34&action=hey</Redirect></Response>' )
def testRecordMaxlengthFinishTimeout(self): """should record with an maxlength, finishonkey, and timeout""" r = twilio.Response() r.addRecord(timeout=4, finishOnKey="#", maxLength=30) r = self.strip(r) self.assertEquals( r, '<Response><Record finishOnKey="#" maxLength="30" timeout="4"/></Response>' )
def testPlayConvienceMethod(self): """convenience method: should play hello monkey""" r = twilio.Response() r.addPlay("http://hellomonkey.mp3", loop=3) r = self.strip(r) self.assertEqual( r, '<Response><Play loop="3">http://hellomonkey.mp3</Play></Response>' )
def testPlayHelloLoop(self): """should play hello monkey loop""" r = twilio.Response() r.append(twilio.Play("http://hellomonkey.mp3", loop=3)) r = self.strip(r) self.assertEqual( r, '<Response><Play loop="3">http://hellomonkey.mp3</Play></Response>' )
def testAddNumber(self): """add a number to a dial""" r = twilio.Response() d = twilio.Dial() d.append(twilio.Number("1231231234")) r.append(d) r = self.strip(r) self.assertEquals( r, '<Response><Dial><Number>1231231234</Number></Dial></Response>')
def testSayLoopWoman(self): """should say have a woman say hello monkey and loop 3 times""" r = twilio.Response() r.append(twilio.Say("Hello Monkey", loop=3, voice=twilio.Say.WOMAN)) r = self.strip(r) self.assertEquals( r, '<Response><Say loop="3" voice="woman">Hello Monkey</Say></Response>' )
def testAddConferenceConvenceMethod(self): """ add a conference to a dial, conviently""" r = twilio.Response() d = r.addDial() d.addConference("My Room") r = self.strip(r) self.assertEquals( r, '<Response><Dial><Conference>My Room</Conference></Dial></Response>' )
def get(self): # validate it is in fact coming from twilio if config.ACCOUNT_SID == self.request.get('AccountSid'): logging.debug( "PHONE request was confirmed to have come from Twilio.") else: logging.error("was NOT VALID. It might have been spoofed!") self.response.out.write(errorResponse("Illegal caller")) return # pull the route and stopID out of the request body and # pad it with a zero on the front if the message forgot # to include it (that's how they are stored in the DB) routeID = memcache.get(self.request.get('AccountSid')) memcache.delete(self.request.get('AccountSid')) stopID = self.request.get('Digits') if len(stopID) == 3: stopID = "0" + stopID # hack - creating a string out of the details to conform to other interfaces requestArgs = "%s %s" % (routeID, stopID) ## magic ## logging.info('starting the magic... %s' % requestArgs) textBody = api_bridge.getarrivals(requestArgs, 1) logging.debug('phone results are %s' % textBody) # create an event to log the event task = Task(url='/loggingtask', params={ 'phone': self.request.get('Caller'), 'inboundBody': requestArgs, 'sid': self.request.get('SmsSid'), 'outboundBody': textBody, }) task.add('eventlogger') # transform the text a smidge so it can be pronounced more easily... # 1. strip the colons textBody = textBody.replace(':', ' ') # 2. add a space between p-and-m and a-and-m textBody = textBody.replace('pm', 'p m').replace('am', 'a m') logging.debug('transformed results into %s' % textBody) # setup the response r = twilio.Response() r.append( twilio.Say(textBody, voice=twilio.Say.MAN, language=twilio.Say.ENGLISH, loop=1)) self.response.out.write(r)
def testAddConference(self): """ add a conference to a dial""" r = twilio.Response() d = twilio.Dial() d.append(twilio.Conference("My Room")) r.append(d) r = self.strip(r) self.assertEquals( r, '<Response><Dial><Conference>My Room</Conference></Dial></Response>' )
def __call__(self, *args, **kwargs): response = self.f(*args, **kwargs) if isinstance(response, HttpResponse): return response if response is None: response = twilio.Response() return HttpResponse(u"""<?xml version="1.0" encoding="UTF-8"?>\n""" + unicode(response), mimetype='text/xml')
def testSayConvienceMethod(self): """convenience method: should say have a woman say hello monkey and loop 3 times and be in french""" r = twilio.Response() r.addSay("Hello Monkey", loop=3, voice=twilio.Say.MAN, language=twilio.Say.FRENCH) r = self.strip(r) self.assertEquals( r, '<Response><Say language="fr" loop="3" voice="man">Hello Monkey</Say></Response>' )
def testNestedSayPlayPauseConvience(self): """ a gather with a say, play, and pause""" r = twilio.Response() g = r.addGather() g.addSay("Hey") g.addPlay("hey.mp3") g.addPause() r = self.strip(r) self.assertEquals( r, '<Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause/></Gather></Response>' )
def testActionMethod(self): """ Test the action and method parameters on Sms""" r = twilio.Response() r.append( twilio.Sms("Hello", method="POST", action="example.com?id=34&action=hey")) r = self.strip(r) self.assertEquals( r, '<Response><Sms action="example.com?id=34&action=hey" method="POST">Hello</Sms></Response>' )
def testToFromAction(self): """ Test the to, from, and status callback""" r = twilio.Response() r.append( twilio.Sms("Hello, World", to=1231231234, sender=3453453456, statusCallback="example.com?id=34&action=hey")) r = self.strip(r) self.assertEquals( r, '<Response><Sms from="3453453456" statusCallback="example.com?id=34&action=hey" to="1231231234">Hello, World</Sms></Response>' )
def testNestedSayPlayPause(self): """ a gather with a say, play, and pause""" r = twilio.Response() g = twilio.Gather() g.append(twilio.Say("Hey")) g.append(twilio.Play("hey.mp3")) g.append(twilio.Pause()) r.append(g) r = self.strip(r) self.assertEquals( r, '<Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause/></Gather></Response>' )
def configure_playback_call(request, recordedcall_id): """ After the call is started, Twilio makes a request to this method in order to get the TwilML to configure the call. This method returns TwilML which instructs Twilio to playback a previously recorded message. """ from django.shortcuts import get_object_or_404 from models import RecordedCall recorded_call = get_object_or_404(RecordedCall, pk=recordedcall_id) response = twilio.Response() response.addPlay(recorded_call.recording_url) response.addHangup() return response
def get_menu_twilml(): response = twilio.Response() # typos are intentional to get Twilio to pronounce correctly gather = twilio.Gather(action=reverse('handle_menu_response'), ) gather.addSay( """ Press one to enter a quick config code. Press two to pick a group to broadcast to. """, voice=twilio.Say.MAN, ) response.append(gather) response.addSay("I'm sorry. I did not hear a selection. Goodbye.", voice=twilio.Say.MAN) response.addHangup() return response
def get_recording_twilml(url): response = twilio.Response() # typos are intentional to get Twilio to pronounce correctly response.addSay( "After the beep, please reecord a message to be broadcast to all members. This call will end after 60 seconds. Press any key or hang up when you are done.", voice=twilio.Say.MAN) response.addRecord( action=url, maxLength=60, timeout=3, ) response.addSay( "I'm sorry. I did not hear a message to record. Please initiate another call. Goodbye.", voice=twilio.Say.MAN) response.addHangup() return response
def post(self): # validate it is in fact coming from twilio if config.ACCOUNT_SID != self.request.get('AccountSid'): logging.error( "Inbound request was NOT VALID. It might have been spoofed!") self.response.out.write(errorResponse("Illegal caller")) return # who called? and what did they ask for? phone = self.request.get("From") msg = self.request.get("Body") logging.info("New inbound request from %s with message, %s" % (self.request.get('From'), msg)) # look out for the abusers if filter_the_abusers(phone): # don't reply! return # interrogate the message body to determine what to do if msg.lower().find('invite') > -1: # ... an invitation request response = sendInvite(self.request) else: ## magic ## response = api_bridge.getarrivals(msg, 4) # create an event to log the request task = Task(url='/loggingtask', params={ 'phone': self.request.get('From'), 'inboundBody': self.request.get('Body'), 'sid': self.request.get('SmsSid'), 'outboundBody': response, }) task.add('eventlogger') # setup the response SMS #smsBody = "Route %s, Stop %s" % (routeID, stopID) + "\n" + response r = twilio.Response() r.append(twilio.Sms(response)) self.response.out.write(r) return
def get_config_code_twilml(): """ After the call is started, Twilio makes a request to this method in order to get the TwilML to configure the call. This method returns TwilML which instructs Twilio to say a message and and record up to one minute from the call recipient. If no audio is recorded before a 5 second timeout is reached, another message is played instructing the user to initiate another call. """ response = twilio.Response() gather = twilio.Gather(action=reverse('handle_config_code_response'), ) gather.addSay("Please enter your quick config code.", voice=twilio.Say.MAN) response.append(gather) response.addSay( "I'm sorry. I did not hear a code. Please call back. Goodbye.", voice=twilio.Say.MAN) response.addHangup() return response