質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Alexa

Alexa(アレクサ)は、米アマゾンが開発したクラウドベースのAIアシスタント。Amazon EchoやEcho dotに搭載され、話かけると音楽を再生したり、天気予報やスケジュールなど様々な情報を提供します。

Q&A

0回答

482閲覧

Alexa 通知の方法について

miyasu

総合スコア21

Alexa

Alexa(アレクサ)は、米アマゾンが開発したクラウドベースのAIアシスタント。Amazon EchoやEcho dotに搭載され、話かけると音楽を再生したり、天気予報やスケジュールなど様々な情報を提供します。

0グッド

0クリップ

投稿2019/06/10 12:57

Alexaの通知である「プロアクティブイベントAPI」を使って通知機能を実装しようと思っているのですが、通知イベントの結果がHTTPステータス400を返します。
下記がサンプルコードです。
ここを参考に作りました。
https://qiita.com/tekuto_tk/items/84e7f9c5a710e364e2b0

認証後のトークンの取得は無事できています。
最後、通知を行う「https.request」でエラーステータスを受信しているのですが、原因がわかりません。
サイト上では「パラメータが違う」とありますがどこが違うのかわかりません。
イベントで「AMAZON.OrderStatus.Updated」を設定しているのですが、対応するインテントが必要なのでしょうか?
Alexaスキルのインテントにはテスト用の「HelloWorld」しか構築していません。

■下記コード実行時の結果


Error https response: 400
requestId: 5cb5a0b2-029e-7830-36b1-41dafcf2d6a5


■実行コード
// Alexa Proactive API sample script
const https = require('https');
const mode = 'dev'; // or 'prod'

const clientID = "xxxxxx";
const clientSecret = "xxxxxx";
let userId1 = "xxxxx";

notify(userId1, 'ORDER', 3); // order shipped, arrives in 3 days

async function notify(userId, eventType, message) {
const token = await getToken();
console.log("token >- "+token);
const status = await sendEvent(eventType, token, userId, message);
return status;
}
function getProactiveOptions(token, postLength){

return { //api.fe.amazonalexa.com //hostname: 'api.amazonalexa.com', // api.eu.amazonalexa.com (Europe) api.fe.amazonalexa.com (Far East) //hostname: 'api.eu.amazonalexa.com', // api.eu.amazonalexa.com (Europe) api.fe.amazonalexa.com (Far East) hostname: 'api.fe.amazonalexa.com', // api.eu.amazonalexa.com (Europe) api.fe.amazonalexa.com (Far East) port: 443, path: '/v1/proactiveEvents/' + (mode && mode === 'prod' ? '' : 'stages/development'), // mode: global var method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': postLength, 'Authorization' : 'Bearer ' + token } };

}
function getProactivePostData(eventType, userId, message) {
// console.log(routing ${eventType} ${message});
switch (eventType) {
case "ORDER":
return getOrderStatusEvent(userId, message);
// case "MEDIA":
// return getMediaEvent(message);
// case "SPORTS":
// return getSportsEvent(userId, message);
default:
return getOrderStatusEvent(userId);
}
}
function getOrderStatusEvent(userId, message) {
let timestamp = new Date();
let expiryTime = new Date();
let arrivalDate = new Date();
arrivalDate.setTime( arrivalDate.getTime() + message * 86400000 );
arrivalDate.setHours(0,0,0,0);
expiryTime.setMinutes(expiryTime.getMinutes() + 60);
let referenceId = "SampleReferenceId" + new Date().getTime(); // cross reference to records in your existing systems
const eventJson = {

"timestamp": timestamp.toISOString(), "referenceId": referenceId, "expiryTime": expiryTime.toISOString(), "event": { "name": "AMAZON.OrderStatus.Updated", "payload": { "state": { "status": "ORDER_SHIPPED", "deliveryDetails": { "expectedArrival": arrivalDate.toISOString() // "2018-12-14T23:32:00.463Z" } }, "order": { "seller": { "name": "localizedattribute:sellerName" } } } }, "localizedAttributes": [ { "locale": "en-US", "sellerName": "Delivery Owl" }, { "locale": "en-US", "sellerName": "Delivery Owl" }, { "locale": "en-GB", "sellerName": "Delivery Owl UK" } ], "relevantAudience": { "type": "Unicast", "payload": { "user": userId } } }; console.log("****************************************************"); console.log(eventJson); console.log("****************************************************"); return eventJson;

}
// ----------------------------------------------------------------------------
function getToken() {
--省略
}
// ----------------------------------------------------------------------------
function sendEvent(eventType, token, userId, message) {
return new Promise(resolve => {
const ProactivePostData = JSON.stringify(getProactivePostData(eventType, userId, message));
// console.log(\nProactivePostData\n${JSON.stringify(JSON.parse(ProactivePostData), null, 2)}\n-----------);
const ProactiveOptions = getProactiveOptions(token, ProactivePostData.length);
// console.log(ProactiveOptions\n${JSON.stringify(ProactiveOptions, null, 2)});
const req = https.request(ProactiveOptions, (res) => {
res.setEncoding('utf8');
if ([200, 202].includes(res.statusCode)) {
// console.log('successfully sent event');
console.log(requestId: ${res.headers['x-amzn-requestid']});
} else {

//console.log(res); console.log(`Error https response: ${res.statusCode}`); console.log(`requestId: ${res.headers['x-amzn-requestid']}`); if ([403].includes(res.statusCode)) { console.log(`userId ${userId}\nmay not have subscribed to this event.`) } } let returnData; res.on('data', (chunk) => { returnData += chunk; }); res.on('end', () => { //console.log(`return headers: ${JSON.stringify(res.headers, null, 2)}`); resolve(`sent event ${eventType}`); }); }); req.write(ProactivePostData); req.end(); });

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問