Skip to main content
Version: v4

Form Message

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

Constructor

NameTypeDescription
receiverIdstringThe ID of the receiver
receiverTypestringThe type of the receiver
titlestringThe title of the form
formFieldsArray<ElementEntity>The fields of the form
submitElementButtonElementThe submit button of the form

Class Usage

How to create an instance of the FormMessage class:

let formMessage = InteractiveMessage()
formMessage.messageCategory = .interactive

let jsonString:[String:Any] = [
"goalCompletionText": "Goal completed YAY",
"title":"By ",
"formFields":[
[
"defaultValue" : "",
"elementId" : "element1",
"elementType" : "textInput",
"label" : "Name",
"maxLines" : 1,
"optional" : false
],
[
"action" : [
"actionType" : "urlNavigation",
"url" : "https://www.cometchat.com/"
],
"buttonText" : "About us",
"disableAfterInteracted" : false,
"elementId" : "element9",
"elementType" : "button"
]
],
"submitElement" : [
"action" : [
"actionType" : "apiAction",
"dataKey" : "CometChatData",
"headers" : [
"Content-Type" : "application/json",
"apiKey" : "",
"appId" : "",
"onBehalfOf" : ""
],
"method" : "POST",
"payload" : [
"category" : "message",
"data" : [
"text" : "Thanks For filling the Form!"
],
"receiver" : "",
"receiverType" : "group",
"type" : "text"
] as [String : Any],
"url" : ""
] as [String : Any],
"buttonText" : "Submit",
"disableAfterInteracted" : true,
"elementId" : "element8",
"elementType" : "button"
] as [String : Any]
]
formMessage.interactiveData = jsonString
let goal = InteractionGoal()
goal.elementIds = ["element9","element8"]
goal.interactionType = .allOf

formMessage.interactionGoal = goal
formMessage.allowSenderInteraction = true
formMessage.type = "form"
formMessage.receiverType = .user
formMessage.receiverUid = "cometchat-uid-1"
formMessage.muid = "1697025959995609"
formMessage.senderUid = CometChat.getLoggedInUser()?.uid ?? ""
formMessage.sender = CometChat.getLoggedInUser()

Send Form Message

CometChat.sendInteractiveMessage(message: formMessage, onSuccess: {
succes in
print("successs",succes)
}, onError: {
error in
print("errorr",error?.description)
})