【问题标题】:C# Twilio Whatsapp Inbound Message return a MediaUrlC# Twilio Whatsapp 入站消息返回一个 MediaUrl
【发布时间】:2021-11-08 06:35:05
【问题描述】:

我有一条带有快速回复按钮的消息要发送给我的用户。快速回复按钮只是说接受。一旦用户点击它,我的服务器就会返回一个指向文件的链接给他们。

到目前为止效果很好。但是,我注意到该 url 纯粹作为文本 url 发送,而不是作为文件解析。有没有办法让 Api 知道我希望它被解析为文件而不是文本?

网址的结构如下:https://location.website.com/filename.pdf

这是我的入站消息函数代码:

[HttpPost("InboundMessage")]
[Consumes("application/x-www-form-urlencoded")]
public TwiMLResult InboundMessage([FromForm] WhatsappInboundMessage Input)
{
    var em = whatsappMessageLogData.GetAll(Input.From, TenantId);
    if (em.Count > 0)
    {
        em = em.OrderByDescending(x => x.CreatedOn).ToList();
        var latestItem = em.FirstOrDefault();
        var difference = (DateTime.Now - latestItem.CreatedOn).TotalHours;
        if (difference < 2)
        {
            var message = new Message();
            message.Body($"You can also access the file here: {latestItem.FileAccessLink}");
            message.Media(link);
            response.Append(message);
            return TwiML(response);
        }

    }
    var res = whatsappIMData.Save(Input);
    response.Message(res);
    return TwiML(response);
}

这是 twiml 响应。我只得到网址。

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Message>
    <Body>You can also access the file here: https://localhost:44332/FileReads/Read?Key=bD4PgsE0Vqq4yMnA5S43WTourBmh3I</Body>
    <Media>https://www.filelink.pdf</Media>
  </Message>
</Response>

【问题讨论】:

    标签: c# asp.net-core .net-core twilio twilio-twiml


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    要将文件作为媒体附件返回,您应该在&lt;Message&gt; 中嵌套一个&lt;Media&gt; 元素:

            if (difference < 2)
            {
                var message = new Message();
                message.Media(latestItem.Link);
                message.Body("Here's the latest items's link");
                response.Append(message);
                return TwiML(response);
            }
    

    有关更多详细信息和示例,请参阅the documentation for &lt;Message&gt;

    【讨论】:

    • 是否可以同时返回文本和媒体网址?我按照您链接中的 c# 示例进行操作,但只显示媒体消息。文本未发送。我尝试调试它,我看到消息已附加但我只收到媒体网址。
    • 是的,您可以使用message.Body("This is the message") 设置消息的文本。我也会更新我的答案。 This example in the docs shows how.
    • 是的,我试过了,但出现的唯一消息是 URL。
    • 我在问题中编辑了我的代码,以显示我目前是如何做到的。
    • 再次检查我的答案。您需要创建一个Message 对象并将BodyMedia 附加到Message,然后将Message 附加到response
    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2020-06-26
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多