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 kindly import combine var cancellables = set\<anycancellable>() kindlysdk sendmessage("hi, i have a question about my order") sink( receivecompletion { completion in if case failure(let error) = completion { print("send failed \\(error)") } }, receivevalue { (message externalchatmessage) in print("sent â server id \\(message id)") } ) store(in \&cancellables) if you don't need the reconciled message, just call it and ignore the promise kindlysdk sendmessage("hi, i have a question about my order") with per call context attach key/value context to this message only â overrides any context previously staged via setnewcontext( ) kindlysdk sendmessage( "where is my order?", newcontext \["orderid" "12345", "tier" "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 kindlysdk events sink { event in if case message(let newmessage, ) = event detail { print("\\(newmessage sender) said \\(newmessage text ?? "")") } } store(in \&cancellables) errors the promise rejects with a kindlysdkerror in these cases error cause sdknotinitialized kindlysdk start( ) was never called chatnotconnected the chat is not currently connected observe kindlysdk state for isconnected == true before calling, or call displaychat() first invaliddata the message text is empty or only whitespace other network or server errors are forwarded as is behaviour summary trims whitespace; empty input rejects with 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