-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.js
125 lines (106 loc) · 3.91 KB
/
chatbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
var SlackBot = require('slackbots');
var PokemonGo = require('pokemon-go-node-api');
var geolib = require('geolib');
var opts = require('commander');
function list(val){
return val.split(',');
}
opts
.version(require('./package.json').version)
.option('-c, --config [filepath]', 'Set configuration json file (can set all below settings)')
.option('-u, --username [username]', 'Pokemon Trainer Club username')
.option('-p, --password [password]', 'Pokemon Trainer Club password')
.option('-n, --name [name]', 'Name of your slack bot')
.option('-t, --token [token]', 'Slack bot token')
.option('--latitude [latitude]', 'Latitude of scan location', parseFloat)
.option('--longitude [longitude]', 'Longitude of scan location', parseFloat)
.option('--channels <channels>', 'List of channels to join (comma seperated) ie: general,games,srsbznss,etc', list)
.option('--exclude <pokemon-list>', 'List of pokemon to exclude from announcement (comma seperated) ie: 21,16,17,etc', list)
.parse(process.argv);
function optsError(){
console.log.apply(null, Object.keys(arguments).map(key => arguments[key]));
opts.outputHelp();
process.exit(0);
}
if(opts.config){
var config;
try{
config = require(opts.config);
} catch(err){
optsError("Something has gone wrong while trying to load config file : ", opts.config, err);
}
Object.keys(config).forEach(function(key){
opts[key] = config[key];
});
}
//Validate our settings
if(!opts.username || !opts.password) return optsError("Username + password for Pokemon Trainers Club must be provided");
if(!opts.name) return optsError("You must provide the name of your slackbot");
if(!opts.token) return optsError("You must provide the slack bot token");
if(!opts.channels || opts.channels.length <= 0) return optsError("You must provide channels for the bot to enter");
if(!opts.latitude || !opts.longitude) return optsError("You must provide latitude and longitude for scanning");
var bot = new SlackBot({
token: opts.token, // Add a bot https://my.slack.com/services/new/bot and put the token
name: opts.name,
channels: opts.channels
});
var pokeGo = new PokemonGo.Pokeio();
var location = {
type: 'coords',
coords: {
latitude: opts.latitude,
longitude: opts.longitude,
altitude: 0
}
};
var encounters = [];
var excludes = [];
if(opts.exclude && opts.exclude.length > 0){
excludes = opts.exclude
}
var heartbeat;
var connectToPokemon = function(){
pokeGo.init(opts.username, opts.password, location, 'ptc', function(err){
if(err){
console.error("Something has gone wrong with connecting to the server - trying again in 20 seconds", err);
setTimeout(connectToPokemon, 20000);
return;
}
console.log("Connected");
heartbeat = setInterval(function(){
pokeGo.Heartbeat(function(err, hb){
if(err) return handleHeartbeatFailure(err);
nearby = [];
hb.cells.forEach(function(cell){
if(cell.MapPokemon.length <= 0) return;
cell.MapPokemon.forEach(function(pokemon){
announcePokemon(pokemon);
});
});
});
}, 5000);
});
};
var handleHeartbeatFailure = function(err){
console.log("Heartbeat failure, resetting.", err);
clearInterval(heartbeat);
pokeGo = null;
pokeGo = new PokemonGo.Pokeio();
connectToPokemon();
};
var announcePokemon = function(pokemon){
var upid = pokemon.EncounterId.high + ' ' + pokemon.EncounterId.low;
if(encounters.indexOf(upid) != -1) return;
encounters.push(upid);
if(excludes.indexOf(pokemon.PokedexTypeId) != -1) return;
var distance = geolib.getDistance(
{ latitude: location.coords.latitude, longitude: location.coords.longitude },
{ latitude: pokemon.Latitude, longitude: pokemon.Longitude });
var name = pokeGo.pokemonlist[parseInt(pokemon.PokedexTypeId)-1].name;
bot.postMessageToChannel('games', 'Pokemon detected: #' + pokemon.PokedexTypeId + ' - ' + name + ' is ' + distance + ' meters away');
}
bot.on('start', function() {
console.log("Bot started");
connectToPokemon();
});