voxa-dashbot
Version:
Integrate Dashbot analytics into your Alexa apps using the voxa framework
261 lines • 12.1 kB
JavaScript
;
/*
* Copyright (c) 2018 Rain Agency <contact@rain.agency>
* Author: Rain Agency <contact@rain.agency>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
var dashbot_1 = __importDefault(require("dashbot"));
var lodash_1 = __importDefault(require("lodash"));
var request_promise_1 = __importDefault(require("request-promise"));
var defaultConfig = {
ignoreUsers: [],
};
var dashbotIntegrations = {
alexa: "alexa",
botframework: "generic",
dialogflow: "google",
google: "google",
};
function register(voxaApp, config) {
var _this = this;
var pluginConfig = lodash_1.default.merge({}, defaultConfig, config);
var dashbotConfig = {
debug: pluginConfig.debug,
printErrors: pluginConfig.printErrors,
redact: pluginConfig.redact,
timeout: pluginConfig.timeout,
};
voxaApp.onRequestStarted(initDashbot);
voxaApp.onRequestStarted(trackIncoming);
voxaApp.onBeforeReplySent(function (voxaEvent, reply, transition) { return __awaiter(_this, void 0, void 0, function () {
var input;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (voxaEvent.dashbot) {
input = voxaEvent.dashbot.input;
}
return [4 /*yield*/, trackOutgoing(voxaEvent, reply, transition, input)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
var alexaRequestTypes = [
"AudioPlayer.PlaybackStarted",
"AudioPlayer.PlaybackFinished",
"AudioPlayer.PlaybackNearlyFinished",
"AudioPlayer.PlaybackStopped",
"AudioPlayer.PlaybackFailed",
"System.ExceptionEncountered",
"PlaybackController.NextCommandIssued",
"PlaybackController.PauseCommandIssued",
"PlaybackController.PlayCommandIssued",
"PlaybackController.PreviousCommandIssued",
"AlexaSkillEvent.ProactiveSubscriptionChanged",
"AlexaSkillEvent.SkillAccountLinked",
"AlexaSkillEvent.SkillEnabled",
"AlexaSkillEvent.SkillDisabled",
"AlexaSkillEvent.SkillPermissionAccepted",
"AlexaSkillEvent.SkillPermissionChanged",
"AlexaHouseholdListEvent.ItemsCreated",
"AlexaHouseholdListEvent.ItemsUpdated",
"AlexaHouseholdListEvent.ItemsDeleted",
"Connections.Response",
"Display.ElementSelected",
"CanFulfillIntentRequest",
"GameEngine.InputHandlerEvent",
"Alexa.Presentation.APL.UserEvent",
"Alexa.Presentation.APLT.UserEvent",
"Messaging.MessageReceived",
];
for (var _i = 0, alexaRequestTypes_1 = alexaRequestTypes; _i < alexaRequestTypes_1.length; _i++) {
var requestType = alexaRequestTypes_1[_i];
if (lodash_1.default.has(voxaApp, "on" + requestType)) {
voxaApp["on" + requestType](initDashbot);
voxaApp["on" + requestType](trackIncoming);
voxaApp["on" + requestType](trackOutgoing);
}
}
function initDashbot(voxaEvent) {
if (!shouldTrack(voxaEvent)) {
return;
}
var platform = voxaEvent.platform;
var apiKey = lodash_1.default.get(pluginConfig, platform.name) || pluginConfig.api_key;
var outgoingIntent = {};
voxaEvent.dashbot = {
promises: [],
trackEvent: function (dashbotEvent) {
return __awaiter(this, void 0, void 0, function () {
var requestBody;
return __generator(this, function (_a) {
requestBody = __assign(__assign({}, dashbotEvent), {
userId: voxaEvent.user.userId,
conversationId: voxaEvent.session.sessionId,
});
voxaEvent.dashbot.promises.push(request_promise_1.default.post({
uri: "https://tracker.dashbot.io/track",
qs: {
platform: dashbotIntegrations[platform.name],
v: "11.1.0-rest",
type: "event",
apiKey: apiKey,
},
json: true,
body: requestBody,
}));
return [2 /*return*/];
});
});
},
addInputs: function (outgoingInputs) {
var input = lodash_1.default.get(outgoingInputs, "input");
outgoingIntent.input = outgoingInputs;
if (input) {
outgoingIntent.input = input;
}
},
get input() {
return outgoingIntent.input;
},
};
}
function trackIncoming(voxaEvent) {
if (!shouldTrack(voxaEvent)) {
return;
}
var rawEvent = voxaEvent.rawEvent, platform = voxaEvent.platform;
var apiKey = lodash_1.default.get(pluginConfig, platform.name) || pluginConfig.api_key;
var Dashbot = dashbot_1.default(apiKey, dashbotConfig)[dashbotIntegrations[platform.name]];
voxaEvent.dashbot.promises.push(Dashbot.logIncoming(rawEvent));
}
function trackOutgoing(voxaEvent, reply, transition, input) {
return __awaiter(this, void 0, void 0, function () {
var rawEvent, platform, apiKey, Dashbot, says, intent;
return __generator(this, function (_a) {
if (!shouldTrack(voxaEvent)) {
return [2 /*return*/];
}
rawEvent = voxaEvent.rawEvent, platform = voxaEvent.platform;
apiKey = lodash_1.default.get(pluginConfig, platform.name) || pluginConfig.api_key;
Dashbot = dashbot_1.default(apiKey, dashbotConfig)[dashbotIntegrations[platform.name]];
if (transition && transition.say) {
says = transition.say;
if (!lodash_1.default.isArray(transition.say)) {
says = [transition.say];
}
if (lodash_1.default.isPlainObject(input)) {
input = [input];
}
intent = says.join(",");
reply = __assign(__assign({}, reply), {
intent: {
name: intent,
input: input,
},
});
}
if (isGoogleAssistant(voxaEvent)) {
reply = lodash_1.default.merge({}, reply, {
payload: {
google: {
userStorage: JSON.stringify(__assign(__assign({}, JSON.parse(voxaEvent.dialogflow.conv.user._serialize())), { dashbotUser: {
userId: voxaEvent.user.userId,
} })),
},
},
});
}
voxaEvent.dashbot.promises.push(Dashbot.logOutgoing(rawEvent, reply));
return [2 /*return*/, Promise.all(voxaEvent.dashbot.promises)];
});
});
}
function shouldTrack(voxaEvent) {
for (var _i = 0, _a = pluginConfig.ignoreUsers; _i < _a.length; _i++) {
var ignoreRule = _a[_i];
if (voxaEvent.user.userId.match(ignoreRule)) {
return false;
}
}
if (pluginConfig.suppressSending) {
return false;
}
return true;
}
}
exports.register = register;
function isGoogleAssistant(voxaEvent) {
return voxaEvent.platform.name === "google";
}
//# sourceMappingURL=Voxa-Dashbot.js.map