This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
53 lines (48 loc) · 1.65 KB
/
index.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
let irc = require("irc");
let axios = require("axios").default;
let secret = require("./secret.json");
let Farmbot = require("farmbot").Farmbot;
// Node JS workaround:
// SEE https://github.com/FarmBot/farmbot-js/issues/33
global.atob = require("atob");
// SEE: https://github.com/FarmBot/Farmbot-Web-API#generating-an-api-token
let API_URL = "https://my.farmbot.io/api/tokens";
// SEE: https://github.com/martynsmith/node-irc#basic-usage
let IRC_SERVER = "irc.freenode.net";
let IRC_NICK = "potato_tornado";
let IRC_CONFIG = { channels: ['#farmbot_example'] };
// STEP ONE: Get an API token
axios
.post(API_URL, secret)
.then(function (resp) {
let token = resp.data.token.encoded;
console.log(`Got API Token...`);
console.dir(resp.data.token.unencoded)
// STEP TWO: Connect to your FarmBot:
let bot = new Farmbot({ token: token, secure: true });
bot
.connect()
.then(function () {
console.log("CONNECTED TO BOT!")
// STEP THREE: Connect to IRC
var client = new irc.Client(IRC_SERVER, IRC_NICK, IRC_CONFIG);
client.addListener('message', function (from, to, message) {
if (message.startsWith("up")) {
bot
.moveRelative({ x: 0, y: 0, z: 7 })
.then(() => console.log("MOVING!!!"))
.catch(() => console.log("CANT MOVE T_T"));
}
console.log("GOT: " + message);
});
})
.catch(function (e) {
console.log("FAILED TO CNNECT TO BOT!!");
console.dir(e);
console.dir(bot._state);
})
})
.catch(function (err) {
console.log("BAD PASSWORD OR EMAIL!");
console.dir(err);
});