Skip to content

Instantly share code, notes, and snippets.

@Sieberkev
Last active August 23, 2016 14:54
Show Gist options
  • Save Sieberkev/0f96f190615cebf15a07ca2a8a2a61ca to your computer and use it in GitHub Desktop.
Save Sieberkev/0f96f190615cebf15a07ca2a8a2a61ca to your computer and use it in GitHub Desktop.
PokemonGo Bot REST API Proof of Concept
<html>
<head>
<title>PokeMonBot GUI</title>
<link rel="shortcut icon" href="img/favicon.ico" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
</head>
<body>
<div id="profile">
Username: <span id="username">n/a</span><br />
Auth Session Key: <span id="auth_session_key">n/a</span> <button onclick="fetch_auth_key();">Auth</button>
</div>
<button onclick="fetch_all_pokemons();">Fetch Pokemons</button>
<pre id="pokemons"></pre>
<script>
// Global bot configuration
var bot_host = "localhost"; // Edit this value if you want to use another host
var bot_api_port = 8080; // Default port is 8080, see the manual on how to change it
// TODO: Should be read in from the global bot list
var bot_socket_port = 8001; // Edit according to your settings
var bot_name = "default"; // Edit according to your settings
var bot_api_password = "hunter2"; // Put your password from the configuration key 'rest_api_password' here
var bot_session_key = ""; // This will be filled in by the script when authing
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Bot auth session key
function fetch_auth_key() {
var xhr = createCORSRequest("POST", "http://" + bot_host + ":" + bot_api_port + "/api/bot/" + bot_name + "/auth");
if (!xhr) {
alert('CORS not supported');
return;
}
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
bot_session_key = this.responseText;
$('#auth_session_key').text(bot_session_key);
}
});
xhr.send(bot_api_password);
}
function fetch_all_pokemons() {
// No valid session key
if (bot_session_key == "") return false;
var xhr = createCORSRequest("GET", "http://" + bot_host + ":" + bot_api_port + "/api/bot/" + bot_name + "/pokemons");
if (!xhr) {
alert('CORS not supported');
return;
}
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
$('#pokemons').text(this.responseText);
}
});
xhr.setRequestHeader("x-pgb-access-token", bot_session_key);
xhr.send();
}
// Bot Socket connection
var socket = io.connect('ws://' + bot_host + ":" + bot_socket_port);
socket.on('profile', function(data){
$('#username').text(data.username);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment