HTTP Request with Alexa Skill

#1

I’m trying to make an alexa skill where it will retrieve a server.serverstate from https://leonick.se/swtor-server-status/?server=bastion but I’m having trouble with it.
Getting the result “The remote endpoint could not be called, or the response it returned was invalid.” in the Amazon Development panel.

Not sure what I’m doing exactly, so finding this very confusing. Anyone know how I can achieve this?

Thanks

0 Likes

#2

Hey Tim,

The first issue is going to be that the result from that URL is not JSON - it looks like JavaScript declaring a few variables:

var ServerName="The Bastion";
var ServerType="PvP";
var ServerLanguage="West";
var ServerState="UP";
var ServerPopulation="Light";
var CacheTime="Mar 04, 09:04 UK";
var ErrorMsg="";

If you control the data format from that URL, I might recommend switching them to JSON, which would look like this:

{
  "ServerName": "The Bastion",
  "ServerType": "PvP",
  "ServerLanguage": "West",
  "ServerState:" "UP",
  "ServerPopulation": "Light",
  "CacheTime": "Mar 04, 09:04 UK",
  "ErrorMsg": ""
}

It the data comes back as JSON, the workflow will automatically parse it and make it available on the payload. Since the current data format is non-standard, you’ll have to parse it manually. Fortunately, since the format is essentially JavaScript, we can use the Function object to parse it. Add this to a Function node to parse the data.

// Make a JavaScript function from the incoming body.
// Add a return statement for the desired variable.
var func = new Function(
  payload.data.serverStateHTTPResult.body + 'return ServerState;');

// Put the result of the above function
// on the payload.
payload.data.serverState = func();

The Function object in JavaScript takes a string representation of some code and turns it into a function we can call. We had to add a return statement to the string so the function actually returns something. The next line adds it back to the payload so you can reference it using a template: {{ data.serverState }}.

You should now be able to craft the Alexa webhook reply with that variable and get it to say the server state.

0 Likes

#3

Here’s a workflow I exported that demonstrates the above instructions. You can import this into a new workflow to see the details.

parse-example.flow (1.4 KB)

0 Likes

#4

Thanks so much Brandon!

0 Likes