Messages Injection
Table of contents
Overview
The hosting app has the ability to control and engage with the messages that are being inserted to the chat.
Messages of vary types can be injected into the chat, while some may be configured to be injected by the SDK.
Injecting chat messages
The SDK exposes an API to enable injection of messages into the chat by the hosting app.
Messages can be injected to the chat, once the chat was Accepted.
How to inject messages:
- Implement
didUpdateState
underChatControllerDelegate
. - On chat state
accepted
underdidUpdateState
Create ProactiveChatElement. -
ProactiveChatElement
creation.let element = ProactiveChatElement(text: "{YOUR-TEXT}", type: ChatElementType.{CHAT-ELEMENT-TYPE}) /* {CHAT-ELEMENT-TYPE} OutgoingElement, IncomingBotElement, IncomingLiveElement, SystemMessageElement */
-
Decide if the element should be posted.
Under
ProactiveChatElement
set booleanelement.withPost = {SHOULD-BE-POSTED}
withPost:
if
false
- Simply present the element.if
true
- Present the element, send it and get the response from the server. -
Message injection.
Call
post
underchatController.proactiveChatElementHandler
self.chatController.proactiveChatElementHandler.post(element)
- Final code.
extension BotDemoViewController: ChatControllerDelegate {
func didUpdateState(_ event: ChatStateEvent!) {
switch event.state {
case .preparing:
print("ChatPreparing")
break
case .started:
print("ChatStarted")
case .accepted:
print("ChatAccepted")
let element = ProactiveChatElement(text:"{YOUR-Text}", type: ChatElementType.{TYPE})
element.withPost = {SHOULD-BE-POSTED}
self.chatController.proactiveChatElementHandler.post(element)
break
case .ending:
print("ChatEnding")
break
case .ended:
print("ChatEnded")
break
case .unavailable:
print("ChatUnavailable")
break
case .pending:
print("ChatPending")
break
case .inQueue:
print("ChatInQueue")
break
case .didChangeLanguage:
print("LanguageChanged")
break
@unknown default:
break
}
}
}