Skip to content

Instantly share code, notes, and snippets.

@divgo
Last active August 10, 2019 02:58
Show Gist options
  • Save divgo/10bbf2bb4432e14db0bb74e64655c164 to your computer and use it in GitHub Desktop.
Save divgo/10bbf2bb4432e14db0bb74e64655c164 to your computer and use it in GitHub Desktop.
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var tableName = "RegisterChatbot_Registrations";
// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
function storeRegistration(intent, callback) {
let userInfo = {};
// store every slot we received as part of registration
Object.keys(intent.currentIntent.slots).forEach((item) => {
console.log(item)
userInfo[item] = {"S": intent.currentIntent.slots[item]};
});
// store a registration date
userInfo.registration_date = {"S": new Date().getTime().toString() };
userInfo.registration_userid = {"S": intent.userId};
dynamodb.putItem({
"TableName": tableName,
"Item" : userInfo
}, function(err, data) {
if (err) {
console.log('Failure storing user info');
console.log(err);
callback(close(intent.sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': "I am sorry, but something went wrong saving your Registration Info. Please try again."}));
} else {
console.log("Successfully Stored UserInfo");
callback(close(intent.sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': "Thank you for registering for our service."}));
}
});
}
// --------------- Main handler -----------------------
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
console.log(event);
try {
storeRegistration(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
};
@xxmafiaxxx
Copy link

"errorMessage": "Cannot read property 'slots' of undefined",
"errorType": "TypeError",
"stackTrace": [
"storeRegistration (/var/task/index.js:23:38)",
"exports.handler (/var/task/index.js:56:9)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment