Search code examples
c#.net-coretwiliotwilio-api

Twilio Conversations - Send Image


The goal is to send an image using the conversations API (group chat) from the backend.

I see how you can use mediaUrl if it's just 1-1 messaging:

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

The above doesn't work for me as I'm looking to use a group chat will multiple members. Here is my current implementation for sending a text to multiple participants

// 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
);

The conversations API doesn't have any concept of mediaURL. Is it possible to use Conversations MessageResource to send an image?


Solution

  • To send media to a conversation is a two part process, unlike sending an MMS message.

    First you need to upload the media to the Media resource endpoint. This is by making a POST request to the URL https://mcs.us1.twilio.com/v1/Services/{Chat Service SID}/Media.

    You can get the Chat Service SID by fetching a Conversation resource and inspecting the Chat Service SID returned. All conversations in the same conversation service will have the same Chat Service SID.

    Once you have the Chat Service SID, you can upload the file. Here is an example in 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
    

    Now that you have uploaded the media, you can use it in a conversation message. To do so, you need to pass an array of media objects that you want to send as part of the message. A media object has a content_type with the MIME type of the media, a filename, a size and the sid of the media resource you just created.

    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"
          }
        }]
    );
    

    (Please excuse my C# if that's not the right way to initialize a list of objects, hopefully it gets the idea across.)