UNPKG

@prass/botpress-native

Version:

A simple and powerful SDK for integrating Botpress Chat API with React Native,

47 lines (44 loc) 1.61 kB
import { prepareHeaders } from '../../utils/prepareHeaders.js'; import { prepareData } from '../../utils/prepareData.js'; import { handleError } from '../../utils/errorHandler.js'; import { z } from 'zod'; const CreateMessageParamsSchema = z.object({ conversationId: z.string().nonempty("Conversation ID is required"), payload: z.any().refine((val) => val !== undefined, "Payload is required"), }); /** * Internal handler for creating a new message in a conversation. * @param this - Botpress instance context * @param params - Parameters containing conversationId and payload * @returns Promise resolving to the created message details * @throws Error if prerequisites are missing or API call fails */ async function handleCreateMessage({ conversationId, payload }) { if (!this.userKey) throw new Error(this.errors.userNotCreated); const { conversationId: cid, payload: pld } = CreateMessageParamsSchema.parse({ conversationId, payload, }); try { const url = this.ChatApiBaseUrl.getUrl(["messages"]); const { data } = await this.axiosInstance.request({ url, method: "POST", headers: prepareHeaders({ "x-user-key": this.userKey, }), data: prepareData({ conversationId: cid, payload: pld, }), timeout: this.config.timeout, }); return data; } catch (e) { return handleError(e, "CreateMessage"); } } export { handleCreateMessage }; //# sourceMappingURL=createMessage.js.map