繁体   English   中英

Twilio 对话 - 发送图片

[英]Twilio Conversations - Send Image

目标是使用来自后端的对话 API(群聊)发送图像。

如果只是 1-1 消息传递,我了解如何使用 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
);

对话 API 没有任何 mediaURL 的概念。 是否可以使用 Conversations MessageResource 发送图像?

与发送 MMS 消息不同,将媒体发送到对话是一个由两部分组成的过程。

首先,您需要将媒体上传到媒体资源端点 这是通过向POST https://mcs.us1.twilio.com/v1/Services/{Chat Service SID}/Media

您可以通过获取对话资源并检查返回的聊天服务 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

现在您已经上传了媒体,您可以在对话消息中使用它。 为此,您需要传递要作为消息的一部分发送的媒体对象数组。 媒体 object 的content_type具有媒体的 MIME 类型、 filenamesize和刚刚创建的媒体资源的sid

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#,希望它能让你明白这一点。)

为了完整起见,我将在这里发布我最终所做的事情 - 它并不漂亮,但似乎没有更丑陋的方式来做到这一点

TL;DR 是您每次都必须将图像上传到 Twilio 作为媒体 object。 在响应中,您获得 SID 并将其传递给您发送的带有图像的消息

ConversationResource conversation = ConversationResource.Create(client: _twilioClient);
string postUrl = "https://mcs.us1.twilio.com/v1/Services/" + conversation.ChatServiceSid + "/Media";

string filePath = "./wwwroot/img/myImage.png";
FileStream fs = System.IO.File.OpenRead(filePath);
var formContent = new MultipartFormDataContent
{
    {new StreamContent(fs), "file", "myImage.png"}
};


var myHttpClient = new HttpClient();
myHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Basic", Convert.ToBase64String(
          System.Text.ASCIIEncoding.ASCII.GetBytes(_config.Twilio.AccountSid + ":" + _config.Twilio.AuthToken)));
var response = await myHttpClient.PostAsync(postUrl, formContent);
var stringContent = await response.Content.ReadFromJsonAsync<TwilioPost>();
var textImage = ParticipantResource.Create(
    identity: "myService",
    messagingBindingProjectedAddress: _config.Twilio.FromNumber,
    pathConversationSid: conversation.Sid,
    client: _twilioClient
);

if (stringContent != null && stringContent.Sid != null)
{
    var groupMessage = ConversationMessageResource.Create(
        body: body,
        mediaSid: stringContent.Sid,
        author: "myService",
        pathConversationSid: conversation.Sid,
        client: _twilioClient
    );
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM