-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Esri/f/context
Added support for persisting context & usage tracking
- Loading branch information
Showing
17 changed files
with
441 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
module.exports = require('./lib/sonar.js'); | ||
const botBuilder = require('claudia-bot-builder') | ||
const sonar = require('./lib/sonar.js'); | ||
|
||
module.exports = botBuilder(sonar, { platforms: ['alexa', 'slackSlashCommand', 'facebook'] }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Persist user context during sessions | ||
// handles "question & answer" abilities | ||
// Also logs usage telemetry for UX/VX evaluation & improvements | ||
|
||
var AWS = require('aws-sdk'); | ||
const tableUser = { | ||
TableName : "sonar-bot-db", | ||
KeySchema: [ | ||
{ AttributeName: "UserID", KeyType: "HASH"} | ||
], | ||
AttributeDefinitions: [ | ||
{ AttributeName: "UserID", AttributeType: "S" }, | ||
], | ||
ProvisionedThroughput: { | ||
ReadCapacityUnits: 10, | ||
WriteCapacityUnits: 10 | ||
} | ||
}; | ||
const tableLog = { | ||
TableName : "sonar-bot-log", | ||
KeySchema: [ | ||
{ AttributeName: "Key", KeyType: "HASH"} | ||
], | ||
AttributeDefinitions: [ | ||
{ AttributeName: "Key", AttributeType: "S" } | ||
], | ||
ProvisionedThroughput: { | ||
ReadCapacityUnits: 10, | ||
WriteCapacityUnits: 10 | ||
} | ||
};; | ||
|
||
|
||
|
||
|
||
|
||
AWS.config.update({ | ||
region: "us-east-1" | ||
// , endpoint: "http://localhost:8000" | ||
}); | ||
|
||
const dynamodb = new AWS.DynamoDB.DocumentClient(); | ||
|
||
// Useful for local development | ||
function setupDB() { | ||
const db = new AWS.DynamoDB; | ||
|
||
|
||
db.createTable(tableUser, function(err, data) { | ||
if (err) { | ||
console.error("Unable to create " + tableUser.TableName + " . Error JSON:", JSON.stringify(err, null, 2)); | ||
} else { | ||
console.log("Created table " + tableUser.TableName + ". Table description JSON:", JSON.stringify(data, null, 2)); | ||
} | ||
}); | ||
|
||
db.createTable(tableLog, function(err, data) { | ||
if (err) { | ||
console.error("Unable to create table " + tableLog.TableName + ". Error JSON:", JSON.stringify(err, null, 2)); | ||
} else { | ||
console.log("Created table " + tableLog.TableName + ". Table description JSON:", JSON.stringify(data, null, 2)); | ||
} | ||
}); | ||
} | ||
|
||
function restoreCtx(sender)//Function will be used later to restore database information for the user that accesses the bot. | ||
{ | ||
// console.log("Trying to restore context for sender", sender); | ||
|
||
var params = { | ||
TableName: tableUser.TableName, | ||
Key: { | ||
'UserID': sender | ||
} | ||
}; | ||
|
||
return dynamodb.get(params).promise(); | ||
} | ||
|
||
function persistCtx(sender, state) // This is used later to repopulate the database with user information | ||
{ | ||
// console.log("Persisting context for sender", sender); | ||
|
||
var params = { | ||
TableName: tableUser.TableName, | ||
Item:{ | ||
'UserID': sender, | ||
'State': state // this is used for persistence. The bot interacts differently with users depending on whether their state is start or fire/trash/map | ||
} | ||
}; | ||
|
||
return dynamodb.put(params).promise(); | ||
} | ||
|
||
function trackUsage(key, timestamp, sender, platform, state, request, response) //Very similar to the persist function, but used to log each user interaction | ||
{ | ||
var params = { | ||
TableName: tableLog.TableName, | ||
Item:{ | ||
'Key': key, | ||
'Timestamp': timestamp, | ||
'UserID': sender, | ||
'State': state, | ||
'Request': request, | ||
'Response': response, | ||
'Platform': platform | ||
} | ||
}; | ||
|
||
return dynamodb.put(params).promise(); | ||
} | ||
|
||
|
||
module.exports = {setupDB, persistCtx, restoreCtx, trackUsage} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const _dayIndex = { | ||
"mon": "Monday", | ||
"tue": "Tuesday", | ||
"wed": "Wednesday", | ||
"thurs": "Thursday", | ||
"thu": "Thursday", | ||
"fri": "Friday", | ||
"sat": "Saturday", | ||
"sun": "Sunday" | ||
} | ||
function days(input) { | ||
|
||
var output = new String(input.toLowerCase()) | ||
var lookup = Object.keys(_dayIndex); | ||
for(var i=0; i<lookup.length; i++) { | ||
output = output.replace(lookup[i], _dayIndex[lookup[i]]) | ||
} | ||
return output; | ||
} | ||
|
||
module.exports = {days} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Returns tokens and metadata about the Alexa devices | ||
|
||
function askPermission() { | ||
return response.card({ | ||
type: "AskForPermissionsConsent", | ||
permissions: [ "read::alexa:device:all:address" ] // full address | ||
}); | ||
} | ||
|
||
function get(request) { | ||
|
||
console.log("env 1: " + JSON.stringify(request)) | ||
// console.log("env 2: " + JSON.stringify(originalRequest)) | ||
|
||
var deviceId = request.originalRequest.context.System.device.deviceId | ||
var userId = request.originalRequest.context.System.user.userId | ||
|
||
console.log("user: " + userId + " | device: " + deviceId) | ||
console.log("user: " + JSON.stringify(request.originalRequest.context.System.user)) | ||
|
||
var consentToken = request.originalRequest.context.System.user.permissions.consentToken | ||
console.log("consentToken: " + consentToken) | ||
|
||
return {deviceId, userId, consentToken} | ||
} | ||
|
||
module.exports = {get, askPermission}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.