Skip to main content

Form Message

The FormMessage class is used to create an interactive form message that can be sent via CometChat. It extends the Interactive Messages class from CometChat.

Properties

NameTypeDescription
receiverUidStringThe ID of the receiver
receiverTypeStringThe type of the receiver
titleStringThe title of the form
formFieldsList<ElementEntity>The fields of the form
submitElementButtonElementThe submit button of the form
goalCompletionTextStringThe text visible after completion of goal
interactionGoalInteractionGoalThe sets the interaction goal for the form to ma
interactionsInteractionsGives the list of elements you have interacted with
allowSenderInteractionboolAllows the sender interaction

Class Usage

How to create an instance of the FormMessage class:

String receiver = "cometchat-uid-2";//replace this with receiver user
String appId = "XXXXX";
String apiKey = "XXXXXX";//Replace this with your api key
String senderId = "XXXXXXX";//Replace this with sender/on behalf of user id
String receiverType = ReceiverTypeConstants.user; //replace this with receiver type
User? loggedInUser = await CometChat.getLoggedInUser();

Map<String, String> header = {
"appId": appId,
"apiKey": apiKey,
"onBehalfOf": senderId,
"Content-Type": "application/json",
"accept": "application/json"
};

Map<String, dynamic> payload = {
"data": {
"text": "Thanks For filling the Form!"
},
"type": "text",
"category": "message",
"receiver": receiver,
"receiverType": "user"
};

FormMessage formMessage = FormMessage(
muid: DateTime.now().microsecondsSinceEpoch.toString(),
allowSenderInteraction: true,
title: "Society Survey",
receiverType: receiverType,
receiverUid: receiver,
sender: loggedInUser,
formFields: [
TextInputElement( elementId: "name", label: "Enter name ", optional: false),
TextInputElement( elementId: "age", label: "Enter age "),
SingleSelectElement(
elementId: "gender",
label: "Gender",
options: [
OptionElement(label: "Male",value: "Male" ),
OptionElement(label: "Female",value: "Female" ),
],
optional: false
),
CheckBoxElement( elementId: "service",
label: "Select Services",
defaultValue: ["pool"],
options: [
OptionElement(label: "pool",value: "pool" ),
OptionElement(label: "gym",value: "gym" ),
OptionElement(label: "garden",value: "garden" ),
]
),
DropdownElement(elementId: "Block", label: "Select block" ,
options: [
OptionElement(label: "A",value: "A" ),
OptionElement(label: "B",value: "B" ),
],
),
ButtonElement(//Button element to open web view with given url
elementId : "aboutUs",
buttonText: "About Us" ,
action: URLNavigationAction(
url: "https://www.cometchat.com/",
)
),
],
submitElement : ButtonElement(//Submit button with call to cometchat's managment api to send anoher message
elementId : "button1",
buttonText: "Submit" ,
action: APIAction(
url: "https://${appId}.api-${CometChatConstants.region}.cometchat.io/v3/messages",
method: APIRequestTypeConstants.post,
dataKey: "cometchatData",
headers: header,
payload: payload
)
),
interactionGoal: InteractionGoal(
type: InteractionGoalTypeConstants.allOf,
elementIds: ["button1"]
),
goalCompletionText: "Goal completed !!",
);

Send Form Message

CometChatUIKit.sendFormMessage(formMessage, onSuccess: (FormMessage message){
// TODO("Not yet implemented")
}, onError: (exception){
// TODO("Not yet implemented")
});