Conversation

In this section you will learn how to create and launch the conversation.

Create and Launch Conversation

One of the easiest ways to create and launch the conversation is to use the below method:


Kommunicate.createAndShowConversation(from: self) { error in
    guard error == nil else {
        print("Conversation error: \(error.debugDescription)")
        return
    }
    
}

If there are no conversations present, then this method will create a new conversation where the default agent and the logged in user will be present. If a conversation is already there, then it will open that conversation. In case there are multiple conversations present then the conversation list will be shown to the user.

For passing agents or bots, you’ve to check out the New Conversation section

If you want to customise it like you want to create your own flow, then check out the Custom Flow section below.

Send a message programmatically

You can send a message programmatically, it will be useful when you want to send a message on the user’s behalf. The following code snippet shows how you can send a message and open that conversation where the message was sent.


let conversationId = ""
let message = KMMessageBuilder()
    .withConversationId(conversationId)
    .withText("Message from the user.")
    .build()

Kommunicate.sendMessage(message: message) { error in
    guard error == nil else {
        print("Failed to send message: \(String(describing: error?.localizedDescription))")
        return
    }
    Kommunicate.showConversationWith(groupId: conversationId, from: self) { shown in
        print("Conversation shown: \(shown)")
    }
}

Sync conversations on app launch

Below code is used to sync conversations from server whenever app is launched. Put below lines in your AppDelegate's didFinishLaunchingWithOptions method.

KMPushNotificationHandler.shared.dataConnectionNotificationHandlerWith(Kommunicate.defaultConfiguration)
let kmApplocalNotificationHandler: KMAppLocalNotification =  KMAppLocalNotification.appLocalNotificationHandler()
kmApplocalNotificationHandler.dataConnectionNotificationHandler()

Custom Flow

Using this method you can create your own custom conversation flow. Let's say you want to create a new conversation every time when the user clicks on support, then use the API mentioned in the New Conversation section below.

Open Conversation List

To open a list of conversations use below method. Reference to current view controller needs to be passed.

  Kommunicate.showConversations(from: self)

New Conversation

To create a new conversation, use createConversation method as shown below. It allows you to create a new conversation by combining different options using KMConversationBuilder.

let kmConversation = KMConversationBuilder()
    
    
    

    
    .withAgentIds( [""])
     
    
    .withBotIds([""])

    
    .withConversationAssignee("") 

    .useLastConversation(false)
    .build()

Kommunicate.createConversation(conversation: kmConversation) { result in
    switch result {
    case .success(let conversationId):
        print("Conversation id: ",conversationId)
    
    case .failure(let kmConversationError):
        print("Failed to create a conversation: ", kmConversationError)
    }
}

Open Conversation

Open any particular conversation by passing the conversationId and reference to current view controller. You will get the conversationId after creating a conversation as described in the New Conversation section.

 Kommunicate.showConversationWith(
     groupId: conversationId,
     from: self,
     showListOnBack: false, 
     completionHandler: { success in
     print("conversation was shown")
 })

Set prefilled text while opening a conversation

The prefilled text can be set while opening a conversation. This will be shown in the input text bar, so the user has to tap the send button instead of typing a message. This will be useful if you are launching a chat from a particular screen where you'll know what a user will be typing. We've added this in the showConversation API as shown below:

Kommunicate.showConversationWith(
    groupId: conversationId,
    from: self,
    prefilledMessage: "This message will be prefilled in the input text bar",
    completionHandler: { success in
        print("conversation was shown")
    }
)

Rich Message

Rich message provide a better overall conversational experience to the users, make the interface look pretty, they also drive more actions from your users and provide a good conversational experience. There are a variety of response types to choose from. For example, you can show images, play videos, provide buttons, list, forms, or card carousels.

Refer the following link to use rich messages in Kommunicate.

Last updated