目标是使用对话API(群聊)从后端发送图像.

我明白了,如果只是1对1的消息,你怎么能做到use mediaUrl:

MessageResource.Create(
        body: "Hello there!",
        from: new Twilio.Types.PhoneNumber("+15555555555"),
        mediaUrl: mediaUrl,
        to: new Twilio.Types.PhoneNumber("+12316851234")
);

以上不适用于我,因为我希望使用群聊将多个成员.以下是我当前用于向多个参与者发送文本的实现

// create a group chat
ConversationResource conversation = ConversationResource.Create(client: _twilioClient);

var bootyService = ParticipantResource.Create(
    identity: "myService",
    messagingBindingProjectedAddress: "+15555555555",
    pathConversationSid: conversation.Sid, 
    client: _twilioClient
);

// participant 1
var sender = ParticipantResource.Create(
    messagingBindingAddress: "+12316851234",
    pathConversationSid: conversation.Sid, 
    client: _twilioClient
);

// participant 2
var Receiver = ParticipantResource.Create(
    messagingBindingAddress: "+12316851234",
    pathConversationSid: conversation.Sid, 
    client: _twilioClient
);

var groupMessage = ConversationMessageResource.Create(
    body: "Hi this is a group chat",
    author: "myService",
    pathConversationSid: conversation.Sid, 
    client: _twilioClient
);

Conversations API没有任何MediaURL的概念.可以使用对话消息资源发送图像吗?

推荐答案

与发送彩信不同,向对话发送媒体是一个由两部分组成的过程.

首先,您需要将媒体上传到Media resource endpoint.这是通过向URL https://mcs.us1.twilio.com/v1/Services/{Chat Service SID}/Media发出POST请求来实现的.

您可以通过获取Conversation resource并判断返回的聊天服务SID来获得聊天服务SID.同一对话服务中的所有对话将具有相同的聊天服务SID.

一旦您有了聊天服务SID,您就可以上传文件.以下是curl年中的一个例子:

curl -u "<account_sid>:<account_secret>" --data-binary @<filename.png> -H "Content-Type: <content-type of upload>" https://mcs.us1.twilio.com/v1/Services/<chat_service_sid>/Media

现在你已经上传了媒体,你可以在a conversation message中使用它.为此,您需要传递要作为消息的一部分发送的媒体对象array.媒体对象具有媒体的MIME类型的content_type、刚刚创建的媒体资源的filenamesizesid.

var groupMessage = ConversationMessageResource.Create(
    body: "Hi this is a group chat",
    author: "myService",
    pathConversationSid: conversation.Sid, 
    client: _twilioClient,
    media: {
      new {
        content_type = "image/png",
        size = 123456,
        filename: "filename.png",
        sid = "MEDIA_SID"
      }
    }]
);

(如果这不是初始化对象列表的正确方式,请原谅我的C#,希望它能理解我的 idea .)

Csharp相关问答推荐

下拉图片点击MudBlazor

FileStream. FlushAsync()是否确保文件被写入磁盘?

从Blob存储中提取tar.gz文件并将提取结果上载到另一个Blob存储

如何在不考虑年份的情况下判断日期时间是否在某个日期范围内?

在C#中,有没有一种方法可以集中定义跨多个方法使用的XML参数描述符?

Appsettings.json未加载.Net 8 Blaazor Web程序集

最新的Mediatr和具有同步方法的处理程序Handle:并非所有代码路径都返回值"

如何使用新的Microsoft.IdentityModel.JsonWebToken创建JwtSecurityToken?

在具有不同属性名称的两个类之间创建关系

Lambda表达式如何与隐式强制转换一起工作?

Polly重试URL复制值

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

在Windows Plesk上发布ASP.NET Core 7 Web API-错误:无法加载文件或程序集';Microsoft.Data.SqlClient';

Content WithTargetPath实际上是有效的MSBuild项吗?

在';、';附近有错误的语法.必须声明标量变量";@Checkin";.';

C# Winforms:从对象树到TreeView的递归转换重复条目

C#如何获取字符串中引号之间的文本?

如何使用LINQ在C#中填充列表列表?

项目参考和方法签名问题

无法使用EF核心插入已有导航属性的新实体