首页 > 解决方案 > 使用 twilio 和烧瓶发送超过 1 张图像

问题描述

嗨,我是烧瓶和 twilio 的新手,我尝试使用 1 个请求发送 2 张图像,但只显示我放入数组中的最后一张图像

这是我的代码

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from dotenv import load_dotenv
import os

load_dotenv()

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

@app.route("/sms", methods=['POST'])
def sms_reply():
    """Respond to incoming calls with a simple text message."""
    # Fetch the message
    msg = request.form.get('Body')

    # CLIENT 
    client = Client(os.getenv("TWILIO_ID"), os.getenv("AUTH_TOKEN"))
    number = request.values.get('From', '')
    #  creating the message
    if msg == "imagen":
        message = client.messages.create(
                                from_='whatsapp:+14155238886',
                                media_url=["https://demo.twilio.com/owl.png", "https://demo.twilio.com/bunny.png"],
                                to=number
                            )
        resp = MessagingResponse()
        resp.message("imagen : {}".format(message.body))
        return str(resp)

怎么能在“media_url”参数中看到我放了 2 个 url,但 twilio 只发送了我的 1,我犯了一个错误?我也这样试试

        mss = MessagingResponse()
        resp = mss.message("this is a message")
        resp.body("imagen1")
        resp.media("https://demo.twilio.com/owl.png")
        resp.body("imagen2")
        resp.media("https://demo.twilio.com/bunny.png")
       
        return str(resp)

但是是一样的。谢谢你的帮助

标签: pythonflasktwiliowhatsappsend

解决方案


Twilio 开发人员布道者在这里。该代码应该可以工作,只是该 URL 上没有兔子图像。您需要将图像托管在可公开访问的 URL 上,如下所示:

 message = client.messages.create(
     from_="YOUR-TWILIO-NUMBER",
     to="NUMBER-TO-RECEIVE-IMAGES",
     body="Test sending two messages",
     media_url=["https://data.whicdn.com/images/339398954/original.gif", "https://thumbs.gfycat.com/PrestigiousUntidyBetafish-max-1mb.gif"],
 )

这将返回此图像: 收到的短信示例 我建议使用Twilio Runtime Assets,这是我们的静态文件托管服务,允许开发人员快速上传和提供支持其应用程序所需的文件。托管支持 Web、语音和消息应用程序的文件。Assets 通常用于托管 TwiML 中使用的 .mp3 音频文件,提供通过 MMS 发送的图像,或存储 Twilio Functions 使用的配置。您还可以部署要托管在 Web 服务器上的图像。

让我知道这是否有帮助!


推荐阅读