Alexa Steam自定义技能api集成 [英] alexa Steam custom skill api integration

查看:127
本文介绍了Alexa Steam自定义技能api集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在尝试发展我的第一批alexa技能。尝试建立一种技能,以使用户了解Steam好友中哪些人在线。

currently trying to develop one of my first alexa skills. trying to build a skill that updates the user on who out of their Steam friends, are online.

目前它很凌乱,不是我希望最终使用的格式,但是出于测试目的,我还没有使用意图,我只是在测试使用

Curerntly it's pretty messy and not in the format i hope it to be in the end, but for testing purposes, I'm not yet using intents, im just testing using the launchrequest.

到目前为止,我已经设法从Steam获得了一个用户(我的)朋友列表,并将所有朋友ID放在应该可以抓取的网址中这些
用户的详细信息。
我遇到的问题是执行第二个API调用以使用steamIds获取玩家详细信息。我不断获得不确定的回报,并为自己在做错的事情而感到困惑。
我是JS的新手,所以这里肯定有很多错误,但是一旦我能正常工作,我就可以整理一下。

So far I've managed to get a users (mine) friends list from steam and put all the friends ids in a url that should be able to grab the details for these users. The issue I'm having is performing the second API call to grab the players details using the steamIds. i keep getting an 'undefined' return and am stumped on what i'm doing wrong. I'm very much new to JS so there's bound to be mistakes in here, but i can work on tidying up later once i've got it working.

这很好用

/**
* Called when the user invokes the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {

console.log("onLaunch requestId=" + launchRequest.requestId
    + ", sessionId=" + session.sessionId);
var cardTitle = "Hello, World!"


testGet(function (response) {

    var speechOutput = "Here is the result of your query: " + response;
    var shouldEndSession = true;

    callback(session.attributes,
    buildSpeechletResponse(cardTitle, speechOutput, "", true));
});



//var speechOutput = "You can tell Hello, World! to say Hello, World!"

//callback(session.attributes,
   // buildSpeechletResponse(cardTitle, speechOutput, "", true));
}

此功能可从我的朋友列表中获取详细信息

This is the function that is grabbing the details from my friendlist

function testGet(response) {

var http = require('http')
var url = " http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=XXXXXXXXXX&steamid=76561198068311091&relationship=friend"


http.get(url, function (res) {

    // data is streamed in chunks from the server
    // so we have to handle the "data" event    
    var buffer = "", 
        data,
        friendsList,
        i,
        address,
        textResponse,
        route;

    res.on("data", function (chunk) {
        buffer += chunk;
    }); 

    res.on("end", function (err) {
        // finished transferring data
        // dump the raw data
        console.log(buffer);
        console.log("\n");
        data = JSON.parse(buffer);
        friendsList = data.friendslist.friends;

       textResponse = isOnline(friendsList);


        response("Friends online: " + textResponse);

}).on('error', function (e) {
    console.log("Error message: " + e.message);
});

})
}

这是最终功能

function isOnline(friendsList){
var http = require('http'),
i,
comma,
friendsIDs = "";

// for loop to get all friends ids in string
        for (i = 0; i < friendsList.length; i++) { 
            // if i equals 0 then it is the start of the loop so no 
            //comma needed, otherwise add a comma to seperate the ids. 
            if(i === 0) {comma = ""}
            else{comma = ","}
            //place the ids in a comma seperate string
            friendsIDs += comma + friendsList[i].steamid;
        }

 var playerurl = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxx&steamids=" + friendsIDs;

//到目前为止可以正常工作

// works fine up to this point

// run the api call to get player details
 http.get(playerurl, function (response) {

    // data is streamed in chunks from the server
    // so we have to handle the "data" event    
    var buffer = "", 
        playerdata,
        returnText,
        textResponse,
        friendsInformation,
        route;

    response.on("playerdata", function (chunk) {
        buffer += chunk;
    }); 

    response.on("end", function (err) {
        // finished transferring data
        // dump the raw data
        console.log(buffer);
        console.log("\n");
        playerdata = JSON.parse(buffer);

        friendsInformation = playerdata.response.players;

        for (i = 0; i < friendsInformation.length; i++) { 

            if(friendsInformation[i].personastate == 1) {
                returnText += friendsInformation[i].personaname + " chicken";
            }
    }

    return returnText;

    }).on('error', function (e) {
    console.log("Error message: " + e.message);
    });


});


}

感觉是如此接近,却不知道我要去哪里错了?!
谢谢

Been going round in circles for hours and feel so close to doing this but have no idea where I'm going wrong?! thanks

推荐答案

我已经设法通过使用JavaScript Promise解决了我的问题。我对诺言完全陌生,因此进行了一些反复尝试,但设法使它起作用。 这是我能找到的最简单的视频,它可以帮助我理解这个概念如何重新排列我的代码。

I have managed to solve my problem by using javascript promises. I'm completely new to promises so took some trial and error but have managed to get it to work. here is the simplest video i could find to explain the concept, it definately helped me understand how to rearrange my code.

如果您希望使用Alexa技能进行两次API调用,请使用第一个api调用中的数据来构造和通知第二个调用,则需要使用promises按顺序执行。

If you wish to do two API calls in an Alexa skill, using the data from the first api call to construct and inform the second call, you will need to use promises to do them sequentially.

它从isOnline()和testGet()函数中获取了代码,并将它们移到了promise中,这使我可以完成1个api调用(朋友列表信息),然后执行第二个调用(第二个api,以根据friendslist api调用的结果获取玩家详细信息)

it took the code from the isOnline() and testGet() functions and moved them into a promise, which allowed me to complete 1 api call (the original call to get friends list info) before executing the second call (the second api to get player details based on the results of the friendslist api call)

我现在可以与alexa一起查看我的哪些蒸汽朋友在线!希望我能够建立更多的功能(例如,他们在玩什么,如果他们处于离线状态或只是不在/忙碌/打s睡)

I can now check with alexa to see which of my steam friends are online! Hopefully i'll be able to build in some more functionality (eg what they are playing, if they are offline or just away/busy/snoozing)

感谢您做出的贡献

这篇关于Alexa Steam自定义技能api集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆