-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (102 loc) · 3.39 KB
/
app.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
require('dotenv').config()
// available functions
// catFacts.random();
// catFacts.all;
const Bot = require('slackbots')
const catFacts = require('cat-facts');
const openers = require('./openers.js');
const settings = {
token: process.env.TOKEN,
name: 'Catfacts'
}
const params = {
icon_emoji: ':cf:',
link_names: true
};
const bot = new Bot(settings)
const channel = 'testing'
let randOpen = () => (openers[Math.floor(Math.random()*openers.length)])
const getTime = () => {
// consider external lib instead.
let time = new Date().toLocaleTimeString()
let day = new Date().getDay()
if ((time.includes('13:00')) && (day === 6)) {
bot.postMessageToGroup(
channel,
`Happy Caturday! ${catFacts.random()}`,
params
)
}
}
const handleMessage = (data) => {
// console.log(data);
// console.log(data.getUser() );
// let userID = data.user
// const postMessageToUserById = async (userID, 'message', params) => {
// const { channel: { id: channelID } } = await bot.openIm(userID);
// bot.postMessage(channelID, 'message', opts)
// }
// bot.openIm(data.user)
// .then(() => {
// bot.postMessage(data.user, 'hi')
// })
// console.log(bot.getUsers());
let msg = data.text
if (msg.split(' ').length === 1) {
bot.postMessageToGroup(channel, 'Meow?', params);
} else if (msg.includes(' fact')) {
bot.postMessageToGroup(channel, randOpen() + catFacts.random(), params);
} else if (msg.includes(' dm')) {
bot.postMessageToGroup(channel, 'DM unavailable at this moment.', params);
// bot.postMessageToUser(data.user, catFacts.random(), params);
// bot.postMessageToUser(data.user, 'hi')
// .always((data) => {
// // console.log(data);
// })
} else if (msg.includes(' help')) {
bot.postMessageToGroup(channel, 'You can @catfacts + "fact" to hear a random fact. Bot will also run every Sunday at 1PM.', params);
} else {
bot.postMessageToGroup(channel, 'No idea what you mean right meow. try typing "fact"', params);
}
}
bot.on('start', () => {
console.log('Catfacts server running');
setInterval(getTime, 60000);
// bot.postMessageToGroup(
// channel,
// `You have been subscribed to @catfacts! ${catFacts.random()}`,
// params
// );
// console.log(bot.getUsers())
// bot.postMessageToUser('ntamura', randomFact, params);
// bot.postMessageToChannel('general', randomFact, params);
// bot.postMessageToGroup('testing', randomFact, params);
})
bot.on('message', (data) => {
if (data.type === 'message' && data.text.includes('<@UB25RGRGS>')) {
handleMessage(data)
}
// if (data.type === "desktop_notification") {
// let text = data.content
// let arg = /([\/])\w+/.exec(text);
// if (arg[0]) {
// switch(arg[0]) {
// case "/facts":
// // console.log('/fact!');
// bot.postMessageToGroup('testing', randomFact, params);
// break;
// default:
// console.log('default');
// }
// } else { console.log('error') }
// }
});
bot.on('error', (err) => {
console.log(err);
})
// references
// https://code.tutsplus.com/articles/building-a-slack-bot-using-nodejs--cms-29444
// https://api.slack.com/events/app_mention
// https://slackapi.github.io/node-slack-sdk/rtm_api
// https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and-chuck-norris-super-powers
// https://blog.heroku.com/how-to-deploy-your-slack-bots-to-heroku