Skip to content

Instantly share code, notes, and snippets.

@r3-yamauchi
Last active February 18, 2018 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r3-yamauchi/8e94f2e477c43a498f8f3d1df9121ade to your computer and use it in GitHub Desktop.
Save r3-yamauchi/8e94f2e477c43a498f8f3d1df9121ade to your computer and use it in GitHub Desktop.
これは https://blog.r3it.com/ に投稿した Alexa プッシュ通知の 送信例Lambda Functionです。 https://blog.r3it.com/alexa-day-2018-73928e3b3923
'use strict';
const Alexa = require('alexa-sdk');
const request = require('request');
exports.handler = function(event, context, callback) {
const requestBody = {
"grant_type": "client_credentials",
"client_id": process.env.clientid,
"client_secret": process.env.secret,
"scope": "alexa:skill_messaging"
};
const request_options = {
"Content-Type": "application/x-www-form-urlencoded",
url: `https://api.amazon.com/auth/O2/token`,
method: "POST",
gzip: false,
json: requestBody
};
request(request_options, function(error, response, body) {
console.log("[client_credentials] error: ", JSON.stringify(error, null, 2));
console.log("[client_credentials] response: ", JSON.stringify(response, null, 2));
console.log("[client_credentials] body: ", JSON.stringify(body, null, 2));
const requestid = response.headers["x-amzn-requestid"];
const SKILL_MESSAGING_TOKEN = body["access_token"];
const ALEXA_USER_ID = process.env.ALEXA_USER_ID;
const requestBody = {
"data": {
"notificationTitle": "HelloTitle",
"spokenOutput": 'プッシュ通知を送ります。</lang>'
},
"expiresAfterSeconds": 60
};
// POST
const request_options = {
"Content-Type": "application/json; charset=UTF-8",
url: `https://api.fe.amazonalexa.com/v1/skillmessages/users/${ALEXA_USER_ID}`,
method: "POST",
gzip: false,
headers: {
"Authorization": `Bearer ${SKILL_MESSAGING_TOKEN}`
},
json: requestBody
};
request(request_options, function(error, response, body) {
console.log("[skillmessages] error: ", JSON.stringify(error, null, 2));
console.log("[skillmessages] response: ", JSON.stringify(response, null, 2));
console.log("[skillmessages] body: ", JSON.stringify(body, null, 2));
return callback(null, {
statusCode: response.statusCode,
body: JSON.stringify(response),
headers: {
"Content-Type": "application/json; charset=utf-8"
},
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment