在Twilio中使用Python如何实现reject()和回调?

在Twilio中使用Python如何实现reject()和回调?,python,twilio,Python,Twilio,我想用手机打我的Twilio号码,Twilio认出了我的号码,拒绝了电话,然后给我回了电话。代码如下: @application.route("/test", methods=['GET', 'POST']) def test(): whitelist = ['81808730xxxx', '44753810xxxx', '+44753810xxxx', '+44792834xxxx', '44792834xxxx'] call_from = request.values.get

我想用手机打我的Twilio号码,Twilio认出了我的号码,拒绝了电话,然后给我回了电话。代码如下:

@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)
@application.route(“/test”,方法=['GET','POST']))
def test():
白名单=['81808730xxxx','44753810xxxx','+44753810xxxx','+44792834xxx','44792834xxx']
call_from=request.values.get('from','None')
如果从白名单中调用\u:
# ""
resp=twilio.twiml.Response()
分别拒绝
时间。睡眠(10)
account\u sid=“account\u sid”
认证令牌=“账户认证令牌”
client=TwilioRestClient(帐户sid、身份验证令牌)
call=client.calls.create(to=call\u from,from=“+1646480xxxx”,url=”https://zshizuka.herokuapp.com/gather")
打印call.sid
其他:
resp=twilio.twiml.Response()
呼叫至=“+44792834xxx”
响应拨号(呼叫到)
#返回json.dumps({'success':True}),200
返回str(resp)
这产生了可怕的反应:“很抱歉发生了系统错误。再见”

然而,如果我在第一次响铃后就拨了这个号码并挂断了电话,它就可以正常工作了。所以我假设问题在于拒绝动词。正如你所看到的,我试过使用twiml和不使用twiml

我想得到一个繁忙的信号,电话拒绝(所以没有费用的手机账单),然后回拨


谢谢你的帮助

我很想更好地理解您的用例,但我将使用

就目前而言,您可以这样做,并确保指定一个将其设置为“忙碌”的原因:

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()
由于
中没有回调,您将有几个选项来决定如何继续

如果使用Python 3.3或更大,则可以考虑用.< /P>进行整个过程。 对于较旧版本的Python,我建议您尝试通过任务队列完成客户端调用,如中所示


请让我知道这是否有帮助

谢谢,谢谢你的评论。我将尝试使用调度程序,而不是time.sleep()