簡體   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