def test_ring(self): ring = Ring(duration=5, answer_call=False) response = Response() response.add_verb(ring) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Ring duration="5" answerCall="False"/></Response>' assert (response.to_bxml() == expected_bxml)
def test_stop_gather(self): stopGather = StopGather() response = Response() response.add_verb(stopGather) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><StopGather/></Response>' assert response.to_bxml() == expected_bxml
def test_record(self): response = Response() record = Record( tag="tag", username="******", password="******", record_complete_url="https://record.url.server/record", record_complete_method="POST", recording_available_url="https://record.url.server/available", recording_available_method="GET", terminating_digits="#", max_duration=90, file_format="mp3", transcribe=False, transcription_available_url= "https://transcribe.url.server/available", transcription_available_method="POST", silence_timeout=90, record_complete_fallback_url="https://test.com", record_complete_fallback_method="GET", fallback_username="******", fallback_password="******") response.add_verb(record) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Record tag="tag" username="******" password="******" recordCompleteUrl="https://record.url.server/record" recordCompleteMethod="POST" recordingAvailableUrl="https://record.url.server/available" recordingAvailableMethod="GET" terminatingDigits="#" maxDuration="90" fileFormat="mp3" transcribe="false" transcriptionAvailableUrl="https://transcribe.url.server/available" transcriptionAvailableMethod="POST" silenceTimeout="90" recordCompleteFallbackUrl="https://test.com" recordCompleteFallbackMethod="GET" fallbackUsername="******" fallbackPassword="******"/></Response>' assert response.to_bxml() == expected_bxml
def test_conference_coach_array(self): conference = Conference("my-conference", call_ids_to_coach=["c-123", "c-456"]) response = Response() response.add_verb(conference) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Conference callIdsToCoach="c-123,c-456">my-conference</Conference></Response>' assert response.to_bxml() == expected_bxml
def handle_end_gather_1(): #URL of recording is in the tag data = json.loads(request.data) response = Response() if "digits" in data.keys() and data["digits"] == "1": #play recording play_recording = PlayAudio( url=data["tag"], username=VOICE_API_USERNAME, password=VOICE_API_PASSWORD ) ask_to_re_record = SpeakSentence( sentence="Would you like to re record? Press 1 if so, otherwise please hangup" ) gather = Gather( first_digit_timeout=15, speak_sentence=ask_to_re_record, max_digits=1, gather_url="/EndGather2" ) response.add_verb(play_recording) response.add_verb(gather) else: hangup = Hangup() response.add_verb(hangup) return response.to_bxml()
def test_stop_stream_bxml_verb(self): expected = '<?xml version="1.0" encoding="UTF-8"?><Response><StopStream name="test_stream"/></Response>' response = Response() stop_stream = StopStream(name='test_stream') response.add_verb(stop_stream) actual = response.to_bxml() assert expected == actual
def test_play_audio(self): response = Response() play_audio_1 = PlayAudio(url="https://audio.url/audio1.wav", username="******", password="******") response.add_verb(play_audio_1) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><PlayAudio username="******" password="******">https://audio.url/audio1.wav</PlayAudio></Response>' assert response.to_bxml() == expected_bxml
def start_gather_game(): """ Callback endpoint that returns BXML for making a gather """ gather = Gather(gather_url="/EndGatherGame", max_digits=2) response = Response() response.add_verb(gather) return response.to_bxml()
def test_speak_sentence(self): response = Response() speak_sentence = SpeakSentence(sentence="Phrase.", voice="kate", locale="en_US", gender="female") expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><SpeakSentence voice="kate" locale="en_US" gender="female">Phrase.</SpeakSentence></Response>' response.add_verb(speak_sentence) assert response.to_bxml() == expected_bxml
def test_forward_xml_with_optional_fields(self): response = Response() forward = Forward(to="+10987654321", from_="+11234567890", call_timeout=100, diversion_treatment="propagate", diversion_reason="away") response.add_verb(forward) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Forward to="+10987654321" callTimeout="100" from="+11234567890" diversionTreatment="propagate" diversionReason="away"/></Response>' assert response.to_bxml() == expected_bxml
def test_gather_speak_sentence_ssml(self): response = Response() speak_sentence = SpeakSentence( sentence= 'Hello. Your number is <say-as interpret-as="telephone">asdf</say-as>, lets play a game. What is 10 + 3. Press the pound key when finished.' ) gather = Gather(speak_sentence=speak_sentence) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Gather><SpeakSentence>Hello. Your number is <say-as interpret-as="telephone">asdf</say-as>, lets play a game. What is 10 + 3. Press the pound key when finished.</SpeakSentence></Gather></Response>' response.add_verb(gather) assert response.to_bxml() == expected_bxml
def test_speak_sentence_SSML(self): response = Response() speak_sentence = SpeakSentence( sentence= '<lang xml:lang="es-MX">Hydrogen</lang> is the most abundant element in the universe.', voice="kate", locale="en_US", gender="female") expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><SpeakSentence voice="kate" locale="en_US" gender="female"><lang xml:lang="es-MX">Hydrogen</lang> is the most abundant element in the universe.</SpeakSentence></Response>' response.add_verb(speak_sentence) assert response.to_bxml() == expected_bxml
def test_start_gather(self): startGather = StartGather(dtmfUrl="https://test.com", dtmfMethod="POST", username="******", password="******", tag="custom tag") response = Response() response.add_verb(startGather) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><StartGather dtmfUrl="https://test.com" dtmfMethod="POST" username="******" password="******" tag="custom tag"/></Response>' assert response.to_bxml() == expected_bxml
def handle_record_complete_callback(): #Loops endlessly until the recording is available pause = Pause( duration=3 ) redirect = Redirect( redirect_url="/RecordCompleteCallback" ) response = Response() response.add_verb(pause) response.add_verb(redirect) return response.to_bxml()
def handle_end_gather_2(): data = json.loads(request.data) response = Response() if "digits" in data.keys() and data["digits"] == "1": redirect = Redirect( redirect_url="/RecordCallback" ) response.add_verb(redirect) else: hangup = Hangup() response.add_verb(hangup) return response.to_bxml()
def test_transfer(self): response = Response() phone1 = PhoneNumber( number="+11234567891", transfer_answer_url="https://transfer.com/answer", transfer_answer_method="POST", username="******", password="******", tag="tag", transfer_disconnect_method="POST", transfer_disconnect_url="https://transfer.com/disconnect", transfer_answer_fallback_url="https://test.com", transfer_answer_fallback_method="GET", fallback_username="******", fallback_password="******") phone1_bxml = '<PhoneNumber transferAnswerUrl="https://transfer.com/answer" transferAnswerMethod="POST" username="******" password="******" tag="tag" transferDisconnectMethod="POST" transferDisconnectUrl="https://transfer.com/disconnect" transferAnswerFallbackUrl="https://test.com" transferAnswerFallbackMethod="GET" fallbackUsername="******" fallbackPassword="******">+11234567891</PhoneNumber>' phone2 = PhoneNumber(number="+11234567892") phone2_bxml = '<PhoneNumber>+11234567892</PhoneNumber>' transfer = Transfer( transfer_caller_id="+15555555555", call_timeout=50, tag="tag", transfer_complete_url="https://transcribe.url.server/complete", transfer_complete_method="POST", username="******", password="******", diversion_treatment="propagate", diversion_reason="away", phone_numbers=[phone1, phone2], transfer_complete_fallback_url="https://test.com", transfer_complete_fallback_method="GET", fallback_username="******", fallback_password="******") response.add_verb(transfer) expected_bxml = f'<?xml version="1.0" encoding="UTF-8"?><Response><Transfer transferCallerId="+15555555555" callTimeout="50" tag="tag" transferCompleteUrl="https://transcribe.url.server/complete" transferCompleteMethod="POST" username="******" password="******" diversionTreatment="propagate" diversionReason="away" transferCompleteFallbackUrl="https://test.com" transferCompleteFallbackMethod="GET" fallbackUsername="******" fallbackPassword="******">{phone1_bxml}{phone2_bxml}</Transfer></Response>' print(response.to_bxml()) print(expected_bxml) assert response.to_bxml() == expected_bxml
def handle_voice_callback_inbound(): sentence = 'Hello, let\'s play a game. What is 9 + 2' voice = 'kate' speak_sentence = SpeakSentence(sentence, voice) gather = Gather(max_digits=2, first_digit_timeout=10, gather_url='/Callbacks/Voice/Gather', speak_sentence=speak_sentence) response = Response() response.add_verb(gather) bxml = response.to_bxml() print(bxml) return bxml
def test_redirect(self): response = Response() redirect = Redirect(redirect_url="http://flow.url/newFlow", redirect_method="POST", tag="tag", username="******", password="******", redirect_fallback_url="https://test.com", redirect_fallback_method="GET", fallback_username="******", fallback_password="******") response.add_verb(redirect) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Redirect redirectUrl="http://flow.url/newFlow" redirectMethod="POST" tag="tag" username="******" password="******" redirectFallbackUrl="https://test.com" redirectFallbackMethod="GET" fallbackUsername="******" fallbackPassword="******"/></Response>' assert response.to_bxml() == expected_bxml
def test_start_stream_bxml_verb(self): expected = '<?xml version="1.0" encoding="UTF-8"?><Response><StartStream destination="https://www.test.com/stream" name="test_stream" tracks="inbound" streamEventUrl="https://www.test.com/event" streamEventMethod="POST" username="******" password="******"/></Response>' response = Response() start_stream = StartStream(destination='https://www.test.com/stream', name='test_stream', tracks='inbound', streamEventUrl='https://www.test.com/event', streamEventMethod='POST', username='******', password='******') response.add_verb(start_stream) actual = response.to_bxml() assert expected == actual
def handle_voice_callback_gather(): data = json.loads(request.data) digits = data['digits'] success_file = 'https://bw-demo.s3.amazonaws.com/tada.wav' fail_file = 'https://bw-demo.s3.amazonaws.com/fail.wav' audio_uri = success_file if digits == '11' else fail_file play_audio = PlayAudio(url=audio_uri) hangup = Hangup() response = Response() response.add_verb(play_audio) response.add_verb(hangup) bxml = response.to_bxml() print(bxml) return bxml
def end_gather_transfer(): """ A method that receives a Gather callback from Bandwidth and creates a Transfer response """ data = json.loads(request.data) phone_number_string = "+1" + data["digits"] phone_number = PhoneNumber(number=phone_number_string) transfer = Transfer(transfer_caller_id=data["from"], phone_numbers=[phone_number]) response = Response() response.add_verb(transfer) return response.to_bxml()
def handle_voice_callback(): ring_audio = PlayAudio( url="https://www.soundjay.com/phone/telephone-ring-01a.wav" ) leave_voicemail = SpeakSentence( sentence="Please leave a message after the beep. Your time limit is 3 minutes. Press # to stop the recording early" ) redirect = Redirect( redirect_url="/RecordCallback" ) response = Response() response.add_verb(ring_audio) response.add_verb(leave_voicemail) response.add_verb(redirect) return response.to_bxml()
def start_gather_transfer(): """ A method that returns BXML for creating a gather on an outbound call """ speak_sentence = SpeakSentence( sentence= "Who do you want to transfer this call to? Enter the 10 digit phone number", voice="susan", locale="en_US", gender="female") gather = Gather(gather_url="/EndGatherTransfer", max_digits=10, speak_sentence=speak_sentence) response = Response() response.add_verb(gather) return response.to_bxml()
def handle_record_callback(): beep_audio = PlayAudio( url="https://www.soundjay.com/button/beep-01a.wav" ) start_recording = Record( record_complete_url="/RecordCompleteCallback", record_complete_method="POST", recording_available_url="/RecordingAvailableCallback", recording_available_method="POST", max_duration=180, terminating_digits="#" ) response = Response() response.add_verb(beep_audio) response.add_verb(start_recording) return response.to_bxml()
def handle_recording_gather_1(): #Recording URL is in the "tag" of the data data = json.loads(request.data) ask_to_hear_recording = SpeakSentence( sentence="Your recording is now available. If you'd like to hear your recording, press 1, otherwise please hangup" ) gather = Gather( first_digit_timeout=15, speak_sentence=ask_to_hear_recording, max_digits=1, gather_url="/EndGather1", tag=data["tag"] ) response = Response() response.add_verb(gather) return response.to_bxml()
def test_start_recording(self): response = Response() record = StartRecording( tag="tag", username="******", password="******", recording_available_url="https://record.url.server/available", recording_available_method="GET", file_format="mp3", multi_channel=True, transcribe=False, transcription_available_url= "https://transcribe.url.server/available", transcription_available_method="POST") response.add_verb(record) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><StartRecording tag="tag" username="******" password="******" recordingAvailableUrl="https://record.url.server/available" recordingAvailableMethod="GET" fileFormat="mp3" multiChannel="true" transcribe="false" transcriptionAvailableUrl="https://transcribe.url.server/available" transcriptionAvailableMethod="POST"/></Response>' assert response.to_bxml() == expected_bxml
def test_gather_play_audio(self): response = Response() play_audio_1 = PlayAudio(url="https://audio.url/audio1.wav") gather = Gather(gather_url="https://gather.url/nextBXML", gather_method="POST", terminating_digits="#", tag="tag", max_digits=20, inter_digit_timeout=50, username="******", password="******", first_digit_timeout=10, repeat_count=3, play_audio=play_audio_1) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Gather gatherUrl="https://gather.url/nextBXML" gatherMethod="POST" terminatingDigits="#" tag="tag" maxDigits="20" interDigitTimeout="50" username="******" password="******" firstDigitTimeout="10" repeatCount="3"><PlayAudio>https://audio.url/audio1.wav</PlayAudio></Gather></Response>' response.add_verb(gather) assert response.to_bxml() == expected_bxml
def test_gather_no_nested(self): response = Response() gather = Gather(gather_url="https://gather.url/nextBXML", gather_method="POST", terminating_digits="#", tag="tag", max_digits=20, inter_digit_timeout=50, username="******", password="******", first_digit_timeout=10, repeat_count=3, gather_fallback_url="https://test.com", gather_fallback_method="GET", fallback_username="******", fallback_password="******") response.add_verb(gather) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Gather gatherUrl="https://gather.url/nextBXML" gatherMethod="POST" terminatingDigits="#" tag="tag" maxDigits="20" interDigitTimeout="50" username="******" password="******" firstDigitTimeout="10" repeatCount="3" gatherFallbackUrl="https://test.com" gatherFallbackMethod="GET" fallbackUsername="******" fallbackPassword="******"/></Response>' assert response.to_bxml() == expected_bxml
def end_gather_game(): """ Callback endpoint that expects a gather callback """ data = json.loads(request.data) digits = int(data["digits"]) audio_url = None if digits == 12: audio_url = CORRECT_URL else: audio_url = INCORRECT_URL play_audio = PlayAudio(url=audio_url) response = Response() response.add_verb(play_audio) return response.to_bxml()
def test_gather_with_speak_sentence(self): response = Response() speak_sentence = SpeakSentence(sentence="Phrase.", voice="kate", locale="en_US", gender="female") gather = Gather(gather_url="https://gather.url/nextBXML", gather_method="POST", terminating_digits="#", tag="tag", max_digits=20, inter_digit_timeout=50, username="******", password="******", first_digit_timeout=10, repeat_count=3, speak_sentence=speak_sentence) expected_bxml = '<?xml version="1.0" encoding="UTF-8"?><Response><Gather gatherUrl="https://gather.url/nextBXML" gatherMethod="POST" terminatingDigits="#" tag="tag" maxDigits="20" interDigitTimeout="50" username="******" password="******" firstDigitTimeout="10" repeatCount="3"><SpeakSentence voice="kate" locale="en_US" gender="female">Phrase.</SpeakSentence></Gather></Response>' response.add_verb(gather) assert response.to_bxml() == expected_bxml