Send a Message Programmatically
drive the chat from the host app send a message as the user without going through the input field useful for guided flows, in app actions ("ask kindly about my order"), or composing a message from contextual ui usage import androidx lifecycle lifecyclescope import kotlinx coroutines launch import no kindly chatsdk chat kindlysdk lifecyclescope launch { try { val message = kindlysdk sendmessage("hi, i have a question about my order") await() println("sent â server id ${message id}") } catch (e exception) { println("send failed ${e message}") } } if you don't need the reconciled message back, just call it and ignore the deferred kindlysdk sendmessage("hi, i have a question about my order") you can also observe completion without a coroutine via invokeoncompletion kindlysdk sendmessage("hi") invokeoncompletion { error > if (error != null) println("send failed $error") } with per call context attach key/value context to this message only â overrides any context previously staged via setnewcontext( ) kindlysdk sendmessage( message = "where is my order?", newcontext = mapof("orderid" to "12345", "tier" to "premium"), ) important do not also display the message yourself the sdk renders the bubble locally with a temporary id the moment you call sendmessage , then reconciles with the server assigned id when the post response returns if you also add the message to a chat log you maintain, you'll see it twice the message also fires through the unified event stream import no kindly chatsdk chat models kindlyevent lifecyclescope launch { kindlysdk events collect { event > if (event detail is kindlyevent detail message) { val msg = (event detail as kindlyevent detail message) newmessage println("${msg sender} ${msg text}") } } } errors the deferred completes exceptionally with a kindlysdkerror in these cases error cause kindlysdkerror sdknotinitialized kindlysdk start( ) was never called kindlysdkerror chatnotconnected the chat is not currently connected observe kindlysdk state for isconnected == true before calling, or call launchchat(context) first kindlysdkerror invaliddata the message text is empty or only whitespace kindlysdkerror apierror / other network or server errors are forwarded behaviour summary trims whitespace; empty input completes exceptionally with kindlysdkerror invaliddata optimistic local render â bubble appears immediately with a temp id posts to /message , reconciles temp id â server id on success emits kindlyevent message for the user's message (consumers of kindlysdk events see it like any other message) per call newcontext overrides global setnewcontext( ) for this single call works whether or not the chat ui is currently displayed â the message is delivered, and when the user opens the chat next, they see it in place