Twilio 使用 python 和 Flask 的 StatusCallback URL 无效

问题描述 投票:0回答:1

我正在尝试使用 python 和 Flask Webhook 向 Twilio 中的呼叫者发送短信。我的 python webhook 托管在 pythonanywhere.com 上。

代码片段如下所示。我无法在互联网上找到任何有关为什么我不断收到无效 StatusCallback URL 错误的详细信息。所有其他路线似乎都运行良好。

def twiml(resp: VoiceResponse) -> flask.Response:
    resp = flask.Response(resp)
    resp.headers['Content-Type'] = 'text/xml'
    return resp


@app.route('/send-message', methods=['POST'])
def send_message():
    response = VoiceResponse()

    msg = 'a text message'

    response.sms(msg,
                 status_callback=url_for('sms_callback', source=request.endpoint),
                 status_callback_method='POST',
                 to=request.values.get('From'),
                 from_=request.values.get('To')
                 )

    return twiml(response)


@app.route('/sms-callback', methods=['POST'])
def sms_callback():
    response = VoiceResponse()

    response.say('message callback')

    return twiml(response)


python flask twilio pythonanywhere
1个回答
0
投票

当然,我在发布问题后找到了答案。希望这对其他人有帮助!

status_callback
参数必须是完整 URL 链接,而不仅仅是相对链接,因为它正在传递到 Twilio 服务器。他们不知道如何处理相对链接,所以它是无效的。

我通过了如下所示的完整链接,它有效。

    response.sms(msg,
             status_callback='https://myaccount.pythonanywhere.com/sms-callback',
             status_callback_method='POST',
             to=request.values.get('From'),
             from_=request.values.get('To')
             )
© www.soinside.com 2019 - 2024. All rights reserved.