@botonic/core
Version:
Build Chatbots using React
241 lines • 8.34 kB
JavaScript
import { __awaiter } from "tslib";
import axios from 'axios';
import { PATH_PAYLOAD_IDENTIFIER } from './constants';
import { BotonicAction, EVENT_FORMAT_VERSION, } from './models';
const HUBTYPE_API_URL = 'https://api.hubtype.com';
export var HelpdeskEvent;
(function (HelpdeskEvent) {
HelpdeskEvent["StatusChanged"] = "status_changed";
HelpdeskEvent["AgentMessageCreated"] = "agent_message_created";
HelpdeskEvent["QueuePositionChanged"] = "queue_position_changed";
})(HelpdeskEvent || (HelpdeskEvent = {}));
function contextDefaults(context) {
return {
timeoutMs: context.timeoutMs || 10000,
};
}
export function getOpenQueues(session, context = {}) {
return __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 axios({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: endpointUrl,
data: { bot_id: session.bot.id },
timeout: context.timeoutMs,
});
return resp.data;
});
}
export 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 = `${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: 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 __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);
});
}
}
/**
* @deprecated use {@link HandOffBuilder} class instead
*/
export function humanHandOff(session, queueNameOrId = '', onFinish) {
return __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();
});
}
function _humanHandOff(session, queueNameOrId = '', onFinish, agentEmail = '', agentId = '', forceAssignIfNotAvailable = true, autoAssignOnWaiting = false, caseInfo = '', note = '', autoIdleMessage = '', shadowing = false, extraData = undefined, botEvent, subscribeHelpdeskEvents = []) {
return __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 = `${BotonicAction.CreateCase}:${JSON.stringify(params)}`;
});
}
export function storeCaseRating(session, rating, context = {}) {
return __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 axios({
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;
});
}
export function getAvailableAgentsByQueue(session, queueId) {
return __awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const resp = yield axios({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: `${baseUrl}/v1/queues/${queueId}/get_available_agents/`,
});
return resp.data;
});
}
export function getAvailableAgents(session) {
return __awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const botId = session.bot.id;
const resp = yield axios({
headers: {
Authorization: `Bearer ${session._access_token}`,
},
method: 'post',
url: `${baseUrl}/v1/bots/${botId}/get_agents/`,
});
return resp.data;
});
}
export function getAgentVacationRanges(session, { agentId, agentEmail }) {
return __awaiter(this, void 0, void 0, function* () {
const baseUrl = session._hubtype_api || HUBTYPE_API_URL;
const botId = session.bot.id;
const resp = yield axios({
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;
});
}
export function cancelHandoff(session, typification = null) {
let action = BotonicAction.DiscardCase;
if (typification)
action = `${action}:${JSON.stringify({ typification })}`;
session._botonic_action = action;
}
export function deleteUser(session) {
session._botonic_action = BotonicAction.DeleteUser;
}
//# sourceMappingURL=handoff.js.map