Skip to content

Instantly share code, notes, and snippets.

@furuya02
Created May 31, 2019 22:46
Show Gist options
  • Save furuya02/bfb0584ca98c173a40ba9692a97e66d8 to your computer and use it in GitHub Desktop.
Save furuya02/bfb0584ca98c173a40ba9692a97e66d8 to your computer and use it in GitHub Desktop.
const aws = require('aws-sdk');
exports.handler = async (event) => {
if(event.type != 'req') {
return;
}
const message = await lexClient(event.body, event.userId);
await sendMqtt(message);
};
async function lexClient(message, userId) {
const botAlias = '$LATEST';
const botName = 'OrderFlowers';
const sessionAttributes = {};
const lexruntime = new aws.LexRuntime( {region: 'us-east-1'});
const params = {
botAlias: botAlias,
botName: botName,
inputText: message,
userId: userId,
sessionAttributes: sessionAttributes
};
const data = await lexruntime.postText(params).promise();
console.log(JSON.stringify(data));
return data.message;
}
async function sendMqtt(message) {
const endpoint = 'xxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com';
const topic = "Sample_Topic"
const region = 'ap-northeast-1';
var iotdata = new aws.IotData({
endpoint: endpoint,
region: region
});
const param = {
type: 'res',
body: message
}
var iotParams = {
topic: topic,
payload: JSON.stringify(param),
qos: 0
};
let result = await iotdata.publish(iotParams).promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment