Skip to content

Instantly share code, notes, and snippets.

@noriyukitakei
Last active May 14, 2018 02:55
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 noriyukitakei/f34f88cd58ad301a9629367b587ce019 to your computer and use it in GitHub Desktop.
Save noriyukitakei/f34f88cd58ad301a9629367b587ce019 to your computer and use it in GitHub Desktop.
【アレクサ、〇〇さんの今日の予定を教えて】lambdaのコード
'use strict';
const Alexa = require('alexa-sdk');
const http = require('https');
//=========================================================================================================================================
//TODO: The items below this comment need your attention.
//=========================================================================================================================================
//Replace with your app ID (OPTIONAL). You can find this value at the top of your skill's page on http://developer.amazon.com.
//Make sure to enclose your value in quotes, like this: const APP_ID = 'amzn1.ask.skill.bb4045e6-b3e8-4133-b650-72923c5980f1';
const APP_ID = undefined;
const SKILL_NAME = 'Space Facts';
const GET_FACT_MESSAGE = "Here's your fact: ";
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';
//=========================================================================================================================================
//Editing anything below this line might break your skill.
//=========================================================================================================================================
const handlers = {
'LaunchRequest': function () {
this.emit('GetNewFactIntent');
},
'TellMeYourCalendar': function () {
// スロットの値(「Googleカレンダーの〇〇さんの今日の予定を教えて」の〇〇の部分)の値が
// this.event.request.intent.slots.name.valueに入ってきます。
// 日本語が入ってくる場合もあるので、URIエンコードします。
var name = encodeURI(this.event.request.intent.slots.name.value);
// Google Apps Scriptで導入した後に表示されたURL(現在のWebアプリケーションのURL)を
// 設定します。
const uri = '[Google Apps Scriptで導入した後に表示されるURL]';
// Google Apps ScriptのRest APIにアクセスして、JSONを取得する関数を呼びます。
var reqURI = uri + '?name=' + name;
getHttp(reqURI,this);
},
'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');
},
};
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
function getHttp(url,instance) {
// この関数は、Google Apps ScriptのRest APIにアクセスして、JSONを取得して
// 発話をする処理です。再帰的に呼び出しています。Google Apps Scriptは
// 何回かリダイレクトを繰り返して、最後にJSONを表示します。
// node.jsのHTTPクライアントライブラリは、リダイレクトを追従してくれないので、
// 自分でその処理を入れる必要があります。ここでは、HTTPステータスコード301もしくは
// 302が返ってきたら、この関数を再帰的に呼び出し、200が返ってきたら
// 初めてJSONのParseを行います。
var req = http.request(url, (res) => {
if(res.statusCode == 301 ||res.statusCode == 302) {
// HTTPステータスコードがリダイレクトなので、この関数を再帰的に呼び出しています。
getHttp(res.headers.location,instance);
} else if(res.statusCode == 200) {
// HTTPステータスコードが200なので、JSONを解析する処理を実行します。
var body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
var result = JSON.parse(body);
var speech = '';
// JSONに格納された予定をひとずつ取り出し、発話を作成しています。
for (var i in result) {
var startDate = new Date(result[i].start);
var endDate = new Date(result[i].end);
speech += startDate.getHours() + '時' + startDate.getMinutes() + '分から' + endDate.getHours() + '時' + endDate.getMinutes() + '分まで' + result[i].title + 'の予定があります。';
}
// 実際に発話をする処理です
instance.emit(":tell", speech);
});
res.on('error', (e) => {
console.log('problem with request: ' + e.message);
});
}
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment