ChatProvider

ChatProvider Class reference for Kustomer Chat iOS Core API .

public class ChatProvider

shared

public static let shared: ChatProvider

The central object for managing non-UI Kustomer chat related activities.

Listeners

listeners

public var listeners: [String : KUSChatListener]

All currently registered KUSChatListeners.

addChatListener(_:)

public func addChatListener(_ listener: KUSChatListener) -> String

Register a KusChatListener to the Core SDK to receive chat events

removeChatListener(_:)

public func removeChatListener(_ uuid: String)

Unregister a KUSChatListener using its unique String identifier

removeAllChatListeners()

public func removeAllChatListeners()

Removes all registered KUSChatListeners from the Core SDK

listenForTyping(conversationId:)

public func listenForTyping(conversationId: String)

Begins listening for typing events.

stopListeningForTyping(conversationId:)

public func stopListeningForTyping(conversationId: String)

Reading data

getConversations(completion:)

public func getConversations(completion: @escaping (Result<[KUSConversation], KError>) -> Void)

Fetch all conversations for the current customer.

getConversations()

❗️

Deprecated: Use getConversations(completion:) instead.

public func getConversations() -> [KUSConversation]

All conversations for the current customer.

Immediately returns last known list of conversations for the current customer from the SDK’s persistent offline storage.

getConversation(conversationId:completion:)

public func getConversation(conversationId: String, completion: @escaping (Result<KUSConversation, KError>) -> Void)

Calls the completion with a Result containing the KUSConversation object for the given id in the success case, or an error in the failure case.

getConversation(conversationId:)

❗️

Deprecated: Use getConversation(conversationId:completion:) instead.

public func getConversation(conversationId: String) -> KUSConversation?

Returns a KUSConversation object for the given id. Returns nil if the ID is not found.

chatMessages(conversationId:)

❗️

Deprecated: Use getChatMessages(conversationId:completion:) instead.

public func chatMessages(conversationId: String) -> [KUSChatMessage]

Immediately returns last known list of messages for a conversation. Use a KUSChatListener to be notified when new messages come in.

getChatMessages(conversationId:completion:)

public func getChatMessages(conversationId:String, completion: @escaping (Result<[KUSChatMessage],KError>) -> Void)

Gets the historical messages for a conversation using a network call to Pubnub history (up to 30 days)

getUnreadCount(completion:)

 public func getUnreadCount(completion: @escaping (Result<Int, KError>) -> Void)

Fetches the unread count and calls the completion with a Result containing the unread count in the success case or an error in the failure case. Use a KUSChatListener to be notified when the unread count changes.

unreadCount()

❗️

Deprecated: Use getUnreadCount(completion:) instead.

public func unreadCount() -> Int

Immediately returns the current customer’s unread count. Use a KUSChatListener to be notified when this changes.

getOpenConversationCount(completion:)

public func getOpenConversationCount(completion: @escaping (Result<Int, KError>) -> Void)

Fetches the open conversation count and calls the completion with a Result containing the count in the success case or an error in the failure case.

openConversationCount()

❗️

Deprecated: Use getOpenConversationCount(completion:) instead.

public func openConversationCount() -> Int

currentCustomer()

❗️

Deprecated: Use customerExists() instead.

public func currentCustomer() -> KUSCustomer?

Information about the current customer. Value is persisted across application restarts.

Is nil if you have not successfully called logIn(...) and there are no anonymous chats.

Anonymous chats are chats created by users who have not called logIn(...).

After logOut(...) this becomes nil.

customerExists()

public func customerExists() -> Bool

Returns true if there is a current Customer (either anonymous or logged-in) in the Kustomer system.

getChatSettings()

public func getChatSettings() -> KUSChatSettings

Returns chat settings. Does not attempt to load or update them from the REST API

isChatAvailable(_:)

public func isChatAvailable(_ callback: ((Bool) -> Void))

Check the “turned on/off” status of your chat within Business Hours and outside of Holidays settings asynchronously. For example, if chat is turned off or outside of business hours, you may want to turn off the button or deflect customers to contact an email).

If you change your business hours or holidays in the admin panel, you must restart the app to see the new values.

Writing data

createConversation(text:completion:)

  public func createConversation(firstCustomerMessage text: String,
                                 completion: @escaping ((Result<(conversation: KUSConversation, messages: [KUSChatMessage], customer: KUSCustomer?), KError>) -> Void))

Creates a conversation with a message.

Important: the KUSChatMessages returned in messages may or may not have been sent successfully yet. check their .status. and use the observer on new message create to detect when they are.

sendChatMessage(action:conversationId:completion:)

Reply to a message from a chat assistant using an action (action == a quick reply button)

public func sendChatMessage(action: KUSMessageAction, conversationId: String, completion _outerCompletion: @escaping (Result<(message: KUSChatMessage, conversation: KUSConversation), KError>) -> Void)

sendChatMessage(action:completion:)

❗️

Deprecated: Use sendChatMessage(action:conversationId:completion:) instead.

Reply to a message from a chat assistant using an action (action == a quick reply button)

public func sendChatMessage(action: KUSMessageAction, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)

sendChatMessage(text:assistant:conversationId:completion:)

Sends a text message as a reply to a chat assistant question.

public func sendChatMessage(text: String, assistant: KUSAssistant, conversationId: String, completion _outerCompletion: @escaping (Result<(message: KUSChatMessage, conversation: KUSConversation), KError>) -> Void)

📘

For non-conversational-assistant conversations, use sendChatMessage(text:String, conversationId:String, completion: @escaping ((KError?) -> Void)) instead.

sendChatMessage(text:assistant:completion:)

❗️

Deprecated: Use sendChatMessage(text:assistant:conversationId:completion:) instead.

Sends a text message as a reply to a chat assistant question.

public func sendChatMessage(text: String, assistant: KUSAssistant, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)

sendChatMessage(mllNode:conversationId:completion:)

Reply to a message from a chat assistant using an MLL node

public func sendChatMessage(mllNode: KUSMLLNode, conversationId: String, completion _outerCompletion: @escaping (Result<(message: KUSChatMessage, conversation: KUSConversation), KError>) -> Void)

sendChatMessage(mllNode:completion:)

❗️

Deprecated: Use sendChatMessage(mllNode:conversationId:completion:) instead.

Reply to a message from a chat assistant using an MLL node

public func sendChatMessage(mllNode: KUSMLLNode, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)

sendChatMessage(image:conversationId:completion:)

Posts a message containing an image to a conversation.

public func sendChatMessage(image:UIImage,
                                conversationId:String,
                                completion: @escaping ((KError?) -> Void))

sendChatMessage(fileUrl:conversationId:completion:)

Posts a message containing a file or video to a conversation.

public func sendChatMessage(fileUrl:URL,
                                conversationId:String,
                                completion: @escaping ((KError?) -> Void))

createAssistant(id:after:error:afterCreateConversation:)

Creates a new KUSAssistant. Fetches the properties needed to use this assistant. Gets a list of initialMessages that are the same format as KUSChatMessage. A conversation (KUSConversation) on our system (that shows up on the Kustomer website) is not created. A KUSConversation is only created after you submit a message using this assistant.

1.  When a conversation is created, you will get notifications for each message in `initialMessage`s, but they will have different `id`s and potentially `createdAt` dates.

2.  `initialMessages` is a template for what you should show your customer. They aren’t actual `KUSChatMessages` that are on our platform.
 public func createAssistant(id: String,
                                after: @escaping ((KUSAssistant) -> Void),
                                error: @escaping ((KError) -> Void),
                                afterCreateConversation: ((KUSConversation)->Void)? = { _ in }) -> String

endConversation(conversationId:completion:)

Ends an existing conversation.

public func endConversation(conversationId: String, completion: @escaping ((Result<KUSConversation, KError>) -> Void))

reloadChatSettings(_:)

Reloads chat settings and business schedules from our REST API. If the network call fails, returns an error.

public func reloadChatSettings(_ callback: @escaping ((KError?) -> Void))

describeConversation(conversationId:attributes:_:)

Attach custom attributes to the user’s most recent conversation (or the first one they create).

See Describe Conversation.

public func describeConversation(conversationId: String, attributes: [String : Any], _ completion: @escaping ((Result<Void, KError>) -> Void))

NOTE: These key-value pairs must be enabled on the Conversation Klass via the admin portal. This can be done by an admin via Settings > Platform Settings > Klasses > Conversation

- attributes: A dictionary with the custom information for the Session. You can pass this any key/value pair and the backend will add that information as Custom fields. The type name is appended to the end of the custom field. So if you see a custom string field on your admin dashboard called `favoriteColor`, you should pass `favoriteColorStr` to this method. Suffixes are: `Num` for numbers, `Tree` for multi-level lists, `Bool` for true/false, `Url` for urls, `Str` for strings.

Example:

`self.describe(attributes: ["aNumberNum": 10, "aMlListTree": "scotland", "aDateAt":Date(), "aTrueFalseBool": true, "aUrlUrl":"https://www.apple.com", "favoriteColorStr":"Blue"]) { result in switch result { case .success: kprint("Ok") case .failure(let error): kprint(error.localizedDescription) if case let .describeUnknownParameter(parameter: p, details: details) = error { kprint("unknown param is \(p)") kprint(details!) } if case let .describeParameterWrongType(parameter: p, details: details) = error { kprint("wrong type param is \(p)") kprint(details!) } } }`

describeCurrentCustomer(phone: email:phones:emails:facebook:instagram:twitter:linkedIn:custom:_:)

Attach custom attributes to the customer.

See Describe Customer.

public func describeCurrentCustomer(phone: String? = nil, email: String? = nil, phones: [String]? = nil, emails: [String]? = nil, facebook: String? = nil, instagram: String? = nil, twitter: String? = nil, linkedIn: String? = nil, custom: [String : Any]? = nil, _ completion: ((Result<Void, KError>) -> Void)? = nil)

📘

Attached key-value pairs via the custom property must be enabled on the Customer Klass via the admin portal. This can be done by an admin via Settings > Platform Settings > Klasses > Customer, and works the same way as describeConversation

markRead(conversationId:)

Marks a conversation as read as of right now. Any message received for this conversation after this will be treated as unread, until marked read again. This function should be called, whenever the user closes the chat view for a conversation.

public func markRead(conversationId: String)

Identifying

See Log in and authentication.

identify(jwt:_:)

❗️

Deprecated:See Log in and authentication.

willCrash()

public func willCrash()

Did this page help you?