UNPKG

@butlerbot/sdk

Version:

The official ButlerBot SDK

44 lines (43 loc) 1.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Conversation = void 0; const config_1 = require("../config"); class Conversation { constructor(config) { this.convoId = config.convoId; this.endpoint = (config.serverUrl || config_1.CONFIG.server) + (config.path || config_1.CONFIG.paths.conversation.v3.base); this.apiKey = config.apiKey; } /** Sends a message into the conversation */ send(message, cb) { return __awaiter(this, void 0, void 0, function* () { const params = { message, api_key: this.apiKey, chatId: this.convoId, }; const paramQuery = new URLSearchParams(params).toString(); const url = `${this.endpoint}?${paramQuery}`; const sse = new EventSource(url); sse.onmessage = (event) => { const data = JSON.parse(event.data); if (data.payload.quitStream) sse.close(); cb(data); }; sse.onerror = (event) => { console.error(event); }; }); } } exports.Conversation = Conversation;