Interface with External API [SOLVED]

ty13r
:?:
Is it possible to interface with an external API within Quest?

I'd like to setup an AJAX call to a remote server that outputs a URL to the user if a particular string is sent through the API, otherwise returning a null response.

In my mind this would allow me to use a custom passphrase that could not be determined by looking at the game's source code.

The Pixie
I do not know, but I would guess yes. Mess around with JS.eval, and see what happens. And let us know!

Could you use GetFileData to pull a file across the internet, and examine that?

ty13r
Ok so I've got an API setup to return a response if the correct parameter is passed and have verified it's working.

i've added a JS file and have this function inside it.

function winning (result) {
$.ajax(
{
url : "http://MYURL.com/api.php",
type: "POST",
data : result,
success: function(result)
{
msg ("Success!" + result.status)
}
,
error: function ()
{
msg ("Error!")
}
}
);
}


My intention is now to pass a phrase by the player and return the response from the server.

So far I've had no luck getting this work in Quest, any help would be much appreciated!

ty13r
Currently seeing these errors in the console:

XMLHttpRequest cannot load http://MYURL.com/api.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'res://' is therefore not allowed access.
fb.js:15 Uncaught ReferenceError: ALSEvent is not defined

and

Uncaught ReferenceError: ALSEvent is not defined

ty13r
ty13r wrote:Currently seeing these errors in the console:

XMLHttpRequest cannot load http://MYURL.com/api.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'res://' is therefore not allowed access.
fb.js:15 Uncaught ReferenceError: ALSEvent is not defined

and

Uncaught ReferenceError: ALSEvent is not defined



Ok looks like that's an issue on my server's end. Will update when I've verified if this works.

ty13r
Ok everyone I was able to get everything working as expected! :D

I added a JS file and invoked the function via abstract interaction with an object.

Here's a copy of my finalized js code.

function apiCall () {
var input = prompt ("enter code");
var inputObj = {'query' : input};
if (input != null) {
$.ajax({
type: 'POST',
dataType: 'text',
url: 'http://MYURL.com/api.php',
data: inputObj,
success: function (responseData, textStatus, jqXHR) {
if (responseData != "incorrect") {
$('#divOutput').html('<div>' + responseData + '</div>');
} else {
alert(responseData);
}
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
} else {
alert('FAILBOAT')
}
}

The Pixie
Good effort!

Can the Quest game determine if the right code was put in? I can see how the game will display whether it was successful of not; I cannot see how anything in the game world will know that however.

ty13r
The Pixie wrote:Good effort!

Can the Quest game determine if the right code was put in? I can see how the game will display whether it was successful of not; I cannot see how anything in the game world will know that however.



Thank you. No the quest game is not able to determine the correct code to pass, it's all contained on my server's side.

Basically unless the correct value is passed in my server will return "incorrect"

If the user passes in the correct response the the server will return something of value to the user.

Server side code looks something like:


<?php

header('Access-Control-Allow-Origin: *');

$KEY = 'RANDOM STRING';
$ANSWER= 'RANDOM STRING';

$result = 'incorrect';

if (isset($_POST['query'])) {
$query = $_POST['query'];
$query_hash = hash('sha256', $query);
if ($query_hash == $KEY) {
$result = openssl_decrypt($ANSWER, 'aes256', $KEY);
}
}

echo $result;

The Pixie
Have you tried the ASLEvent function in your JavaScript? If you have a function in Quest called SetResult that takes a string parameter, you should be able to call it like this:

ASLEvent ("SetResult", "correct");
ASLEvent ("SetResult", "wrong");

ty13r
The Pixie wrote:Have you tried the ASLEvent function in your JavaScript? If you have a function in Quest called SetResult that takes a string parameter, you should be able to call it like this:

ASLEvent ("SetResult", "correct");
ASLEvent ("SetResult", "wrong");



I tried to mess with getting the ASLevent to work, but reverted to prompt and alert for troubleshooting purposes.

Thanks for the tip, if I have time I will try to update this :D

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums