在dynamodb中获取单个alexa反应的时间戳

时间:2018-11-03 02:18:31

标签: node.js aws-lambda amazon-dynamodb alexa

我正在创建一个Alexa事实技能,要求用户提供有关其健康的一些信息,并且用户根据不同区域的疼痛程度以1-10的分数进行回答。然后,我将数据输入到DynamoDB表中,该表存储四个健康问题(肿胀,感觉,睡眠,呼吸)和用户ID的信息(满分10分)。但是,条目没有提供创建时间的时间戳。我想知道是否有一种方法可以为每个健康问题的响应都打上时间戳,但是整个条目的时间戳也会有所帮助。在查找DynamoDB文档时,没有找到添加时间戳的任何方法,我是否必须使用任何外部SDK。

下面是我的Lambda函数的index.js代码,该代码用于我的Alexa技能。

'use strict';
const Alexa = require('alexa-sdk');

const SKILL_NAME = 'Home Assist';
const HELP_MESSAGE = 'You can say I want to input my data';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


const handlers = {
    'LaunchRequest': function () {
        this.emit('HomeAssistQuestions');
    },
    'HomeAssistQuestions': function () {
        this.attributes.healthscores = {
            'patientID' : 0,
            'scores': {
                'feeling': {
                    'score': 0
                },
                'sleeping': {
                    'score': 0
                },
                'breathing': {
                    'score': 0
                },
                'swollen': {
                    'score': 0
                }
            }
        };

        if(this.event.request.dialogState !== 'COMPLETED'){
            this.emit(':delegate');
        }
        else{
            const feelingScore = this.event.request.intent.slots.feelingRating.value;
            const sleepingScore = this.event.request.intent.slots.sleepingRating.value;
            const breathingScore = this.event.request.intent.slots.breathingRating.value;
            const swollenScore = this.event.request.intent.slots.SwollenRating.value;
            const id = this.event.request.intent.slots.id.value;


            this.attributes.healthscores.patientID = id;
            this.attributes.healthscores.scores['feeling'].score = feelingScore;
            this.attributes.healthscores.scores['sleeping'].score = sleepingScore;
            this.attributes.healthscores.scores['breathing'].score = breathingScore;
            this.attributes.healthscores.scores['swollen'].score = swollenScore;

            this.response.speak("Health Scores Recorded");
            this.emit(':responseReady');
        }


    },
    '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');
    },
    'SessionEndedRequest': function(){
        this.emit('saveState', true);
    }
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.dynamoDBTableName = 'HealthScores';
    alexa.APP_ID = "amzn1.ask.skill.d5b8d597-eb50-41c6-a22d-b0f18c23b544";
    alexa.registerHandlers(handlers);
    alexa.execute();
};

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

//Make sure to add this exact URL to your Wink Developer Portal! (https://developer.wink.com/clients)
$redirect_uri = "http://[YourServer]/wink_helloworld.php";
// This is from Wink Developer Portal
$client_id = "abcdefg";
$wink_oauth_url = "https://api.wink.com/oauth2/token";
$client_secret = "hijklmnop";
$devices_url = "https://api.wink.com/users/me/wink_devices";
//need to create a state variable, like a session id. should actually be random tho!!
$randomstring="xyzABC123";
$state = base64_encode($randomstring);
/*_____________________________________________________________________________________________________________________________________ */

echo "<h2>Wink Hello World - Show Devices</h2>";

//If we don't have a code, then send user to login page
if($_GET['code'] == null | $_GET['code'] == ""){
    echo "<a href='https://api.wink.com/oauth2/authorize?response_type=code&client_id=".$client_id."&redirect_uri=$redirect_uri&state=".$state."'>Login</a>";
    return;
}
$code = $_GET['code'];
//if we dont have a token, lets get one
if($access_token == null | $access_token == ""){
    $access_token = getAccessToken();
}
// lets get some data from our devices!
getResource($access_token);
/*_____________________________________________________________________________________________________________________________________ */
// Get token
function getAccessToken() {
    global $wink_oauth_url, $code, $client_secret;
    echo "<b>getAccessToken()</b> Using Code: $code<br>";
    $curl = curl_init();    
    curl_setopt($curl, CURLOPT_URL, $wink_oauth_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "{
        \"client_secret\": \"$client_secret\",
        \"grant_type\": \"authorization_code\",
        \"code\": \"$code\"
    }");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));    
    $response = curl_exec($curl);
    //var_dump($response);
    formatResults($response);       //debug output
    curl_close($curl);
    return json_decode($response)->access_token;
}
/*_____________________________________________________________________________________________________________________________________ */
// Get Resource(s) with our code & token
function getResource($access_token) {
    global $devices_url;
    echo "<b>getResource()</b> Using Token: $access_token<p>";
    $header = array("Authorization: Bearer {$access_token}");
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $devices_url,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    $response = curl_exec($curl);
    curl_close($curl);  
    formatResults($response);       //debug output
}

/*_____________________________________________________________________________________________________________________________________ */
//debug formatted output functions
function formatResults($json){
    echo "<pre>";
    echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
    echo "</pre>";
}

?>