【问题标题】:send more than 1 image with twilio and flask使用 twilio 和烧瓶发送超过 1 张图像
【发布时间】:2021-08-30 21:57:23
【问题描述】:

嗨,我是 flask 和 twilio 的新手,我正在尝试发送 2 个带有 1 个请求的图像,但只是出现了我放入数组中的最后一张图像

这是我的代码

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)

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

【问题讨论】:

    标签: python flask twilio whatsapp send


    【解决方案1】:

    这里是 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 服务器上的图像。

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

    【讨论】:

    • 感谢您的回答,但这对我不起作用,我使用的是试用版,这是一个问题吗?
    • 错误是什么?拥有试用帐户不应影响代码,但 URL 无效/无图像会
    • 图片在里面,可以用,问题是只给我发一张
    • 谢谢你的回答,图像有效,我一个接一个地响应并且工作正常,但是使用和数组只是向我发送最后一张图像
    • 当我运行上面的代码时,我得到了两个图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2014-06-04
    • 1970-01-01
    • 2014-07-16
    • 2013-07-08
    • 2014-07-23
    • 2020-07-01
    相关资源
    最近更新 更多