Media Template
Enhance user interaction with media-rich templates featuring images or videos.
The MediaTemplate
consists of a series of MediaElement
instances. Each
MediaElement
can include a media type (image or video), an attachment ID or
URL, and up to three interactive buttons.
MediaElement
Properties
- media_type: The type of media. Can be either 'image' or 'video'.
- attachment_id (optional): The attachment ID for the media. Used when uploading media to the platform.
- url (optional): The URL of the media. Used for referencing media hosted elsewhere.
- buttons: An array of buttons to include with the media element. A maximum of 3 buttons can be added.
Methods
-
constructor(media_type: MediaType): Initializes a new
MediaElement
with the specified media type ('image' or 'video'). -
setAttachmentId(attachment_id: string): Sets the attachment ID for the media.
- Throws an error if both
attachment_id
andurl
are set.
- Throws an error if both
-
setUrl(url: string): Sets the URL for the media.
- Throws an error if both
attachment_id
andurl
are set.
- Throws an error if both
-
addButtons(buttons: Button[]): Adds buttons to the media element.
- Throws an error if adding the buttons exceeds the maximum of 3 buttons.
-
toJSON(): Converts the
MediaElement
into a JSON object for use in the template.
MediaTemplate
Properties
- elements: An array of
MediaElement
instances to include in the template. - sharable (optional): Whether the template is sharable. Defaults to
false
.
Methods
-
constructor(options: { sharable?: boolean }): Initializes a new
MediaTemplate
with optional sharability. -
addElement(element: MediaElement): Adds a
MediaElement
to the template. Maximum of 1 element. -
toJSON(): Converts the
MediaTemplate
into a JSON object suitable for sending via the messaging platform.
Example Usage
JSON Representation
Additional Information
The MediaTemplate
class provides a flexible way to send media content with
interactive options. By using MediaElement
, you can include images or videos
with customizable buttons to enhance user interaction.
For detailed usage, consider exploring the MediaElement
class and its methods
to tailor the media elements according to your application's needs.
Last updated on