An InteractiveMessage is a specialized object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. This enhances user engagement by making the chat experience more interactive and responsive to user input.
InteractiveMessage is a chat message with embedded interactive content. It can contain various properties:
Parameter
Description
receiverId
The UID or GUID of the recipient
Required
receiverType
The type of the receiver to whom the message is to be sent. Options: CometChatConstants.RECEIVER_TYPE_USER (user) or CometChatConstants.RECEIVER_TYPE_GROUP (group)
Required
messageType
The type of the message that needs to be sent
Required
interactiveData
A JSONObject holding structured data for the interactive element.
Required
allowSenderInteraction
A boolean determining whether the message sender can interact with the message. Default is set to false.
interactionGoal
An InteractionGoal object encapsulating the intended outcome of interacting with the InteractiveMessage. Default is set to none.
This method marks a message as interacted by identifying it with the provided Id. it also logs the interactive element associated with the interaction.
Kotlin
Java
Report incorrect code
Copy
Ask AI
val messageId = 1val elementId = "elementId"CometChat.markAsInteracted(messageId, elementId, object : CometChat.CallbackListener<Void?>() { override fun onSuccess(unused: Void?) { } override fun onError(e: CometChatException) { }})
Report incorrect code
Copy
Ask AI
int messageId = 1;String elementId = "elementId";CometChat.markAsInteracted(messageId, elementId, new CometChat.CallbackListener<Void>() { @Override public void onSuccess(Void unused) { } @Override public void onError(CometChatException e) { }});
A key feature of InteractiveMessage is checking whether a user’s interactions with the message meet the defined InteractionGoalYou would be tracking every interaction users perform on an InteractiveMessage (captured as Interaction objects) and comparing those with the defined InteractionGoal. The completion of a goal can vary depending on the goal type:
Goals
Description
Keys
Any Interaction
The goal is considered completed if there is at least one interaction.
CometChatConstants.INTERACTION_TYPE_ANY
Any of Specific Interactions
The goal is achieved if any of the specified interactions occurred.
CometChatConstants.INTERACTION_TYPE_ANY_OF
All of Specific Interactions
The goal is completed when all specified interactions occur.
The InteractiveMessage can be sent using the sendInteractiveMessage method of the CometChat class. The method requires an InteractiveMessage object and a CallbackListener for handling the response.Before sending interactive messages, ensure you have initialized the SDK and logged in a user.Here is an example of how to use it:
Kotlin
Java
Report incorrect code
Copy
Ask AI
val interactiveData = JSONObject()try { interactiveData.put("title", "Demo Form") val jsonArray = JSONArray() val textInput = JSONObject() textInput.put("elementType", "textInput") textInput.put("elementId", "element1") textInput.put("label", "Name") textInput.put("optional", false) textInput.put("maxLines", 1) jsonArray.put(textInput) interactiveData.put("formFields", jsonArray) val submitElement = JSONObject() submitElement.put("elementType", "button") submitElement.put("elementId", "element8") submitElement.put("buttonText", "Submit") submitElement.put("disableAfterInteracted", true) val action = JSONObject() action.put("actionType", "urlNavigation") action.put("url", "https://www.cometchat.com/") submitElement.put("action", action) interactiveData.put("submitElement", submitElement)} catch (e: JSONException) { throw RuntimeException(e)}val interactiveMessage = InteractiveMessage(receiverId, receiverType, "form", interactiveData)CometChat.sendInteractiveMessage(interactiveMessage, object : CometChat.CallbackListener<InteractiveMessage>() { override fun onSuccess(interactiveMessage: InteractiveMessage) { // This block is executed when the InteractiveMessage is sent successfully. } override fun onError(e: CometChatException) { // This block is executed if an error occurs while sending the InteractiveMessage. }})
Report incorrect code
Copy
Ask AI
JSONObject interactiveData = new JSONObject();try { interactiveData.put("title", "Demo Form"); JSONArray jsonArray = new JSONArray(); JSONObject textInput = new JSONObject(); textInput.put("elementType", "textInput"); textInput.put("elementId", "element1"); textInput.put("label", "Name"); textInput.put("optional", false); textInput.put("maxLines", 1); jsonArray.put(textInput); interactiveData.put("formFields", jsonArray); JSONObject submitElement = new JSONObject(); submitElement.put("elementType", "button"); submitElement.put("elementId", "element8"); submitElement.put("buttonText", "Submit"); submitElement.put("disableAfterInteracted", true); JSONObject action = new JSONObject(); action.put("actionType", "urlNavigation"); action.put("url", "https://www.cometchat.com/"); submitElement.put("action", action); interactiveData.put("submitElement", submitElement);} catch (JSONException e) { throw new RuntimeException(e);}InteractiveMessage interactiveMessage = new InteractiveMessage(receiverId,receiverType,"form", interactiveData);CometChat.sendInteractiveMessage(interactiveMessage, new CometChat.CallbackListener<InteractiveMessage>() { @Override public void onSuccess(InteractiveMessage interactiveMessage) { // This block is executed when the InteractiveMessage is sent successfully. } @Override public void onError(CometChatException e) { // This block is executed if an error occurs while sending the InteractiveMessage. }});
CometChat SDK provides event listeners to handle real-time events related to InteractiveMessage. For more details on listener management, see Real-Time Listeners.On InteractiveMessage Received The onInteractiveMessageReceived event listener is triggered when an InteractiveMessage is received.Here is an example:
Kotlin
Java
Report incorrect code
Copy
Ask AI
CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { override fun onInteractiveMessageReceived(interactiveMessage: InteractiveMessage) { // This block is executed when an InteractiveMessage is received. // Here you can define logic to handle the received InteractiveMessage and display it in your chat interface. }})
Report incorrect code
Copy
Ask AI
CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { @Override public void onInteractiveMessageReceived(InteractiveMessage interactiveMessage){ // This block is executed when an InteractiveMessage is received. // Here you can define logic to handle the received InteractiveMessage and display it in your chat interface. } });
Always remove listeners when they’re no longer needed (e.g., in onDestroy() or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.
The onInteractionGoalCompleted event listener is invoked when an interaction goal is achieved.Here is an example:
Kotlin
Java
Report incorrect code
Copy
Ask AI
CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { override fun onInteractionGoalCompleted(interactionReceipt: InteractionReceipt) { // This block is executed when an interaction goal is completed. // Here you can specify the actions your application should take once an interaction goal is achieved, such as updating the UI or notifying the user. }})
Report incorrect code
Copy
Ask AI
CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { @Override public void onInteractionGoalCompleted(InteractionReceipt interactionReceipt) { // This block is executed when an interaction goal is completed. // Here you can specify the actions your application should take once an interaction goal is achieved, such as updating the UI or notifying the user. }});
These event listeners offer your application a way to provide real-time updates in response to incoming interactive messages and goal completions, contributing to a more dynamic and responsive user chat experience.
An InteractiveMessage is constructed with the receiver’s UID, the receiver type, the interactive type, and interactive data as a JSONObject. Once created, the InteractiveMessage can be sent using CometChat’s sendInteractiveMessage() method. Incoming InteractiveMessages can be received and processed via CometChat’s message listener framework.
Always validate your JSONObject structure before sending interactive messages. Ensure all required fields (elementType, elementId, etc.) are present to avoid runtime errors.
Use Unique Element IDs
Assign unique element IDs to each interactive component within a message. This ensures accurate tracking of user interactions and prevents conflicts when processing interaction events.
Set Appropriate Interaction Goals
Choose the right interaction goal type based on your use case. Use ANY for simple acknowledgments, ANY_OF for optional selections, and ALL_OF for required form completions.
Handle Interaction Receipts
Implement the onInteractionGoalCompleted listener to track when users complete interactive elements. This enables you to trigger follow-up actions or update your UI accordingly.