I am currently working on a Flask application with Twilio integration, and I am facing an issue when handling the case where a call is not answered. The expected behavior is to return a specific message when the call is not answered or the line is busy, but instead, I am getting the default error message "We are sorry, but an application error occurred. Goodbye."
Here is a simplified version of my Flask code:
# Flask code
# ... (other imports)
from twilio.twiml.voice_response import VoiceResponse
app = Flask(__name__)
@app.route("/webhook/inbound-call/answer", methods=['POST'])
def inbound_call():
try:
response = VoiceResponse()
caller = request.form.get('From')
called = request.form.get('To')
if caller != 'unknown' and called != 'unknown':
with response.dial(action=f"/webhook/inbound-call/status/{caller}/{called}") as dial:
dial.number('923355667703')
else:
response.say("The number you dialed is busy please try again")
return str(response)
except Exception as exe:
print(f"Webhook: Inbound-call/answer error: %s" % exe)
return str(exe)
@app.route('/webhook/inbound-call/status/<caller>/<called>', methods=['POST'])
def inbound_call_status(caller, called):
try:
call_status = request.form.get('CallStatus')
response = VoiceResponse()
if call_status == 'completed':
response.say('Call successfully completed')
elif call_status == 'busy' or call_status == 'no-answer':
response.say('Call not answered or line is busy')
else:
response.say('Unexpected call status')
return str(response)
except Exception as exe:
print(f"Webhook: Inbound-call/status error: %s" % exe)
return str(exe)
if __name__ == '__main__':
app.run(debug=True)
Despite handling different call statuses in the inbound_call_status endpoint, I still encounter the unexpected error message. I want to make sure that the correct response is returned when the call is not answered. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated.