[日本語Alexa] Alexa Setting API を使用してユーザーの設定情報を取得する(更新)

2018.09.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

先日、下記の記事を投稿させて頂いたのですが、もう、「APIコールしなくてもSDKで対応しているよ」との情報をご提供頂きました。 本記事は、先の投稿の修正記事とさせて頂きます。よって、内容はほぼ同一で、SDKを使用した要領ということになります事をご了承下さい。

1 はじめに

Alexaアプリでは、デバイスごとにタイムゾーン単位(温度・距離)の設定ができるようになっています。

今回は、この設定内容をスキルで取得する要領を試してみました。

最初に、作成したサンプルを御覧ください。Alexaアプリで設定を変更すると、即座にそれをスキル側で取得できていることが分かります。

2 ServiceClientFactory

ServiceClientFactoryは、Alexaの各種APIをコールするためのサービスクライアントを提供しています。

サービスクライアントでは、ApiAccessTokenApiEndpointが、ラップされるため開発者がこれを意識する必要はなくなります。

今回対象としている、タイムゾーンや温度・距離の単位の取得には、各種サービスクライアントのうちの、UpsServiceClientが使用されます。

UpsServiceClientServiceClientFactory経由で取得しているコードは下記のとおりです。

const LaunchRequestHandler = {
    canHandle(h) {
        return isMatch(h,'LaunchRequest');
    },
    async handle(h) {
        const upsServiceClient = h.serviceClientFactory.getUpsServiceClient();

なお、ServiceClientFactoryを使用するためには、Skillオブジェクトで.withApiClient()を使用してAlexa.DefaultApiClient()を設定しておく必要があります。

exports.handler = async function (event, context) {
    if (!skill) {
      skill = Alexa.SkillBuilders.custom()
        .withApiClient(new Alexa.DefaultApiClient()) 
        .addRequestHandlers(
            LaunchRequestHandler,
            SessionEndedRequestHandler)
        .create();
    }
    return skill.invoke(event);
}

3 設定情報取得

各種の情報は、次の要領で取得できます。

(1) タイムゾーン

const timeZone = await upsServiceClient.getSystemTimeZone(deviceId);

返されるタイムゾーンの例は、以下のとおりです。

"Asia / Tokyo"

(2) 距離の単位

const distanceUnits = await upsServiceClient.getSystemDistanceUnits(deviceId);

返される距離の単位の例は、以下のとおりです。

"METRIC"  "IMPERIAL"

(3) 温度の単位

const temperatureUnit = await upsServiceClient.getSystemTemperatureUnit(deviceId);

返される温度の単位の例は、以下のとおりです。

"CELSIUS"  "FAHRENHEIT"

(4) deviceId

上記3つのコールで使用するdeviceIdは、Alexaからのレクエストから取得します。

const deviceId = h.requestEnvelope.context.System.device.deviceId;

4 実装

動作確認のために作成してみたコードです。

const LaunchRequestHandler = {
    canHandle(h) {
      return isMatch(h,'LaunchRequest');
    },
    async handle(h) {
        console.log(JSON.stringify(h.requestEnvelope))

        const upsServiceClient = h.serviceClientFactory.getUpsServiceClient();

        const deviceId = h.requestEnvelope.context.System.device.deviceId;

        const timeZone = await upsServiceClient.getSystemTimeZone(deviceId);
        const distanceUnits = await upsServiceClient.getSystemDistanceUnits(deviceId);
        const temperatureUnit = await upsServiceClient.getSystemTemperatureUnit(deviceId);

        console.log(timeZone + ' ' + distanceUnits + ' ' + temperatureUnit);

        let message = '現在の設定です。<break time="1s"/>';
        message += 'タイムゾーンは、' + timeZone;
        message += '<break time="1s"/>';
        message += '温度の単位は、';
        message += (temperatureUnit == '"CELSIUS"') ? '摂氏' : '華氏';
        message += '<break time="1s"/>';
        message += '距離の単位は、';
        message += (distanceUnits == '"METRIC"') ? 'メートル法' : 'インペリアル';

        return h.responseBuilder
          .speak(message)
          .getResponse();
    }
};

5 最後に

ユーザーが設定したタイムゾーンや単位をスキルの動作に反映させるのは、スキル開発者の役割です。もし、これらの設定に依存するスキルを作成する場合は、留意が必要でしょう。

6 参考リンク


UpsServiceClient
Obtain Customer Settings Information with the Alexa Settings API