# Sending messages with Zoom Chatbot API # Sending messages with Zoom Chatbot API In this section, you'll implement a utility function that sends messages from your chatbot to Chat using the Chatbot API. This function constructs a message body, retrieves a chatbot access token, and sends a `POST` request to Zoom's `/im/chat/messages` endpoint. By the end of this section, you’ll have: - A reusable function to send messages to Chat - Support for replying to messages using message threading - Console logging for debugging message payloads and errors --- ## Implement message sending logic Create a utility function to send a chat message using the Chatbot API. The function accepts the recipient JID, message content, and an optional message ID for threaded replies. ### File Path `utils/zoom-api.js` ### Code Snippet ```javascript /** * Send a message to Zoom Team Chat * @param {string} toJid - Recipient JID (user or channel) * @param {string} message - Message content * @param {string} [replyTo] - Optional message ID to reply to * @returns {Promise} API response */ export async function sendChatMessage(toJid, message, replyTo = null) { try { const accessToken = await getChatbotToken(); const body = { account_id: "5YQGYK-FSc-0M0DgZTCzkQ", content: { head: { text: "Hello World", }, body: [ { type: "message", text: message, }, ], }, robot_jid: "v1nhrah1hrqhuexexb5nn5-a@xmpp.zoom.us", to_jid: toJid, }; if (replyTo) { body.reply_to = replyTo; } console.log("Preparing to send message to Chat:", body); const response = await fetch(`${ZOOM_API_BASE_URL}/im/chat/messages`, { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, body: JSON.stringify(body), }); if (!response.ok) { const error = await response.json(); throw new Error( `Failed to send message: ${error.message || response.statusText}`, ); } return response.json(); } catch (error) { console.error("Error sending chat message:", error); throw error; } } ```