Skip to main content
Version: v4

Card Message

The CardMessage class is used to create a card message for CometChat. It extends the InteractiveMessage class from CometChat.

Constructor

NameTypeDescription
receiverIdstringThe ID of the receiver
receiverTypestringThe type of the receiver
textstringThe text to be displayed on the card
cardActionsButton ElementThe actions to be performed when the card is interacted with

Class Usage

How to create an instance of the FormMessage class:

// Create instance of ButtonElement for card actions

let cardAction = new ButtonElement(
"1",
new APIAction("https://example.com/api", "POST"),
"Click Me"
);

// Create a new instance of CardMessage

let cardMessage = new CardMessage(
"receiverId",
CometChat.RECEIVER_TYPE.USER,
"This is a card",
[cardAction]
);

Key Properties and Methods

Image Url

The setImageUrl() method allows to set the image URL for the card.

let cardMessage = new CardMessage(
"receiverId",
CometChat.RECEIVER_TYPE.USER,
"This is a card",
[cardAction]
);

cardMessage.setImageUrl("https://example.com/image.png");

Example

Below is an example that showcases the creation and manipulation of an instance of CardMessage:

// Create an instance of APIAction

let apiAction = new APIAction("https://example.com/api", "POST");

// Create instance of ButtonElement for card actions

let cardAction = new ButtonElement("1", apiAction, "Click Me");

// Create a new instance of CardMessage

let cardMessage = new CardMessage(
"receiverId",
CometChat.RECEIVER_TYPE.USER,
"customType",
"This is a card",
[cardAction]
);

cardMessage.setImageUrl("https://example.com/image.png");

In this example, a new instance of CardMessage is created. The text, card actions and image URL are retrieved, updated, and retrieved again. Then a new CardMessage instance is made from a JSON object, and the text is retrieved and logged.