-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (99 loc) · 2.72 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
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
function parseForImages(html) {
var parser = new DOMParser()
var dom = parser.parseFromString(html, 'text/html')
meows = dom.getElementsByTagName('img')
// remove non-cat pics from meows
for (var i = 0; i < meows.length; i++) {
var imgSrc = meows[i].getAttribute('src')
var split = imgSrc.split('/')
if (split[1] !== 'thumbs') {
meows.splice(i, 1)
i -= 1
}
}
return meows
}
function randomizeCat(meows) {
var randomCat = meows[Math.floor(Math.random() * meows.length)].getAttribute('src')
return randomCat
}
function getImgURL(randomCat) {
// splice out thumbs, replace with pictures
var tmp = randomCat.replace('thumbs', 'pictures')
// remove _s left over from src tag
var imgURL = tmp.replace('_s', '')
imgURL = 'https://funnycatsite.com' + imgURL
return imgURL
}
function botReply(imgURL) {
// starting express app
const app = express()
// starting server
app.listen(process.env.PORT, function() {
console.log('Bot is listening on port ' + process.env.PORT)
})
// setting up parser for / cmds
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
// sending random cat pic in response to / cmd
app.post('/', (req, res) => {
var randomCat = randomizeCat(meows)
var imgURL = getImgURL(randomCat)
var data = {form: {
token: process.env.SLACK_AUTH_TOKEN,
channel: '#meowbot',
text: randomizeMessage(),
attachments: JSON.stringify([
{
'text': 'have a great day!',
'image_url': imgURL
}
])
}}
request.post('https://slack.com/api/chat.postMessage', data,
function(error, response, body) {
// error handling
console.error('error', error)
console.log('statusCode:', response && response.statusCode)
console.log('body:', body)
})
})
}
function randomizeMessage() {
var messages = [
'MRROOWWWW',
'meeeeeoowwww',
'PURRRRR',
'miaoooo',
'meoooo',
'miaouuuu',
'MYAUUUUU'
]
var emojis = [
':smiley_cat:',
':heart_eyes_cat:',
':cat2:',
':pouting_cat:',
':cat:',
':joy_cat:'
]
var rand1 = Math.floor(Math.random() * messages.length)
var rand2 = Math.floor(Math.random() * emojis. length)
return messages[rand1] + emojis[rand2]
}
// node dependencies
require('dotenv').config()
const fetch = require('node-fetch')
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const DOMParser = require('dom-parser')
fetch('https://funnycatsite.com').then(res => {
return res.text()
}).then(html => {
return parseForImages(html)
}).then(meows => {
return botReply(meows)
}).catch(error => {
console.error('error:', error)
})