openai/chatkit-python
Publicmirrored fromhttps://github.com/openai/chatkit-pythonAvailable
docs/guides/let-your-app-draft-and-send-messages.md
91lines · modecode
| 1 | # Let your app draft and send messages |
| 2 | |
| 3 | Use ChatKit’s commands to let your app pre-fill the composer and send messages programmatically for quick replies, “ask again” buttons, or deep links from the rest of your UI. |
| 4 | |
| 5 | At a high level: |
| 6 | |
| 7 | - `setComposerValue` lets your app draft or edit the pending message. |
| 8 | - `sendUserMessage` lets your app send a message without the user pressing Enter. |
| 9 | |
| 10 | ## Get ChatKit commands from `useChatKit` |
| 11 | |
| 12 | When you call `useChatKit`, you can destructure commands alongside the `control` object you pass into `<ChatKit />`: |
| 13 | |
| 14 | ```tsx |
| 15 | import {ChatKit, useChatKit} from "@openai/chatkit-react"; |
| 16 | |
| 17 | export function Inbox() { |
| 18 | const { |
| 19 | control, |
| 20 | setComposerValue, |
| 21 | sendUserMessage, |
| 22 | setThreadId, |
| 23 | } = useChatKit({ |
| 24 | // ... your normal options (api, history, composer, etc.) |
| 25 | }); |
| 26 | |
| 27 | return <ChatKit control={control} />; |
| 28 | } |
| 29 | ``` |
| 30 | |
| 31 | The commands are safe to call as long as ChatKit is not currently loading a thread or streaming a response; combine them with the loading/response state from [**Keep your app in sync with ChatKit**](keep-your-app-in-sync-with-chatkit.md) when you need to guard calls. |
| 32 | |
| 33 | ## Draft messages with `setComposerValue` |
| 34 | |
| 35 | Use `setComposerValue` to pre-fill or update the composer text from your own UI: |
| 36 | |
| 37 | - Quick-reply chips that insert a suggested reply. |
| 38 | - “Ask again with more detail” buttons that tweak the last question. |
| 39 | - Deep links from outside the chat that open a specific prompt. |
| 40 | |
| 41 | ```tsx |
| 42 | function QuickReplies({ |
| 43 | setComposerValue, |
| 44 | }: { |
| 45 | setComposerValue: (params: {text: string}) => Promise<void>; |
| 46 | }) { |
| 47 | return ( |
| 48 | <div className="quick-replies"> |
| 49 | <button onClick={() => setComposerValue({text: "Can you summarize this thread?"})}> |
| 50 | Summarize this thread |
| 51 | </button> |
| 52 | <button onClick={() => setComposerValue({text: "Explain this like I'm five."})}> |
| 53 | Explain like I'm five |
| 54 | </button> |
| 55 | </div> |
| 56 | ); |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | `setComposerValue` only changes the draft text; the user can still edit it before sending, or you can pair it with `sendUserMessage` to fire immediately. |
| 61 | |
| 62 | ## Send messages with `sendUserMessage` |
| 63 | |
| 64 | Use `sendUserMessage` when your app needs to initiate a turn directly—for example, from a custom toolbar button or a widget action handled on the client. |
| 65 | |
| 66 | ```tsx |
| 67 | export function Inbox() { |
| 68 | const { |
| 69 | control, |
| 70 | sendUserMessage, |
| 71 | setThreadId, |
| 72 | } = useChatKit({ |
| 73 | // ... |
| 74 | }); |
| 75 | |
| 76 | const handleHelpClick = () => { |
| 77 | // Send a canned message from a fresh thread |
| 78 | sendUserMessage({text: "I need help with my billing.", newThread: true}); |
| 79 | }; |
| 80 | |
| 81 | return ( |
| 82 | <> |
| 83 | <button onClick={handleHelpClick}>Contact support</button> |
| 84 | <ChatKit control={control} /> |
| 85 | </> |
| 86 | ); |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | You can also rely on the current active thread when calling `sendUserMessage` without `newThread: true`. |
| 91 | |
| 92 | |