基于Python的Twilio Chatbot无响应及12200 Schema验证警告求助
问题排查与解决
- 核心原因:你的Flask接口默认返回
text/html类型的内容,但Twilio要求接收text/xml格式的响应,这会导致Twilio无法正确解析返回的TwiML,触发12200 schema验证警告,最终不发送回复。 - 修复步骤:
- 使用Flask的
make_response方法,明确设置响应的Content-Type为text/xml。 - 调整后的完整代码:
- 使用Flask的
from flask import Flask, request, make_response from twilio.twiml.messaging_response import MessagingResponse app = Flask(__name__) @app.route('/sms', methods=['POST']) def sms(): from_number = request.form['From'] to_number = request.form['To'] body = request.form['Body'] resp = MessagingResponse() resp.message("The Robots are coming! Head for the hills!") # 创建响应并指定内容类型 response = make_response(str(resp)) response.headers['Content-Type'] = 'text/xml' return response if __name__ == '__main__': app.run()
- 额外检查项:
- 确认Flask服务已通过内网穿透工具(如ngrok)暴露到公网,Twilio需要能正常访问你的
/sms接口。 - 核对Twilio控制台中Webhook的配置,确保地址正确且请求方法设置为POST。
- 确认Flask服务已通过内网穿透工具(如ngrok)暴露到公网,Twilio需要能正常访问你的
内容的提问来源于stack exchange,提问作者Kavya Shree
相关产品推荐
相关产品推荐

