CAIConversation

public final class CAIConversation : MessagingPublisher

This class is the standard implementation of MessagingPublisher for connecting to a bot of the CAI platform via the Channel Connector.

  • Declaration

    Swift

    public typealias Output = Result<ResponseMessageData, Error>
  • Declaration

    Swift

    public typealias Failure = Never
  • CAI Conversation ID. Read-only getter to get the current conversation ID set with this object. It’s created for you in load() or you can pass it in constructor

    Declaration

    Swift

    public private(set) var conversationID: String? { get }

Lifecycle

  • Creates a new CAI conversation object.

    Declaration

    Swift

    public convenience init(config: CAIServiceConfig, channelId: String, channelToken: String?, withExistingConvID conversationID: String? = nil)

    Parameters

    config

    The configuration object holding central config properties

    channelId

    The channel ID (from channel connector)

    channelToken

    The channel connector token

    conversationID

    An existing conversation ID (stored somewhere externally). If this is supplied, no new conversation will be created in the backend. Optional. Default is nil hence a request to the backend will be triggered to create one.

  • Creates a new CAI conversation object. Convenience init using CAIChannel struct

    Declaration

    Swift

    public convenience init(config: CAIServiceConfig, channel: CAIChannel, withExistingConvID conversationID: String? = nil)

    Parameters

    config

    CAIServiceConfig

    channel

    CAIChannel. Channel you want to create a conversation to

    conversationID

    An existing conversation ID (stored somewhere externally). If this is supplied, no new conversation will be created in the backend. Optional. Default is nil hence a request to the backend will be triggered to create one.

  • Creates a new CAI conversation object. Convenience init using CAIChannel struct

    Declaration

    Swift

    public convenience init(config: CAIServiceConfig, channel: CAIChannel, messageDelivery: MessageDelivering, withExistingConvID conversationID: String? = nil)

    Parameters

    config

    CAIServiceConfig

    channel

    CAIChannel. Channel you want to create a conversation to

    messageDelivery

    Method / UI Protocol on how to retrieve messages from backend. Polling & WebSocket are supported.

    conversationID

    An existing conversation ID (stored somewhere externally). If this is supplied, no new conversation will be created in the backend. Optional. Default is nil hence a request to the backend will be triggered to create one.

Public API

  • Declaration

    Swift

    public func load()
  • Reset an existing conversation. Next time a message is sent, this will trigger a creation of a new conversation.

    Declaration

    Swift

    public func resetConversation()
  • Reset an existing conversation and create a new conversation.

    Declaration

    Swift

    public func resetAndCreateConversation()
  • Post a text message to CAI platform

    Declaration

    Swift

    public func postMessage(text: String, memoryOptions: MemoryOptions? = nil)

    Parameters

    text

    String

  • Post a postback action to CAI platform. Used by buttons and quickReplies

    Declaration

    Swift

    public func postMessage(type: PostbackType, postbackData: PostbackData, memoryOptions: MemoryOptions? = nil)

    Parameters

    type

    UIModelPostbackRequestDataType

    postbackData

    ButtonData

  • Creates a conversation in the backend and returns the newly created conversation ID

    Declaration

    Swift

    public func createConversation(_ completionHandler: @escaping ((Result<String, CAIError>) -> Void))

    Parameters

    completionHandler

    Handler function

  • Returns a publisher that emits the current conversationId. If no conversationId is available, it will trigger a backend call to create a new one.

    Declaration

    Swift

    public func getConversationId() -> AnyPublisher<String, CAIError>
  • Loads content for an existing conversation ID

    Declaration

    Swift

    public func loadConversation(_ conversationId: String, _ completionHandler: @escaping (Result<CAIResponseData, CAIError>) -> Void)

    Parameters

    conversationId

    String

    completionHandler

    Handler function called once finished

Publisher impl

Combin-ify APIs

  • Loads a conversation and returns a Publisher. Always emits on Main thread.

    Declaration

    Swift

    func loadConversation(_ conversationId: String) -> AnyPublisher<CAIResponseData, CAIError>

    Parameters

    conversationId

    String