Last active
August 31, 2016 10:18
-
-
Save dgoguerra/2dbe852828c94e4b1b35375405b1e959 to your computer and use it in GitHub Desktop.
Pokemon GO slack bot notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'), | |
request = require('request'), | |
sprintf = require('sprintf-js').sprintf, | |
pokegoScan = require('pokego-scan'); | |
// coords to check in a loop | |
var coords = { | |
latitude: null, | |
longitude: null | |
}; | |
// slack webhook | |
var slack = { | |
webhook: 'https://hooks.slack.com/services/...', | |
channel: '@user|#channel' | |
}; | |
// max distane to the given coordinates | |
var distance = 100; | |
function sendSlackNotification(slackInfo, content, next) { | |
var data = { | |
username: content.appName+' deployment', | |
text: content.message, | |
attachments: [] | |
}; | |
if (slackInfo.channel) { | |
data.channel = slackInfo.channel; | |
} | |
if (content.extraText) { | |
data.attachments.push({ text: content.extraText }); | |
} | |
request.post(slackInfo.webhook, { json: data }, next); | |
} | |
var alreadyFound = {}; | |
function performScan(next) { | |
pokegoScan(coords, {distance: distance}, function(err, pokemon) { | |
var attachments = []; | |
pokemon.forEach(function(pok) { | |
// already found in a scan before, dont report it | |
if (alreadyFound[pok.id]) { | |
return; | |
} | |
// save the pokemon as already found | |
alreadyFound[pok.id] = true; | |
var gmapsUrl = 'https://www.google.com/maps/place/'+pok.latitude+','+pok.longitude; | |
attachments.push({ | |
text: sprintf("%s (<%s|PokeVision>, <%s|Google Maps>)", pok.name, pok.map, gmapsUrl), | |
thumb_url: pok.image, | |
fields: [{ | |
title: 'Distance', | |
value: pok.distance_str, | |
short: true | |
}, { | |
title: 'Despawns', | |
value: pok.despawns_in_str, | |
short: true | |
}] | |
}) | |
}); | |
if (attachments.length) { | |
request.post(slack.webhook, { | |
json: { | |
channel: slack.channel, | |
username: 'PokeVision Bot', | |
icon_url: 'https://pokevision.com/asset/image/logo-mini-light.png', | |
text: 'Pokemon found '+distance+'m from you', | |
attachments: attachments | |
} | |
}, function() { | |
next(null, attachments.length); | |
}); | |
} else { | |
next(null, 0); | |
} | |
}); | |
} | |
console.log('scanning for pokemon within '+distance+'m'); | |
async.forever(function(loop) { | |
performScan(function(err, numNewFound) { | |
console.log(numNewFound+' new pokemon found within '+distance+'m'); | |
if (err) return loop(err); | |
setTimeout(function() { | |
loop(null); | |
}, 60000 /* 60s */); | |
}); | |
}, function(err) { | |
console.error('error: '+err.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, assign valid coordinates to
coords
and a valid slack webhook and channel name toslack
. By editingdistance
pokemon in a different range to the given coordinates can be retrieved.