UNPKG

plazbot

Version:

Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.

68 lines (67 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = void 0; const http_1 = require("./http"); class Message extends http_1.PlazbotHttp { constructor(options) { super(options); } async onWhatsappMessage(params) { const body = { workspaceId: this.workspaceId, recipientPhone: params.to, content: params.message, }; const response = await this.http.post('/api/message', body); this.throwIfError(response, 'Error sending WhatsApp message'); return response.data; } async onConversation(params) { const body = { workspaceId: this.workspaceId, destination: params.to, template: params.template, ...(params.variablesBody && { variablesBody: params.variablesBody }), ...(params.variablesHeader && { variablesHeader: params.variablesHeader }), ...(params.file && { file: params.file }), }; const response = await this.http.post('/api/conversation', body); this.throwIfError(response, 'Error sending template message'); return response.data; } async registerWebhook(params) { const url = `/api/workspace/${this.workspaceId}/whatsapp/numbers/${params.number}/webhook`; const response = await this.http.post(url, { webhook: params.webhookUrl }); this.throwIfError(response, 'Error registering webhook'); return response.data; } async deleteWebhook(params) { const url = `/api/workspace/${this.workspaceId}/whatsapp/numbers/${params.number}/webhook`; const response = await this.http.delete(url); this.throwIfError(response, 'Error deleting webhook'); return response.data; } // ── Message History ──────────────────────────────────────────────── async getMessages(params) { const response = await this.http.get('/api/message', { params: { workspaceId: this.workspaceId, ...params }, }); this.throwIfError(response, 'Error retrieving messages'); return response.data; } async getConversationHistory(params) { const response = await this.http.get('/api/message/get-all', { params: { workspaceId: this.workspaceId, contactId: params.contactId }, }); this.throwIfError(response, 'Error retrieving conversation history'); return response.data; } async searchMessages(params) { const response = await this.http.get('/api/message/get-query-messages', { params: { workspaceId: this.workspaceId, phrase: params.query }, }); this.throwIfError(response, 'Error searching messages'); return response.data; } } exports.Message = Message;