You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twilio SMS Python快速入门:回复收到的消息遇阻求助

解决Twilio SMS回复问题:Flask request模块用法 + 本地环境调试

Hey there! Let's work through your Twilio SMS reply issue and clear up that Flask request module confusion.

首先:你绝对需要Flask的request模块!

Twilio sends all the details of the incoming SMS (like the message content, sender's phone number, etc.) to your Flask app via an HTTP POST request. The request module is how you pull that critical data out of the request so you can craft a relevant reply. Without it, you won't know what message was sent to you, or who to reply to.

具体用法示例

Here's how to integrate it into your reply script (replace with your details):

from flask import Flask, request  # 必须导入request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

# 这个路由是Twilio会调用的Webhook
@app.route("/sms", methods=['POST'])  # 必须用POST方法,Twilio默认用POST
def sms_reply():
    # 从Twilio的请求中提取短信内容和发件人
    incoming_message = request.form.get('Body', '').strip()
    sender_number = request.form.get('From')

    # 创建Twilio的回复对象
    response = MessagingResponse()
    reply_msg = response.message()

    # 自定义回复逻辑,比如echo收到的消息
    reply_msg.body(f"Got your message: '{incoming_message}' from {sender_number}!")

    # 返回Twiml格式的回复给Twilio
    return str(response)

if __name__ == "__main__":
    # 启动Flask服务,debug模式方便调试
    app.run(debug=True, port=5000)

解决virtualenv环境下收不到回复的问题

Since you're working in a virtualenv and already have send_sms.py working, let's fix the reply flow:

  • Install dependencies in your virtualenv: Make sure Flask and Twilio are installed in the active env:
    pip install flask twilio
    
  • Expose your local server to the internet: Twilio can't reach your local Flask app directly. Use a local tunneling tool (like ngrok) to create a public URL. Run this in a separate terminal (with your virtualenv active or not, it doesn't matter for ngrok):
    ngrok http 5000
    
    You'll get an HTTPS URL like https://abc123.ngrok.io – copy this.
  • Update Twilio Webhook settings: Go to your Twilio phone number's settings page, find the "A Message Comes In" section. Set the Webhook URL to your-ngrok-url/sms (e.g., https://abc123.ngrok.io/sms), and make sure the request method is set to POST.
  • Start your Flask app in virtualenv: Activate your virtualenv, then run your reply script:
    python your_reply_script.py
    
  • Debug checks:
    • Keep an eye on your Flask terminal – you should see incoming POST requests from Twilio when someone sends a message.
    • Check Twilio's Console > Monitor > Logs to see if there are any errors with the Webhook request (like timeout, 404, etc.).

快速调试小贴士

  • If you're getting a 404 error, double-check that your Webhook URL matches the Flask route (e.g., if your route is /sms, the URL must end with /sms).
  • Make sure your Flask app is running on the same port you told ngrok to forward (default 5000 in the example).
  • Disable any local firewalls that might block incoming requests to your Flask port.

内容的提问来源于stack exchange,提问作者kidchae

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:42:00