简体   繁体   English

如何使用 twilio rest api 获取媒体消息 URL?

[英]How to get a media messages URL using the twilio rest api?

I am trying to gather the URL of a media message sent in by a user in a python function. In theory (and according to this https://www.twilio.com/blog/retrieving-twilio-mms-image-urls-in-python tutorial) my python code below should work for this:我正在尝试收集用户在 python function 中发送的媒体消息的 URL。理论上(并根据此https://www.twilio.com/blog/retrieving-twilio-mms-image-urls- in-python教程)我下面的 python 代码应该适用于此:

last_message = client.messages.list(limit = 1)
last_message_instance = last_message[0]
media = last_message_instance
media_url = 'https://api.twilio.com' + media.uri[:-5]

However, for some reason the media.uri parameter does not return all three sid (AccountSid, MessageSid, Sid) strings needed for the url. The url should be composed of:但是,出于某种原因,media.uri 参数不会返回 url 所需的所有三个 sid(AccountSid、MessageSid、Sid)字符串。url 应由以下部分组成:

https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages/{MessageSid}/Media/{Sid}.json

The.uri returns only my AccountSid and the sent message's MessageSid (interestingly, labelled as Sid in the json message as shown below) the.uri只返回我的AccountSid和发送消息的MessageSid(有趣的是,在json消息中标记为Sid如下图)

"sid": "MMbde22b567bf7e3c77fcd4fe01d286446",

Does anyone have any tips on how to find the Media/{Sid} term I need (this typically begins with MEXxXxXxX) Thanks!有没有人对如何找到我需要的 Media/{Sid} 术语有任何提示(这通常以 MEXxXxXxX 开头)谢谢!

I think the issue here is that you are only making the request to the API to get a message, which is why you do not have the detail about the media.我认为这里的问题是您只是向 API 发出请求以获取消息,这就是为什么您没有有关媒体的详细信息。

You can request the media for the message by calling last_message_instance.media.list() .您可以通过调用last_message_instance.media.list()请求消息的媒体。 The result of that will be a list of media objects from which you can get the media URL.其结果将是一个媒体对象列表,您可以从中获取媒体 URL。

last_message = client.messages.list(limit = 1)
last_message_instance = last_message[0]
    for media in last_message_instance.media.list():
        media_url = 'https://api.twilio.com' + media.uri[:-5]
        print(media_url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 TWILIO 的 HTTP GET REST API 响应中消费分页消息的最佳 Python 设计模式是什么? - What is the best Python Design pattern for consuming Paginated Messages from a TWILIO's HTTP GET REST API response? 如何处理来自 Twilio REST API 的响应 - How to process a response from the Twilio REST API 如何使用 PHP 下载 Twilio Whatsapp 中的媒体? - How to download media in Twilio Whatsapp using PHP? Twilio Whatsapp:发送多条媒体消息 - Twilio Whatsapp: Sending Multiple Media Messages Twilio REST API 错误 - “认证” - Twilio REST API error - "Authenticate" 使用Twilio资源转发Twilio彩信 - Forward Twilio MMS Messages Using Twilio Resources 如何使用 .NET SDK 从 Twilio 下载彩信媒体 - How to download MMS Media from Twilio using the .NET SDK Twilio 错误 11200- HTTP 检索失败:尝试检索媒体失败,同时尝试通过 Rest API 触发 Twilio Studio 流程执行 - Twilio Error 11200- HTTP retrieval failure: Attempt to retrieve media failed, while trying to Trigger a Twilio Studio Flow Execution via Rest API 如何使用 Dialogflow 的 Twilio 接收 WhatsApp 音频消息 - How to Receive WhatsApp Audios Messages Using Twilio for Dialogflow 如何使用rest api从dynamodb获取数据 - how to get data from dynamodb using rest api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM