-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinston.js
More file actions
262 lines (219 loc) · 7.31 KB
/
winston.js
File metadata and controls
262 lines (219 loc) · 7.31 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const Bot = require('keybase-bot');
const PDFDocument = require('pdfkit');
const FeedParser = require('feedparser');
const request = require('request');
const fs = require('fs');
const https = require('https');
const TuyAPI = require('tuyapi');
//////////// constants /////////////
const PWD_FILE = "bot-paper-key.txt"
const IMG_DIR = "images/";
const PDF_NAME = "homework.pdf";
const XKCD_FEED = "https://www.xkcd.com/rss.xml";
const XKCD_IMG_NAME = "xkcd";
const XKCD_TIME_RANGE = 60;
/////////////////////////////////
// Include the config file
// https://stackoverflow.com/a/28066576
require('./config.js')();
const bot = new Bot()
async function main() {
try {
const file = fs.readFileSync(PWD_FILE, { encoding: 'utf8' }).split("\n");
const username = file[0]
const paperkey = file[1]
await bot.init(username, paperkey)
const info = bot.myInfo()
console.log(`${info.username} initialized.`)
await bot.chat.clearCommands()
const onMessage = async message => {
var channel = message.channel.topicName;
var messageText = message.content.text.body;
var words = parseMessage(messageText);
if (channel == "png2pdf") {
if (message.content.type == 'attachment') {
bot.chat.download(message.conversationId, message.id, IMG_DIR + message.content.attachment.object.filename);
return;
}
if (words.includes('convert')) {
makePDF();
bot.chat.attach(message.conversationId, PDF_NAME);
}
}
if (channel == "xkcd") {
getRSSFeed((new Date()).getTime(), message.conversationId);
}
if (channel == "testing") {
console.log(message);
console.log("---------------------------------\n");
}
// If not text, quit
if (message.content.type !== 'text') {
return
}
// Control the lights
if (words.includes("lights")) {
if (words.includes("on")) {
lightsOn();
} else if (words.includes("off")) {
lightsOff();
} else if (words.includes("dim")) {
lightsDim();
}
}
// PING PONG (for testing)
if (messageText.startsWith('PING')) {
bot.chat.send(message.conversationId, {
body: "PONG",
});
}
// !echo command (for testing)
if (messageText.startsWith('!echo ')) {
bot.chat.send(message.conversationId, {
body: message.content.text.body.substr(6),
});
}
return;
}
const onError = e => console.error("Keybase Error 2: " + e);
console.log(`Listening for messages...`);
await bot.chat.watchAllChannelsForNewMessages(onMessage, onError);
} catch (error) {
console.error("Keybase Error 1: " + error);
}
}
async function shutDown() {
await bot.deinit()
process.exit()
}
//////////////////////////// helper functions /////////////////////
function parseMessage(message) {
var words = message.split(" ");
for (let i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}
return words;
}
function makePDF() {
const doc = new PDFDocument({autoFirstPage: false});
doc.pipe(fs.createWriteStream(PDF_NAME));
var arrayOfFiles = [];
try {
arrayOfFiles = fs.readdirSync(IMG_DIR)
} catch(e) {
console.log("Error Reading Directory: " + e)
}
for (i in arrayOfFiles) {
var fname = IMG_DIR + arrayOfFiles[i];
doc.addPage();
// For some reason images are sideways by default
doc.rotate(90);
// No clue why this works...
// https://stackoverflow.com/questions/51030194/using-rotate-for-images-leads-to-blank-pdf-with-pdfkit
doc.image(fname, 0, -620, {
height: 620,
width: 795
});
// Delete the image
try {
fs.unlinkSync(fname);
} catch (err) {
console.log("Error Deleting Image: " + err);
}
}
doc.end();
return;
}
function getRSSFeed(timeNow, channel) {
var req = request(XKCD_FEED);
var feedparser = new FeedParser();
req.on('error', function (error) {
console.log("XKCD Feed Request Error: " + error);
});
req.on('response', function (res) {
var stream = this;
if (res.statusCode !== 200) {
this.emit('error', new Error('Bad status code'));
} else {
stream.pipe(feedparser);
}
});
feedparser.on('error', function (error) {
console.log("FeedParser Error: " + error);
});
feedparser.on('readable', function () {
var stream = this;
var meta = this.meta;
var item;
while (item = stream.read()) {
var title = item.title;
var imgUrl = item.summary.split('"')[1];
var altText = item.summary.split('"')[3];
// This is in minutes
var delta = (timeNow - item.pubDate.getTime()) / (1000 * 60);
if (delta < XKCD_TIME_RANGE) {
// Download the file
const file = fs.createWriteStream(XKCD_IMG_NAME);
const request = https.get(imgUrl, function(response) {
response.pipe(file);
}).on("error", (err) => {
console.log("XKCD Image Request Error: " + err.message);
});
// Send it
bot.chat.attach(channel, XKCD_IMG_NAME, {
title: "*" + title + ":* " + altText
});
} else {
console.log("entry too old");
}
}
});
}
// Add event listeners
device.on('connected', () => {
console.log('Connected to device!');
});
device.on('disconnected', () => {
console.log('Disconnected from device.');
});
device.on('error', error => {
console.log('Error!', error);
});
async function lightsOn() {
await device.find();
await device.connect();
await device.set({dps: 1, set: true});
await device.set({dps: 4, set: true});
await device.set({dps: 5, set: true});
makeRequest(LIGHT_STRIP_ON);
device.disconnect();
}
async function lightsOff() {
await device.find();
await device.connect();
await device.set({dps: 1, set: false});
await device.set({dps: 4, set: false});
await device.set({dps: 5, set: false});
makeRequest(LIGHT_STRIP_OFF);
device.disconnect();
}
async function lightsDim() {
await device.find();
await device.connect();
await device.set({dps: 1, set: true});
await device.set({dps: 4, set: false});
await device.set({dps: 5, set: true});
makeRequest(LIGHT_STRIP_OFF);
device.disconnect();
}
function makeRequest(url) {
https.get(url, (resp) => {
console.log("Request successful.")
}).on("error", (err) => {
console.log("Request Error: " + err.message);
});
}
//////////////////////////////////////////////////////////////////
process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)
main()