Programmable Chat supports media messages, allowing your users to add photos, video or any other type of file to their chat. A media message appears on the channel similar to a text only message but with additional properties that let your client know the media's size as well as an optional content type and default filename. When creating your media message, you will pass media to the Programmable Chat SDK which will be read and saved to your Chat instance. Your application can later request a stream to retrieve the media associated with a message and display it to your users.
Please note Media messaging is currently a beta feature.
A typical media message creation will include the following steps with the details depending on your client platform:
- Create a new message, passing in a stream to the media and its mime content type. (The current maximum size of media accepted is 150mb.)
- Optionally specify a default download filename to help your application display the media to other users.
- Programmable Chat will provide you feedback of upload progress as well as an indication your media has been successfully saved. (iOS and Android only)
- The message is created on the specified channel and the channel's members are notified.
On the receiving side of a media message, you will:
- Receive a chat message that includes a media SID.
- Programmable Chat will provide feedback as the download progresses and a completion call when the media is ready.
- Your client will display or otherwise make available to the user the message's media content.
Media on messages within Programmable Chat are attachments which live separately from your chat message. They are associated to the owning message by a media SID. Media files cannot exist without their owning message, and deletion of a message will result in cleanup of it's associated media. Media files are immutable once created, you can modify other supported attributes of a message that has media content but the media itself is not changeable. Media messages do not contain a per-message text portion, instead being populated by the service with a placeholder message for legacy clients without media attachment support – this placeholder message is developer definable on the service instance.
Most developers will want to implement media caching to avoid unnecessary battery or network consumption should the application need to view the same media multiple times. An example of such a caching mechanism can be found in the helper classes of the Chat demo applications published to GitHub. It is also a good idea to prevent concurrent downloads of the same media in your clients, the helper library also handles queueing up requests for the same media until a single download completes before returning the requested media object.
For your users to create messages with media content, their Role must contain the
sendMessage permissions. On new Chat instances this is in the default channel roles, but must be added to existing instances if you wish to support it. For an example on how to apply this permission to your Channel User and Admin roles, see Update a Role or Roles and Permissions for more general information on Roles in Programmable Chat.
The media creation and download methods within the client SDK's take a stream or file as their parameter. How the media is expressed in the client SDK will depend on your platform:
Media files are uploaded by providing an NSInputStream compliant stream to the TCHMessageOptions for your new message. Likewise, any NSOutputStream can be provided as the destination for a media download. Often these streams will be backed by a file but they can just as easily live in memory, pass through to a network connection, or something else entirely.
For Android, any java.io.InputStream compliant stream can be used as the source for a new media message. Likewise, any java.io.OutputSteram can be provided as the destination for a media download. Often these streams will be backed by a file but they can just as easily live in memory, pass through to a network connection, or something else entirely.
Creating a media enriched message is very similar to creating a new text-only message - you start by creating a message as usual but instead of body for the content, you provide media for the message:
Only messages which contain media will have a media SID associated with them. The code sample provided here shows how to detect a media message on each platform:
It's important to note that at this time, Programmable Chat does not maintain an internal cache of media so you are encouraged to cache downloaded media in your application to avoid unnecessary downloads. It is also important for best performance to only allow a particular media file to be downloaded one at a time to avoid unnecessary battery and network consumption.
Your backend services can also use the Media feature off Chat - uploading new files for attachment with Messages, and of course sending Messages with Media attached. This section provides a brief overview of typical Media operations and flows. For a more detailed API description, please refer to Chat Media REST API documentation.
At present the underlying Media REST endpoint which is used to create (upload) the media (files) is a separate endpoint and not supported in the Twilio Helper Libraries.
Sending a Media Message via REST is a 2 step process:
- Upload media to MCS
- Send Media Message to chat channel (linking/attaching the Media created in step 1 to the new Message)
Uploading media is should be done directly from the source machine, using native HTTP facilities. Using cURL, the equivalent request looks like this:
curl -u “<acount_sid>:<account_secret>” --data-binary “@<filename>” https://mcs.us1.twilio.com/v1/Services/<chat_service_sid>/Media
That will leave your media on Twilio servers, ready to be bound to a Chat Message by the returned SID. The REST Message endpoint documentation is a good general description for sending Chat messages; the
MediaSid parameter accepts the SID from the media upload and binds that media to the message.
curl -u “<acount_sid>:<account_secret>” -X POST https://chat.twilio.com/v2/Services/<chat_service_sid>/Channels/<channel_sid>/Messages -d MediaSid=<media_sid>
Now that you're ready to enable your users to share the finest cute animal photos the internet has to offer, let's learn about Result Paging in Chat.
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。