Reputation: 1725
I want to call my Twilio number from my cellphone, Twilio recognizes my number, rejects the call and then calls me back. Here is the code:
@application.route("/test", methods=['GET', 'POST'])
def test():
whitelist = ['81808730xxxx', '44753810xxxx', '+44753810xxxx', '+44792834xxxx', '44792834xxxx']
call_from = request.values.get('From', 'None')
if call_from in whitelist:
# "<Response><Reject /></Response>"
resp = twilio.twiml.Response()
resp.reject()
time.sleep( 10 )
account_sid = "account_sid"
auth_token = "account_auth_token"
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(to=call_from, from_="+1646480xxxx", url="https://zshizuka.herokuapp.com/gather")
print call.sid
else:
resp = twilio.twiml.Response()
call_to = "+44792834xxxx"
resp.dial(call_to)
#return json.dumps({'success':True}), 200
return str(resp)
This produces the dreaded response: "We are sorry a system error has occurred. Goodbye".
If, however, I dial the number and hangup after the first ring it works perfectly. So I am assuming that the problem is with the reject verb. As you can see I have tried with and without twiml.
I would like to get a busy signal, call rejected (so no cost to cell phone bill) and then callback.
Grateful for help.
Upvotes: 3
Views: 259
Reputation: 3811
I'd love to better understand your use case but I'm going to give this a go using the Twilio Python Helper Library.
As far as <Reject>
TwiML goes, you can do something like this and be sure to specify a reason setting it to 'busy':
from flask import Flask
from flask import request
from twilio import twiml
account_sid = "YOU_ACCOUNT_SID"
auth_token = "YOUR_ACCOUNT_TOKEN"
client = TwilioRestClient(account_sid, auth_token)
app = Flask(__name__)
@app.route('/test', methods=["GET", "POST"])
def test():
whitelist = ['+14151XXXXXX', '+14152XXXXXX']
call_from = request.form['From']
if call_from in whitelist:
resp = twiml.Response()
resp.reject(reason="busy")
return str(resp)
else:
resp = twiml.Response()
call_to = "+14153XXXXXX"
resp.dial(call_to)
return str(resp)
if __name__ == "__main__":
app.run()
As there is no callback within <Reject>
you will have a few options as to how you can proceed.
If you're using Python 3.3 or greater you might consider doing the whole process with asyncio.
With older versions of Python, then I would recommend you try completing the client call through a task queue as demonstrated in this post.
Please let me know if this helps at all.
Upvotes: 1