Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Sending Messages and Media


Using the SDK, you can craft messages with text and/or media attachments and send them to other participants in your Conversation.


Send a text Message

send-a-text-message page anchor

If you'd like to send your Message as a one-shot method call, you can use this basic method.

Send a text Message

send-a-text-message-1 page anchor
Node.js
Typescript

_10
//send a basic message into the Conversation
_10
await conversation.sendMessage('hello world');

We also provide a more flexible method that allows you to programmatically build your message. This method is more robust and is typically ideal for most use cases.

Message Builder

message-builder page anchor

The Message Builder class allows you to build a new text and/or media Message and makes the Message ready to be sent to the Conversation.

When creating the MessageBuilder object, you'll set each property of the message individually. For example, you'll set the body and the message attributes separately.

Node.js
Typescript

_10
/* Using MessageBuilder */
_10
// Message builder. Allows the message to be built and sent via method chaining.
_10
_10
await testConversation.prepareMessage()
_10
.setBody('Hello!')
_10
.setAttributes({foo: 'bar'})
_10
.addMedia(media1)
_10
.addMedia(media2)
_10
.build()
_10
.send();


You can add photos, videos, and other types of media files to your Conversation. Media is displayed seamlessly between all participating channels.

To add a media Message (i.e. photos, videos) to your Conversation, you'll need to create a Message and add a media file, filename and content type to the Message Builder.

Node.js
Typescript

_11
const file = await fetch("https://v.fastcdn.co/u/ed1a9b17/52533501-0-logo.svg");
_11
const fileBlob = await file.blob();
_11
_11
// Send a media message
_11
const sendMediaOptions = {
_11
contentType: file.headers.get("Content-Type"),
_11
filename: "twilio-logo.svg",
_11
media: fileBlob
_11
};
_11
_11
await conversation.prepareMessage().addMedia(sendMediaOptions);

Each SDK accepts media input differently:

For JS, use:

  • A String or Node.js Buffer containing a media byte stream
  • A new FormData object containing file information: filename , content-type , size , and all required FormData information

For iOS, use:

  • an InputStream
  • an NSInputStream-compliant stream
  • an NSData buffer

For Android, use:

  • any java.io.InputStream-compliant stream

Send Multiple Media Messages

send-multiple-media-messages page anchor
(information)

Info

The maximum combined size of attachments is 150 MB.

You can also attach multiple media items to a single message by using the Message Builder.

Send multiple Media Messages

send-multiple-media-messages-1 page anchor
Node.js
Typescript

_23
/* Send multiple media */
_23
_23
const file = await fetch("https://v.fastcdn.co/u/ed1a9b17/52533501-0-logo.svg");
_23
const fileBlob = await file.blob();
_23
_23
const mediaFormData = new FormData();
_23
mediaFormData.set("twilio-logo", fileBlob, "twilio-logo.svg");
_23
_23
const sendMediaOptions = {
_23
contentType: file.headers.get("Content-Type"),
_23
filename: "twilio-logo.svg",
_23
media: fileBlob
_23
};
_23
_23
await testConversation.prepareMessage()
_23
.setBody("Hello!")
_23
// add multiple media
_23
.addMedia(mediaFormData)
_23
.addMedia(sendMediaOptions)
_23
// ...
_23
.addMedia(mediaN)
_23
.build()
_23
.send();

Retrieve Media Message Content

retrieve-media-message-content page anchor

You can get a short-lived, temporary URL to download the media content in a Conversation.

If a message has more than one attachment, an array of media Messages can be retrieved, but it has to match the specific category of media.

(information)

Info

media is currently the only category available

You can use your preferred method to retrieve the content from the temporary URL and render it in your UI.

Node.js
Typescript

_10
/* Check and update Media samples */
_10
_10
// Return all media attachments, without temporary urls
_10
const media = message.attachedMedia;
_10
_10
// Return a (possibly empty) array of media matching a specific set of categories. Allowed category is so far only 'media'
_10
const categorizedMedia = await message.getMediaByCategory(["media"]);
_10
_10
//Get a temporary URL for the first media returned by the previous method
_10
const mediaUrl = await categorizedMedia[0].getContentTemporaryUrl();

Conversations is a cross-channel messaging product, so each channel has a different set of limitations about incoming media files. Please refer to theMedia Limits documentation for channel-specific information and supported file types.

Media content is encrypted and can not be downloaded directly. When required, only authenticated users can generate temporary/expiring URLs to download the media content. The temporary URLs are valid for 300 seconds, after which a new temporary URL must be requested.


Let's learn other features in Twilio Conversations:


Rate this Page: