Skip to content

Commit

Permalink
Add support and tests for handling replies from wait_for
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Giesler committed Apr 13, 2015
1 parent 21dc4e1 commit abed592
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
49 changes: 48 additions & 1 deletion vumi_twilio_api/tests/test_twilio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_make_call_parsing_gather_verb_non_defaults(self):
self.assertEqual(reply['to_addr'], '+54321')

@inlineCallbacks
def test_make_all_parsing_gather_verb_subverbs(self):
def test_make_call_parsing_gather_verb_subverbs(self):
response = twiml.Response()
with response.gather() as g:
g.play('test_url')
Expand All @@ -462,6 +462,53 @@ def test_make_all_parsing_gather_verb_subverbs(self):
self.assertEqual(
reply2['helper_metadata']['voice']['speech_url'], 'test_url2')

@inlineCallbacks
def test_make_call_parsing_gather_verb_with_reply(self):
response = twiml.Response()
response.gather(action='reply.xml')
self.twiml_server.add_response('default.xml', response)
response = twiml.Response()
response.play('test_url')
self.twiml_server.add_response('reply.xml', response)

yield self._twilio_client_create_call(
'default.xml', from_='+12345', to='+54321')
[msg] = yield self.app_helper.wait_for_dispatched_outbound(1)
yield self.app_helper.dispatch_event(self.app_helper.make_ack(msg))
[_, rep] = yield self.app_helper.wait_for_dispatched_outbound(1)
reply = rep.reply('123')
yield self.app_helper.dispatch_inbound(reply)
[_, _, play] = yield self.app_helper.wait_for_dispatched_outbound(1)
self.assertEqual(
play['helper_metadata']['voice']['speech_url'], 'test_url')
request = self.twiml_server.requests[-1]
self.assertEqual(request['filename'], 'reply.xml')
self.assertEqual(request['request'].method, 'POST')
self.assertEqual(request['request'].args['Digits'], ['123'])

@inlineCallbacks
def test_make_call_parsing_gather_verb_with_reply_get_request(self):
response = twiml.Response()
response.gather(action='reply.xml', method='GET')
self.twiml_server.add_response('default.xml', response)
response = twiml.Response()
response.play('test_url')
self.twiml_server.add_response('reply.xml', response)

yield self._twilio_client_create_call(
'default.xml', from_='+12345', to='+54321')
[msg] = yield self.app_helper.wait_for_dispatched_outbound(1)
yield self.app_helper.dispatch_event(self.app_helper.make_ack(msg))
[_, rep] = yield self.app_helper.wait_for_dispatched_outbound(1)
reply = rep.reply('123')
yield self.app_helper.dispatch_inbound(reply)
[_, _, play] = yield self.app_helper.wait_for_dispatched_outbound(1)
self.assertEqual(
play['helper_metadata']['voice']['speech_url'], 'test_url')
request = self.twiml_server.requests[-1]
self.assertEqual(request['filename'], 'reply.xml')
self.assertEqual(request['request'].method, 'GET')

@inlineCallbacks
def test_receive_call(self):
response = twiml.Response()
Expand Down
30 changes: 26 additions & 4 deletions vumi_twilio_api/twilio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ def _request_data_from_session(self, session):
}

@inlineCallbacks
def _get_twiml_from_client(self, session):
data = self._request_data_from_session(session)
def _get_twiml_from_client(self, session, data=None):
if data is None:
data = self._request_data_from_session(session)
twiml_raw = yield self._http_request(
session['Url'], session['Method'], data)
if twiml_raw.code < 200 or twiml_raw.code >= 300:
Expand All @@ -141,13 +142,14 @@ def _get_twiml_from_client(self, session):

@inlineCallbacks
def _handle_connected_call(
self, session_id, session, status='in-progress'):
self, session_id, session, status='in-progress', twiml=None):
# TODO: Support sending ForwardedFrom parameter
# TODO: Support sending CallerName parameter
# TODO: Support sending geographic data parameters
session['Status'] = status
self.session_manager.save_session(session_id, session)
twiml = yield self._get_twiml_from_client(session)
if twiml is None:
twiml = yield self._get_twiml_from_client(session)
for verb in twiml:
if not verb:
continue
Expand Down Expand Up @@ -193,6 +195,26 @@ def _send_message(self, url, session, session_event=None, wait_for=None):
from_addr_type=TransportUserMessage.AT_MSISDN,
helper_metadata=helper_metadata)

@inlineCallbacks
def consume_user_message(self, message):
# At the moment there is no way to determine whether or not a message
# is the result of a wait_for or just a single digit, so if the Gather
# data exists inside the current session data, then we assume that it
# is the result of a Gather
# TODO: Fix this
session = yield self.session_manager.load_session(message['from_addr'])
if session.get('Gather_Action') and session.get('Gather_Method'):
data = self._request_data_from_session(session)
data['Digits'] = message['content']
twiml = yield self._get_twiml_from_client({
'Url': session['Gather_Action'],
'Method': session['Gather_Method'],
'Fallback_Url': None,
'Fallback_Method': None, },
data=data)
yield self._handle_connected_call(
message['from_addr'], session, twiml=twiml)

@inlineCallbacks
def consume_ack(self, event):
message_id = event['user_message_id']
Expand Down

0 comments on commit abed592

Please sign in to comment.