Getting timestamp of individual alexa responses in dynamodb - node.js

I am creating an Alexa factskill that asks the user for some information on their health, and users respond with a score from 1-10 depending on the level of pain in different areas. I then input the data into a DynamoDB table, which stores the information(score out of 10) for the four health questions (swelling, feeling, sleeping, breathing) and the user ID. However, the entries are not giving a timestamp for when they were created. I was wondering if there was a way to make a timestamp for preferably every health question response, but also a timestamp for the entire entry would help. Would I have to use any external SDKs, as I was looking up DynamoDB documentations and didn't find out any way to add a timestamp.
Below is my index.js code for my Lambda function that is used for my Alexa skill.
'use strict';
const Alexa = require('alexa-sdk');
const SKILL_NAME = 'Home Assist';
const HELP_MESSAGE = 'You can say I want to input my data';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';
const handlers = {
'LaunchRequest': function () {
this.emit('HomeAssistQuestions');
},
'HomeAssistQuestions': function () {
this.attributes.healthscores = {
'patientID' : 0,
'scores': {
'feeling': {
'score': 0
},
'sleeping': {
'score': 0
},
'breathing': {
'score': 0
},
'swollen': {
'score': 0
}
}
};
if(this.event.request.dialogState !== 'COMPLETED'){
this.emit(':delegate');
}
else{
const feelingScore = this.event.request.intent.slots.feelingRating.value;
const sleepingScore = this.event.request.intent.slots.sleepingRating.value;
const breathingScore = this.event.request.intent.slots.breathingRating.value;
const swollenScore = this.event.request.intent.slots.SwollenRating.value;
const id = this.event.request.intent.slots.id.value;
this.attributes.healthscores.patientID = id;
this.attributes.healthscores.scores['feeling'].score = feelingScore;
this.attributes.healthscores.scores['sleeping'].score = sleepingScore;
this.attributes.healthscores.scores['breathing'].score = breathingScore;
this.attributes.healthscores.scores['swollen'].score = swollenScore;
this.response.speak("Health Scores Recorded");
this.emit(':responseReady');
}
},
'AMAZON.HelpIntent': function () {
const speechOutput = HELP_MESSAGE;
const reprompt = HELP_REPROMPT;
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'SessionEndedRequest': function(){
this.emit('saveState', true);
}
};
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.dynamoDBTableName = 'HealthScores';
alexa.APP_ID = "amzn1.ask.skill.d5b8d597-eb50-41c6-a22d-b0f18c23b544";
alexa.registerHandlers(handlers);
alexa.execute();
};

You can try something like this:
...
this.attributes.healthscores.scores['swollen'].score = swollenScore;
this.attributes.healthscores.timestamp = Date.now();
this.response.speak("Health Scores Recorded");
...

Related

AWS lambda sending sns message to phone number

I have a lambda function running node.js The function makes database calls and sends a text message via sns. It has the following structure
the functions -> index.js file looks like this
async function sendTextMessage(message, phoneNumber) {
try {
console.log("hitSendFunction");
const sns = new AWS.SNS();
const params = {
Message: message,
MessageStructure: "string",
PhoneNumber: phoneNumber
};
//remember phone number must have +1 before it.
return await sns.publish(params).promise();
} catch (error) {
console.log(error);
}
}
module.exports = {
sendTextMessage
};
that function is than called in the main index.js file:
const database = require("./db");
const { sendTextMessage } = require("./functions");
const AWS = require("aws-sdk");
AWS.config.region = "us-east-1";
exports.handler = async function (event, context) {
try {
console.log("hit");
const result = await database.query("call getTicker()");
const data = result[0][0];
const currentProjectEndDate = new Date(
data.currentProjectEndDate
).getTime();
const now = new Date().getTime();
console.log("data.currentProjectEndDate", data.currentProjectEndDate);
const runningHot =
data.jobsInQueue > 0 && currentProjectEndDate <= now && data.textSent < 1;
if (runningHot) {
const numbers = ["+1435994****"];
for (let i = 0; i < numbers.length; i++) {
let number = numbers[i];
console.log("number", number);
let messageResult = await sendTextMessage(
"The CE Bot is running hot from lambda",
number
);
console.log("messageResult", messageResult);
}
await database.query("call insertIntoTextSent()");
console.log("yes");
}
} catch (error) {
console.log("this was the error", error);
}
The database calls work correctly but the textMessage function just hangs and times out. The lambda function has the following permissions attached to it:
Finally even though I do not think it is needed as the db code is working here is what the db -> index.js file looks like:
const mysql = require("mysql");
const util = require("util");
const awsConfig = {
host: process.env.RDS_HOST,
user: process.env.RDS_USER,
password: process.env.RDS_PASSWORD,
database: process.env.RDS_DATABASE
};
const connection = mysql.createConnection(awsConfig);
connection.query = util.promisify(connection.query.bind(connection));
connection.end = util.promisify(connection.end.bind(connection));
module.exports = connection;
I am not quite sure where I am going wrong here. Can someone point me in the right direction?
You can just send it via sms in sns just type in the phone number

Async Await issues with NodeJS & AWS Lambda

I'm currently developing a Lamba function call for my AWS project, but with me not being a master at asynchronous functions it appears it's falling apart, the code I've put together is:
const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");
AWS.config.update({
region: "us-east-1"
});
exports.handler = async (event, context, callback) => {
//set db
var documentClient = new AWS.DynamoDB.DocumentClient()
//params
const params = {
Item: {
'id': uuid.v1(),
'player_1_name': null,
'player_1_network': null,
'player_1_matches': 0,
'player_1_kills': 0,
'player_1_last_updated': 0,
'player_2_name': null,
'player_2_network': null,
'player_2_matches': 0,
'player_2_kills': 0,
'player_2_last_updated': 0,
'match_id': 0,
'status': 0
},
TableName : 'matches'
};
var matchData = JSON.parse(event.body);
//player 1
const player_1_name = matchData.player_1_name ? matchData.player_1_name : null;
const player_1_network = matchData.player_1_network ? matchData.player_1_network : null;
//player 2
const player_2_name = matchData.player_2_name ? matchData.player_2_name : null;
const player_2_network = matchData.player_2_network ? matchData.player_2_network : null;
//match data
const match_id = matchData.match_id ? matchData.match_id : 0;
//game object
let gameAPI = new game(
[
"email#email.com",
"password"
]
);
//gameAPI.login() returns a Promise()
await gameAPI.login().then(() => {
//check stats for player 1, getStats returns a Promise()
gameAPI.getStats(player_1_name, player_1_network).then(stats => {
params.Item.player_1_matches = stats.lifetimeStats.matches;
params.Item.player_1_kills = stats.lifetimeStats.kills;
}).catch(err => {
//error! we must work out what to do here!
console.log(err);
});
//example insert
documentClient.put(params, function(err, data){
return callback(err, data);
});
}).catch(err => {
console.log("We failed to login!");
console.log(err);
});
};
This logic seems flawed since nothing is being thrown to my AWS logs? my idea is to send the request to the function & have it do it as quickly as possible so I can send a 200 response back to Lambda, can anyone point me in the correct direction?
When using async/await you don't need to use callback neither you need to fall into the Promise Hell.
Just await on your promises and grab the result. The great advantage here is it looks as though your code is synchronous.
Here's your refactored code:
const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");
AWS.config.update({
region: "us-east-1"
});
exports.handler = async (event) => {
//set db
var documentClient = new AWS.DynamoDB.DocumentClient()
//params
const params = {
Item: {
'id': uuid.v1(),
'player_1_name': null,
'player_1_network': null,
'player_1_matches': 0,
'player_1_kills': 0,
'player_1_last_updated': 0,
'player_2_name': null,
'player_2_network': null,
'player_2_matches': 0,
'player_2_kills': 0,
'player_2_last_updated': 0,
'match_id': 0,
'status': 0
},
TableName : 'matches'
};
var matchData = JSON.parse(event.body);
//player 1
const player_1_name = matchData.player_1_name ? matchData.player_1_name : null;
const player_1_network = matchData.player_1_network ? matchData.player_1_network : null;
//player 2
const player_2_name = matchData.player_2_name ? matchData.player_2_name : null;
const player_2_network = matchData.player_2_network ? matchData.player_2_network : null;
//match data
const match_id = matchData.match_id ? matchData.match_id : 0;
//game object
let gameAPI = new game(
[
"email#email.com",
"password"
]
);
//gameAPI.login() returns a Promise()
await gameAPI.login()
const stats = await gameAPI.getStats(player_1_name, player_1_network)
params.Item.player_1_matches = stats.lifetimeStats.matches;
params.Item.player_1_kills = stats.lifetimeStats.kills;
//example insert
await documentClient.put(params).promise();
};
If you need to handle the exceptions (you should) just wrap your await calls in try/catch blocks, like this:
try {
console.log(await somePromise)
} catch (e) {
console.log(e)
}
The snippet above is equivalent to:
somePromise.then(console.log).catch(console.log)
with the great difference that you don't need to chain promises/asynchronous code in order to keep the execution order, so I highly recommend you pick the async/await approach and forget about .then().catch()

alexa implement CanFulfillIntentRequest in node.js

Alexa has released CanFulfillIntentRequest feature or Name-free Interaction for custom skills recently. I am trying to implement it in my existing skill which uses alexa-sdk. Please find my code below:
'use strict';
const Alexa = require('alexa-sdk');
var handlers = {
'LaunchRequest': function() {
var speechOutput = "You can ask me to read out quotes from Steve Jobs";
var repromptText = "Sorry I didnt understand";
this.emit(':tell', speechOutput, repromptText);
},
'RandomQuote': function() {
let data = getQuoteFunction();
const author = data[0];
const quote = data[1];
let cardTitle = "Quotation from author";
let cardContent = "Actual quote";
let speechOutput = "Actual quote";
// Speak out the output along with card information
this.emit(':tellWithCard', speechOutput, cardTitle, cardContent);
}
}
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.registerHandlers(handlers);
alexa.execute();
};
Do we need to add handler for CanFulfillIntentRequest, the way I did for other handlers ? for example:
var handlers = {
'LaunchRequest': function() {
},
'RandomQuote': function() {
},
'CanFulfillIntentRequest': function() {
//code to handle
}
}
Is this feature only available in ASK SDK v2 for Node.js ? Can we implement it in skill developed using alexa-sdk. Could anyone please let me know ?
Thanks

AWS Lambda cannot publish to IoT topic properly

I have written Lambda function using JavaScript, which responses to my voice and turns on the LED on my Raspberry.
But I have a problem with publishing its state to my thing topic. While Alexa responses correct ("Turning on" if Im asking to turn it on and "Turning off" if asking to off), my topic doesn't always get the state changes. Some times it gets data and sometime it doesn't and after few more invocations it gets data in bulk, and I cant even get the logic of creating a sequence of data in that bulk.
var AWS = require('aws-sdk');
var config = {};
config.IOT_BROKER_ENDPOINT = "xxxxxx.iot.us-east-1.amazonaws.com";
config.IOT_BROKER_REGION = "us-east-1";
config.IOT_THING_NAME = "raspberry";
var iotData = new AWS.IotData({endpoint: config.IOT_BROKER_ENDPOINT});
var topic = 'LED';
exports.handler = function (event, context) {
...
...
function updatePowerState (intent, session, callback) {
var speechOutput = '';
var newValue = '';
var repromptText = '';
const cardTitle = 'Power';
var sessionAttributes = {};
const shouldEndSession = true;
var value = intent.slots.PowerState.value;
if(value == 'on' || value == 'off') {
newValue = value.toUpperCase();
speechOutput = 'Turning your lamp ' + value;
updateShadow(newValue);
} else {
speechOutput = 'I didnt understand you. Please, repeat your request.';
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function updateShadow(newValue) {
let payload = {
state: {
desired: {
power_state: newValue
}
}
};
var JSON_payload = JSON.stringify(payload);
var updates = {
topic: topic,
payload: JSON_payload,
qos: 0
};
iotData.publish(updates, (err, data) => {
if(err) {
console.log(err);
}
else {
console.log('Success!');
}
});
}
Do you have any ideas about its causes? Thank you!
Async methods like iotData.publish cause problems into AWS Lambda, because you request a execution and the lambda function ends soon without waiting for the response and processing the request.
Another problem could be your permissions.
var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({endpoint: 'iotCoreEndpoint.iot.us-east-1.amazonaws.com'});
exports.handler = async (event) => {
var params = {
topic: 'topic/topicName',
payload: JSON.stringify(event.body),
qos: 0
};
await iotdata.publish(params).promise()
};
Just make sure to add the required permissions or you can attach the following policy to your lambda role: AWSIoTWirelessFullPublishAccess

Alexa skill which can read Facebook posts using AWS Lambda, Node.js and the Alexa Skills Kit

I am developing an Alexa skill using AWS Lambda, Node.js and the Alexa Skills Kit.I am using a forked from skill-sample-nodejs-fact project & successfully deployed & tested the sample fact project .Now I am trying to modify that code to read posts on some Facebook feeds.First I tried to develop some node application which can read posts & it was successful.Please find below code for your reference.I used fb module -
https://www.npmjs.com/package/fb
const FB = require('fb');
FB.setAccessToken('abc');
const query='cnninternational/posts';
FB.api(query, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
});
Next, I tried to integrate above code block into the lambda function.Unfortunately, I was unable, to read Facebook posts using these codes.Please find those code blocks in the below panel .Also, I checked cloudwatch logs as well.I can see the "GetNewsIntent", but I didn't see "fb-init" , "fb-error" or "fb-exit"entries in logs.Surprisingly, no error in logs as well.I would much appreciate it if someone can help to solve that issue.
'use strict';
const Alexa = require('alexa-sdk');
const FB = require('fb');
const APP_ID = 'abc';
const SKILL_NAME = 'test';
const GET_FACT_MESSAGE = "Here's your news: ";
const STOP_MESSAGE = 'Goodbye!';
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function () {
this.emit('GetNewsIntent');
},
'GetNewsIntent': function () {
console.log('GetNewsIntent');
const speechOutput = GET_FACT_MESSAGE;
const query='cnninternational/posts';
FB.setAccessToken('abc');
FB.api(query, function (res) {
console.log('fb-init');
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
console.log('fb-error');
return;
}
console.log(res);
speechOutput = speechOutput + res;
console.log('fb-exit');
});
this.response.cardRenderer(SKILL_NAME, speechOutput);
this.response.speak(speechOutput);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
};
Have you implemented account linking? You should be using event.session.user.accessToken for the parameter to setAccessToken().
I have removed this.response.cardRenderer , this.response.speak & changed the code bit.It's working fine now.Please find the below code snippet which can be used to read posts on the BBC Facebook page.
var accessToken = '';
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'NewSession': function() {
var welcomeMessage = "Welcome to Athena";
welcomeMessage = welcomeMessage +"<break time=\"1s\"/>"+ "<audio src='https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3' />"+"<break time=\"1s\"/>";
welcomeMessage += HELP_MESSAGE;
accessToken = this.event.session.user.accessToken;
if (accessToken) {
FB.setAccessToken(accessToken);
this.emit(':ask', welcomeMessage, HELP_REPROMPT);
}
else {
// If we don't have an access token, we close down the skill.
this.emit(':tellWithLinkAccountCard', "This skill requires you to link a Facebook account. Seems like you are not linked to a Facebook Account. Please link a valid Facebook account and try again.");
}
},
'LaunchRequest': function () {
this.emit('NewSession');
},
'ReadBbcNewsFacebookPostsIntent': function () {
var alexa = this;
FB.api("bbcnews/posts", function (response) {
if (response && !response.error) {
if (response.data) {
var output = "Here are recent posts" + "<break time=\"1s\"/>";
var max = 5;
for (var i = 0; i < response.data.length; i++) {
if (i < max) {
output += "<break time=\"1s\"/>" + "Post " +
(i + 1) +
response.data[i].message.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '')
+ ". ";
}
}
alexa.emit(':ask', output+ ", What would you like to do next?",HELP_MESSAGE);
} else {
// REPORT PROBLEM WITH PARSING DATA
}
} else {
// Handle errors here.
console.log(response.error);
this.emit(':tell', EMPTY_ACCESS_TOKEN_MESSAGE, TRY_AGAIN_MESSAGE);
}
});
}
};

Resources