botfriends-handover-nodejs
Version:
NodeJs wrapper for BOTfriends Handover Tool
132 lines • 5.23 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../api");
class HandoverClient {
constructor(jwt, backendUrl) {
if (!jwt || !backendUrl) {
throw new Error('Please provide jwt token and backend url.');
}
this.jwt = jwt;
this.backendUrl = backendUrl;
}
static buildMessage(role, type) {
return {
role,
type
};
}
static callGateway(jwt, backendUrl, data) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield api_1.sendToGateway(jwt, backendUrl, data);
if (!result.success) {
throw new Error(result.message);
}
});
}
sendMessage(userId, message) {
return __awaiter(this, void 0, void 0, function* () {
let messageData = HandoverClient.buildMessage(message.role, 'text');
messageData.text = message.text;
if (message.actions)
messageData.actions = message.actions;
if (message.nlpData)
messageData.nlpData = message.nlpData;
const data = {
userId,
source: message.source,
message: messageData
};
yield HandoverClient.callGateway(this.jwt, this.backendUrl, data);
});
}
sendFile(userId, file, type) {
return __awaiter(this, void 0, void 0, function* () {
let messageData = HandoverClient.buildMessage(file.role, type);
messageData.mediaUrl = file.mediaUrl;
if (file.mediaType)
messageData.mediaType = file.mediaType;
if (file.nlpData)
messageData.nlpData = file.nlpData;
const data = {
userId,
source: file.source,
message: messageData
};
yield HandoverClient.callGateway(this.jwt, this.backendUrl, data);
});
}
sendCarousel(userId, carousel) {
return __awaiter(this, void 0, void 0, function* () {
let messageData = HandoverClient.buildMessage(carousel.role, 'carousel');
messageData.items = carousel.items;
if (carousel.nlpData)
messageData.nlpData = carousel.nlpData;
const data = {
userId,
source: carousel.source,
message: messageData
};
yield HandoverClient.callGateway(this.jwt, this.backendUrl, data);
});
}
sendLocation(userId, location) {
return __awaiter(this, void 0, void 0, function* () {
let messageData = HandoverClient.buildMessage(location.role, 'location');
messageData.coordinates = location.coordinates;
if (location.nlpData)
messageData.nlpData = location.nlpData;
const data = {
userId,
source: location.source,
message: messageData
};
yield HandoverClient.callGateway(this.jwt, this.backendUrl, data);
});
}
getOnlineAgents() {
return __awaiter(this, void 0, void 0, function* () {
const users = yield api_1.checkGatewayStatus(this.jwt, this.backendUrl);
if (!users.success) {
throw new Error(users.message);
}
return users;
});
}
getAllConversationIds() {
return __awaiter(this, void 0, void 0, function* () {
const conversationIdsRes = yield api_1.queryConversationIds(this.jwt, this.backendUrl);
if (!conversationIdsRes.success) {
throw new Error(conversationIdsRes.message);
}
return conversationIdsRes.data;
});
}
getHandoverStatus(userId) {
return __awaiter(this, void 0, void 0, function* () {
const handoverRes = yield api_1.queryHandoverStatus(this.jwt, this.backendUrl, userId);
if (!handoverRes.success) {
throw new Error(handoverRes.message);
}
return handoverRes.data;
});
}
startHandover(userId) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield api_1.sendStartHandover(this.jwt, this.backendUrl, userId);
if (!response.success) {
throw new Error(response.message);
}
return response.data;
});
}
}
exports.HandoverClient = HandoverClient;
//# sourceMappingURL=sdk.js.map