-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
54 lines (39 loc) · 1.62 KB
/
bot.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
"use strict";
const rtmApiClient = require('@slack/client').RtmClient;
const bot = new rtmApiClient(process.env.SLACK_API_KEY);
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const createScreenshot = require('./tasks/createScreenshot');
const webApiClient = require('@slack/client').WebClient;
const web = new webApiClient(process.env.SLACK_API_KEY);
var botID = "";
bot.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
botID = rtmStartData.self.id;
});
bot.on(RTM_EVENTS.MESSAGE, (message) => {
if ( message.subtype == "bot_message" || message.subtype == "message_changed") { return; };
let messageText = message.text.split(" ");
let user = messageText[0].match(/@(.*)\>/);
if (!user || user.pop() !== botID) { return; };
let channel = message.channel;
let keyword = messageText[1] || null;
if (keyword == null) {
bot.sendMessage("I need some commands! Try help or screenshot", channel);
return;
}
let website = messageText[2] || null;
let device = messageText[3] || "mobile";
switch (keyword) {
case "help":
bot.sendMessage("Sure thing. At the moment I can take screenshots for you. Here's how it works:\nType <command> <url> <device>\nFor example, screenshot newyorktimes.com mobile\nWe're working on more features, stay tuned...", channel);
break;
case "screenshot":
if (website == null) {
bot.sendMessage("please provide a website", channel);
return;
}
createScreenshot(web, bot, website, channel, device);
break;
}
});
module.exports = bot;