@botonic/core
Version:
Build Chatbots using React
253 lines • 9.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteUser = exports.cancelHandoff = exports.getAgentVacationRanges = exports.getAvailableAgents = exports.getAvailableAgentsByQueue = exports.storeCaseRating = exports.humanHandOff = exports.HandOffBuilder = exports.getOpenQueues = exports.HelpdeskEvent = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const constants_1 = require("./constants");
const models_1 = require("./models");
const HUBTYPE_API_URL = 'https://api.hubtype.com';
var HelpdeskEvent;
(function (HelpdeskEvent) {
HelpdeskEvent["StatusChanged"] = "status_changed";
HelpdeskEvent["AgentMessageCreated"] = "agent_message_created";
HelpdeskEvent["QueuePositionChanged"] = "queue_position_changed";
})(HelpdeskEvent = exports.HelpdeskEvent || (exports.HelpdeskEvent = {}));
function contextDefaults(context) {
return {
timeoutMs: context.timeoutMs || 10000,
};
}
function getOpenQueues(session, context = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
//be aware of https://github.com/axios/axios/issues/1543
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const endpointUrl = `${baseUrl}/v1/queues/get_open_queues/`;
context = contextDefaults(context);
const resp = yield (0, axios_1.default)({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: endpointUrl,
data: { bot_id: session.bot.id },
timeout: context.timeoutMs,
});
return resp.data;
});
}
exports.getOpenQueues = getOpenQueues;
class HandOffBuilder {
constructor(session) {
this._session = session;
}
withQueue(queueNameOrId) {
this._queue = queueNameOrId;
return this;
}
withOnFinishPayload(payload) {
this._onFinish = payload;
return this;
}
withOnFinishPath(path) {
this._onFinish = `${constants_1.PATH_PAYLOAD_IDENTIFIER}${path}`;
return this;
}
withAgentEmail(email) {
this._email = email;
return this;
}
withAgentId(agentId) {
this._agentId = agentId;
return this;
}
withForceAssignIfNotAvailable(forceAssign) {
this._forceAssignIfNotAvailable = forceAssign;
return this;
}
withAutoAssignOnWaiting(autoAssignOnWaiting) {
this._autoAssignOnWaiting = autoAssignOnWaiting;
return this;
}
withNote(note) {
this._note = note;
return this;
}
withCaseInfo(caseInfo) {
this._caseInfo = caseInfo;
return this;
}
withAutoIdleMessage(message) {
this._autoIdleMessage = message;
return this;
}
withShadowing(shadowing = true) {
this._shadowing = shadowing;
return this;
}
withExtraData(extraData) {
this._extraData = extraData;
return this;
}
withBotEvent(botEvent) {
this._bot_event = {
format_version: models_1.EVENT_FORMAT_VERSION,
flow_id: botEvent.flowId,
flow_name: botEvent.flowName,
flow_node_id: botEvent.flowNodeId,
flow_node_content_id: botEvent.flowNodeContentId,
};
return this;
}
withSubscribeHelpdeskEvents(events) {
this._subscribeHelpdeskEvents = events;
return this;
}
handOff() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return _humanHandOff(this._session, this._queue, this._onFinish, this._email, this._agentId, this._forceAssignIfNotAvailable, this._autoAssignOnWaiting, this._caseInfo, this._note, this._autoIdleMessage, this._shadowing, this._extraData, this._bot_event, this._subscribeHelpdeskEvents);
});
}
}
exports.HandOffBuilder = HandOffBuilder;
/**
* @deprecated use {@link HandOffBuilder} class instead
*/
function humanHandOff(session, queueNameOrId = '', onFinish) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const builder = new HandOffBuilder(session);
if (queueNameOrId) {
builder.withQueue(queueNameOrId);
}
if (onFinish) {
if (onFinish.path) {
builder.withOnFinishPath(onFinish.path);
}
else if (onFinish.payload) {
builder.withOnFinishPayload(onFinish.payload);
}
else {
throw new Error('onFinish requires payload or path field');
}
}
return builder.handOff();
});
}
exports.humanHandOff = humanHandOff;
function _humanHandOff(session, queueNameOrId = '', onFinish, agentEmail = '', agentId = '', forceAssignIfNotAvailable = true, autoAssignOnWaiting = false, caseInfo = '', note = '', autoIdleMessage = '', shadowing = false, extraData = undefined, botEvent, subscribeHelpdeskEvents = []) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const params = {};
params.force_assign_if_not_available = forceAssignIfNotAvailable;
if (queueNameOrId) {
params.queue = queueNameOrId;
}
if (agentEmail) {
params.agent_email = agentEmail;
}
if (agentId) {
params.agent_id = agentId;
}
if (autoAssignOnWaiting) {
params.auto_assign_on_waiting = autoAssignOnWaiting;
}
if (caseInfo) {
params.case_info = caseInfo;
}
if (note) {
params.note = note;
}
if (autoIdleMessage) {
params.auto_idle_message = autoIdleMessage;
}
if (shadowing) {
params.shadowing = shadowing;
}
if (onFinish) {
params.on_finish = onFinish;
}
if (extraData) {
params.case_extra_data = extraData;
}
if (botEvent) {
params.bot_event = botEvent;
}
if (subscribeHelpdeskEvents.length > 0) {
params.subscribe_helpdesk_events = subscribeHelpdeskEvents;
}
session._botonic_action = `${models_1.BotonicAction.CreateCase}:${JSON.stringify(params)}`;
});
}
function storeCaseRating(session, rating, context = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const chatId = session.user.id;
context = contextDefaults(context);
const resp = yield (0, axios_1.default)({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: `${baseUrl}/v1/chats/${chatId}/store_case_rating/`,
data: { chat_id: chatId, rating },
timeout: context.timeoutMs,
});
return resp.data;
});
}
exports.storeCaseRating = storeCaseRating;
function getAvailableAgentsByQueue(session, queueId) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const resp = yield (0, axios_1.default)({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: `${baseUrl}/v1/queues/${queueId}/get_available_agents/`,
});
return resp.data;
});
}
exports.getAvailableAgentsByQueue = getAvailableAgentsByQueue;
function getAvailableAgents(session) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const botId = session.bot.id;
const resp = yield (0, axios_1.default)({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: `${baseUrl}/v1/bots/${botId}/get_agents/`,
});
return resp.data;
});
}
exports.getAvailableAgents = getAvailableAgents;
function getAgentVacationRanges(session, { agentId, agentEmail }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const botId = session.bot.id;
const resp = yield (0, axios_1.default)({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'get',
url: `${baseUrl}/v1/bots/${botId}/get_agent_vacation_ranges/`,
params: { agent_id: agentId, agent_email: agentEmail },
});
return resp.data;
});
}
exports.getAgentVacationRanges = getAgentVacationRanges;
function cancelHandoff(session, typification = null) {
let action = models_1.BotonicAction.DiscardCase;
if (typification)
action = `${action}:${JSON.stringify({ typification })}`;
session._botonic_action = action;
}
exports.cancelHandoff = cancelHandoff;
function deleteUser(session) {
session._botonic_action = models_1.BotonicAction.DeleteUser;
}
exports.deleteUser = deleteUser;
//# sourceMappingURL=handoff.js.map