Skip to content

Instantly share code, notes, and snippets.

@gittimos
Last active June 28, 2020 11:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gittimos/5de9cf3a1394210572ae1ddb73ff9b9f to your computer and use it in GitHub Desktop.
Save gittimos/5de9cf3a1394210572ae1ddb73ff9b9f to your computer and use it in GitHub Desktop.
Using the Lex postText API with AWS Node.js SDK
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var lexruntime = new AWS.LexRuntime();
var params = {
botAlias: '$LATEST', /* required, has to be '$LATEST' */
botName: 'BOT', /* required, the name of you bot */
inputText: 'TEXT', /* required, your text */
userId: 'USER', /* required, arbitrary identifier */
sessionAttributes: {
someKey: 'STRING_VALUE',
/* anotherKey: ... */
}
};
lexruntime.postText(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
@dilanWijerathne
Copy link

u sure this is working in a lambda function ?

@gittimos
Copy link
Author

gittimos commented Aug 7, 2019

Sure, why not?

@dilanWijerathne
Copy link

yes it works thanks

@gittimos
Copy link
Author

Great!

@zhaoyonghe
Copy link

Hi, I am super confused why this code does not work. It seems that the lex does not receive this message. I will be very appreciate you if you can tell me why.
`const AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});

exports.handler = async (event) => {

var lexRuntime = new AWS.LexRuntime({apiVersion: '2016-11-28'});

const params = {
  botAlias: "$LATEST",
  botName: "DiningOrder", 
  inputText: "I would like to find a restaurant",
  userId: "ghgff",
  sessionAttributes: {

  }, 
  requestAttributes: {
      
  }
};
console.log("ghdjfd");
var ew = {a: 3};
lexRuntime.postText(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data); ew = data;         // successful response
});
console.log("g88");
return ew;
/*
let mes = event.messages[0].unstructured.text;

const response = {
    statusCode: 200,
    body: JSON.stringify([mes, "I am still under development. Please come back later."]),
};
return response;*/

};
`

@zhaoyonghe
Copy link

Both two comment can be printed.

@zhaoyonghe
Copy link

This code is written in a lambda function.

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