plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
74 lines (73 loc) • 2.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Message = void 0;
const axios_1 = __importDefault(require("axios"));
class Message {
constructor(options) {
this.workspaceId = options.workspaceId;
this.apiKey = options.apiKey;
const zone = options.zone ?? "LA";
if (zone !== "EU" && zone !== "LA") {
throw new Error("Invalid zone. Must be 'EU' or 'LA'.");
}
this.mcpUrl = options.customUrl
?? (zone === "EU" ? "https://apieu.plazbot.com" : "https://api.plazbot.com");
this.http = axios_1.default.create({
baseURL: this.mcpUrl,
headers: {
...(this.apiKey && { 'Authorization': `Bearer ${this.apiKey}` }),
'x-workspace-id': this.workspaceId
}
});
}
//Metodo que envia un mensaje a la IA y luego este lo envia al numero de Whatsapp.
async onWhatsappMessage(params) {
const body = {
workspaceId: this.workspaceId,
recipientPhone: params.to,
content: params.message
};
const response = await this.http.post('/api/message', body);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error enviando mensaje WhatsApp: ${response.statusText}`);
}
return response.data;
}
async onConversation(params) {
const body = {
workspaceId: this.workspaceId,
destination: params.to,
template: params.template,
variablesBody: params.variables,
sendType: 3
};
const response = await this.http.post('/api/conversation', body);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error enviando conversación: ${response.statusText}`);
}
return response.data;
}
async registerWebhook(params) {
const url = `/api/workspace/${this.workspaceId}/whatsapp/numbers/${params.number}/webhook`;
const body = {
webhook: params.webhookUrl
};
const response = await this.http.post(url, body);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error registrando el webhook: ${response.statusText}`);
}
return response.data;
}
async deleteWebhook(params) {
const url = `/api/workspace/${this.workspaceId}/whatsapp/numbers/${params.number}/webhook`;
const response = await this.http.delete(url);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error eliminando el webhook: ${response.statusText}`);
}
return response.data;
}
}
exports.Message = Message;