Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// This is only initialized when the Lambda is, so it is preserved across calls
// It is NOT a real database, but can be used for testing, as JavaScript Lambdas tend to live for a few hours
// Stay tuned for a more sophisticated example that uses DynamoDB
var lastPlayedByUser = {};
var podcastURL = "https://feeds.soundcloud.com/stream/309340878-user-652822799-episode-010-building-an-alexa-skill-with-flask-ask-with-john-wheeler.mp3";
const bst = require("bespoken-tools");
bst.Logless.Domain = "logless-dev.bespoken.tools";
// Entry-point for the Lambda
exports.handler = bst.Logless.capture("a756512f-d091-477a-a4f9-fd57c916787a", function(event, context) {
var player = new SimplePlayer(event, context);
player.handle();
});
// The SimplePlayer has helpful routines for interacting with Alexa, within minimal overhead
var SimplePlayer = function (event, context) {
this.event = event;
this.context = context;
};
// Handles an incoming Alexa request
SimplePlayer.prototype.handle = function () {
var requestType = this.event.request.type;
var userId = this.event.context ? this.event.context.System.user.userId : this.event.session.user.userId;
// On launch, we tell the user what they can do (Play audio :-))
.withPersistenceAdapter(dynamoDbPersistenceAdapter)
.lambda();
// wrap all log services
if (process.env.DASHBOT_KEY) {
// for the moment doesn't work
// because of https://github.com/actionably/dashbot/issues/28
lambda = dashbot(process.env.DASHBOT_KEY).alexa.handler(lambda);
} else {
warning('env variable DASHBOT_KEY should be defined to send logs to dashbot');
}
if (process.env.BESPOKEN_KEY) {
// FIXME: bespoken doesn't support ANSI color escape codes yet
lambda = bst.Logless.capture(process.env.BESPOKEN_KEY, lambda);
} else {
warning('env variable BESPOKEN_KEY should be defined to send logs to bespoken');
}
return lambda;
};
error('error on global error handler', err);
if (conv.raven) {
conv.raven.captureException(err);
}
}
}
// last chance to give response to user
if (!globalErrorWasHandled) {
conv.ask('Can you rephrase it?');
}
await after.handle(conv);
});
if (_.has(functions.config(), ['bespoken', 'key'])) {
return functions.https.onRequest(bst.Logless.capture(functions.config().bespoken.key, app));
} else {
warning('bespoken key missing\n');
return functions.https.onRequest(app);
}
};
// This is only initialized when the Lambda is, so it is preserved across calls
// It is NOT a real database, but can be used for testing, as JavaScript Lambdas tend to live for a few hours
// Stay tuned for a more sophisticated example that uses DynamoDB
var lastPlayedByUser = {};
var podcastURL = "https://feeds.soundcloud.com/stream/309340878-user-652822799-episode-010-building-an-alexa-skill-with-flask-ask-with-john-wheeler.mp3";
const bst = require("bespoken-tools");
bst.Logless.Domain = "logless-dev.bespoken.tools";
// Entry-point for the Lambda
exports.handler = bst.Logless.capture("a756512f-d091-477a-a4f9-fd57c916787a", function(event, context) {
var player = new SimplePlayer(event, context);
player.handle();
});
// The SimplePlayer has helpful routines for interacting with Alexa, within minimal overhead
var SimplePlayer = function (event, context) {
this.event = event;
this.context = context;
};
// Handles an incoming Alexa request
SimplePlayer.prototype.handle = function () {
var requestType = this.event.request.type;
deployPassThru = async () => {
await this.loadBespokenPluginConfig();
this.serverless.cli.log("cli options", this.options);
// do nothing if inject-passthru not passed on command line
if (!this.injectPassThruOption) {
return;
}
this.serverless.cli.log(
"Replacing lambda handlers with bespoken passthru functions"
);
const bespokenProxyUrl = URLMangler.manglePipeToPath(
Global.config().sourceID()
);
const bespokenProxySecret = Global.config().secretKey();
// inject environment variables with necessary bespoken connection parameters into lambda environment
this.mutateLambdaEnvironmentVariables({
bespoken_proxy_url: bespokenProxyUrl,
bespoken_proxy_secret: bespokenProxySecret
});
this.serverless.cli.log(
`Pass through handlers will proxy requests to: ${bespokenProxyUrl}`
);
const handlers = this.handlers;
const handler = extractHandlerObject(
this.functionsFromServerlessConfig,
this.selectedFunctionFromCommandLine
);
this.serverless.cli.log(
`Server configured in single function mode.Requests will resolve via: ${
handler.file
}: ${handler.exportedFunction}`
);
this.proxy = BSTProxy.lambda(handler.file, handler.exportedFunction);
} else {
this.serverless.cli.log(
"Server configured with passthru routing. Url of requests will be interpreted as serverless handler specifications and dispatched to lambda function based on filesystem path."
);
// run local server in 'directory mode' -- requests are dispatched to appropriate lambda by mapping url to filesystem path
this.proxy = BSTProxy.lambda();
}
// enable secure mode if enabled
if (this.enableSecurity) {
this.proxy.activateSecurity();
}
// start the lambda proxy
this.proxy.start(async () => {
// HACK: reach into proxy and grab the moduleManager instance
const moduleManager = (this.proxy as any).lambdaServer
.moduleManager as ModuleManager;
// stop watching with watcher that uses node's fs.watch and use chokidar instead to allow for recursive symlink traversal
(moduleManager as any).watcher.close();
);
// create the lambda proxy
if (this.withPassThruRoutingOption == null) {
// run local server in 'single function mode' -- all requests are dispatched to a single lambda specified either on command line or
// by choosing first function from serverless config ...
const handler = extractHandlerObject(
this.functionsFromServerlessConfig,
this.selectedFunctionFromCommandLine
);
this.serverless.cli.log(
`Server configured in single function mode.Requests will resolve via: ${
handler.file
}: ${handler.exportedFunction}`
);
this.proxy = BSTProxy.lambda(handler.file, handler.exportedFunction);
} else {
this.serverless.cli.log(
"Server configured with passthru routing. Url of requests will be interpreted as serverless handler specifications and dispatched to lambda function based on filesystem path."
);
// run local server in 'directory mode' -- requests are dispatched to appropriate lambda by mapping url to filesystem path
this.proxy = BSTProxy.lambda();
}
// enable secure mode if enabled
if (this.enableSecurity) {
this.proxy.activateSecurity();
}
// start the lambda proxy
this.proxy.start(async () => {
// HACK: reach into proxy and grab the moduleManager instance
this.serverless.cli.log("cli options", this.options);
// do nothing if inject-passthru not passed on command line
if (!this.injectPassThruOption) {
return;
}
this.serverless.cli.log(
"Replacing lambda handlers with bespoken passthru functions"
);
const bespokenProxyUrl = URLMangler.manglePipeToPath(
Global.config().sourceID()
);
const bespokenProxySecret = Global.config().secretKey();
// inject environment variables with necessary bespoken connection parameters into lambda environment
this.mutateLambdaEnvironmentVariables({
bespoken_proxy_url: bespokenProxyUrl,
bespoken_proxy_secret: bespokenProxySecret
});
this.serverless.cli.log(
`Pass through handlers will proxy requests to: ${bespokenProxyUrl}`
);
const handlers = this.handlers;
// save directory where serverless expects to look for source files
this.originalServicePath = this.originalServicePath
? this.originalServicePath
proxyStart = async () => {
// create the bespoken config file if properties are specified in serverless config
await this.loadBespokenPluginConfig();
// initialize the bespoken cli
await Global.initializeCLI();
// enable verbose logging
LoggingHelper.setVerbose(true);
// ensure environment variables from serverless config are set
mutateProcessEnvironmentVariables(
this.environmentVariablesFromServerlessConfig
);
// create the lambda proxy
if (this.withPassThruRoutingOption == null) {
// run local server in 'single function mode' -- all requests are dispatched to a single lambda specified either on command line or
// by choosing first function from serverless config ...
const handler = extractHandlerObject(
this.functionsFromServerlessConfig,
this.selectedFunctionFromCommandLine
loadBespokenPluginConfig = async () => {
if (this.pluginConfig) {
this.serverless.cli.log("Configuring bespoken to use parameters from serverless.yml")
const directory = `${homedir()}/.bst`
ensureDirSync(directory);
writeJsonSync(`${directory}/config`, {
...this.pluginConfig,
version: "1.0.7"
}, { spaces: 2 });
}
// parse the bespoken config
await Global.loadConfig();
};