Skip to content

Instantly share code, notes, and snippets.

@dtolb
Created May 19, 2020 16:29
Show Gist options
  • Save dtolb/6989ba6d237afd91e980a8004e1d9d2d to your computer and use it in GitHub Desktop.
Save dtolb/6989ba6d237afd91e980a8004e1d9d2d to your computer and use it in GitHub Desktop.
Python Forward with Gather Ring Check
from flask import Flask, request
from bandwidth.voice.bxml.response import Response
from bandwidth.voice.bxml.verbs import SpeakSentence, Hangup, Transfer, PhoneNumber
import json
app = Flask(__name__)
FIRST_FORWARD_NUMBER = "+"
SECOND_FORWARD_NUMBER = "+"
@app.route("/", methods=["GET"])
def home_page():
return "Hello world"
@app.route("/Callbacks/Voice/Inbound", methods=["POST"])
def handle_voice_callback_inbound():
data = json.loads(request.data)
print(data)
sentence = 'Hello, we are going to forward this call, then when that call ends, connect you to another call'
voice = 'kate'
speak_sentence = SpeakSentence(sentence, voice)
phone_number = PhoneNumber(
number=FIRST_FORWARD_NUMBER,
# transfer_answer_url="/Callbacks/Voice/transferAnswer"
transfer_answer_url="/Callbacks/Voice/transferAnswer2"
)
transfer = Transfer(
phone_numbers=[phone_number],
transfer_complete_url="/Callbacks/Voice/transferComplete"
)
response = Response()
response.add_verb(speak_sentence)
response.add_verb(transfer)
bxml = response.to_bxml()
print(bxml)
return bxml
@app.route("/Callbacks/Voice/transferAnswer", methods=["POST"])
def handle_transfer_answer():
data = json.loads(request.data)
print(data)
sentence = 'Hello, we are connecting your call'
voice = 'kate'
speak_sentence = SpeakSentence(sentence, voice)
response = Response()
response.add_verb(speak_sentence)
bxml = response.to_bxml()
print(bxml)
return bxml
@app.route("/Callbacks/Voice/transferAnswer2", methods=["POST"])
def handle_transfer_answer2():
data = json.loads(request.data)
print(data)
sentence = 'Hello, you have an inbound call, press 1 to accept, 2 to end'
voice = 'kate'
speak_sentence = SpeakSentence(sentence, voice)
gather = Gather(max_digits=1,
first_digit_timeout=10,
gather_url='/Callbacks/Voice/GatherAnswer',
speak_sentence=speak_sentence)
response = Response()
response.add_verb(gather)
bxml = response.to_bxml()
print(bxml)
return bxml
@app.route("/Callbacks/Voice/GatherAnswer", methods=["POST"])
def handle_transfer_answer_gather():
data = json.loads(request.data)
print(data)
voice = 'kate'
response = Response()
digits = data["digits"]
if digits == 1:
# connect the call
sentence = "connecting your call"
speak_sentence = SpeakSentence(sentence, voice)
response.add_verb(speak_sentence)
else:
# hangup
sentence = "ending your call"
hangup = Hangup()
speak_sentence = SpeakSentence(sentence, voice)
response.add_verb(speak_sentence)
response.add_verb(hangup)
bxml = response.to_bxml()
print(bxml)
return bxml
@app.route("/Callbacks/Voice/transferComplete", methods=["POST"])
def handle_transfer_complete():
data = json.loads(request.data)
print(data)
sentence = 'Hello, we are going to forward this call part 2'
voice = 'kate'
speak_sentence = SpeakSentence(sentence, voice)
phone_number = PhoneNumber(
number=SECOND_FORWARD_NUMBER
)
transfer = Transfer(
phone_numbers=[phone_number],
transfer_complete_url="/Callbacks/Voice/secondComplete"
)
response = Response()
response.add_verb(speak_sentence)
response.add_verb(transfer)
bxml = response.to_bxml()
print(bxml)
return bxml
@app.route("/Callbacks/Voice/secondComplete", methods=["POST"])
def handle_transfer_complete_again():
data = json.loads(request.data)
print(data)
sentence = 'End of the forward tree'
voice = 'kate'
speak_sentence = SpeakSentence(sentence, voice)
hangup = Hangup()
response = Response()
response.add_verb(speak_sentence)
response.add_verb(hangup)
bxml = response.to_bxml()
print(bxml)
return bxml
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment