setDraft() to prefill the message box so the visitor can review and send it, or sendMessage() to send it directly. For example, a returns page can open the chat with the order number already filled in.
Neither method opens the chat. Call ChatWidget.open() when the visitor should see it.
Prefill a message
setDraft(text) replaces the text in the message box and keeps any attachments the visitor already added. Pass an empty string to clear the text. It does not send anything and does not save a draft between visits.
Send a message
sendMessage(text) sends the text as the current visitor, in their current conversation. If there is no conversation yet, it starts one. Text the visitor already typed and their attachments are kept.
Results
Both methods resolve to an object with astatus. They do not throw.
When
sendMessage() returns requires_input or failed, it places your text in the message box if the box is empty, so the visitor can send it after completing the form.
Failure codes
Rules to follow
- Do not retry automatically. A result of
unknown, or atimeoutafter sending, can mean the message was delivered. Retrying can send it twice. - One call at a time. Calls from the same page run in order. While one message is still sending, the next can return
busy. - Send once per action. If your page sends a message on load, for example after a redirect, make sure it does not send again when the page re-renders or the visitor comes back.
- Contact and verification steps still apply. If the channel requires a contact form or OTP, the visitor must complete it before a message is sent.
- Identify first. If the visitor is a signed-in customer, wait for
identify()to finish before sending. See Identity. - Plain text only. Both methods accept a text string. To send a file, the visitor attaches it in the chat.
Example: open chat from a link with context
A page that opens chat about a specific order when the visitor arrives from an email link such ashttps://example.com/help?order=12345: