【问题标题】:using Python with Twilio how can I implement reject() and callback?将 Python 与 Twilio 一起使用如何实现拒绝()和回调?
【发布时间】:2016-06-15 08:12:16
【问题描述】:

我想用我的手机拨打我的 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)

这会产生可怕的响应:“我们很抱歉发生了系统错误。再见”。

但是,如果我在第一次响铃后拨打该号码并挂断,则效果很好。所以我假设问题出在拒绝动词上。如您所见,我尝试过使用和不使用 twiml。

我想得到一个忙音,拒绝来电(这样不需要手机话费),然后回拨。

感谢您的帮助。

【问题讨论】:

    标签: python twilio


    【解决方案1】:

    我很想更好地了解您的用例,但我将尝试使用 Twilio Python Helper Library

    &lt;Reject&gt; 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()
    

    由于&lt;Reject&gt; 内没有回调,您将有几个选项来决定如何继续。

    如果您使用的是 Python 3.3 或更高版本,您可以考虑使用 asyncio 完成整个过程。

    对于旧版本的 Python,我建议您尝试通过任务队列完成客户端调用,如 this post 中所示。

    如果这有帮助,请告诉我。

    【讨论】:

    • 谢谢,感谢您的评论。我将尝试使用调度程序而不是 time.sleep()
    猜你喜欢
    • 2016-07-08
    • 2014-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    相关资源
    最近更新 更多