-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (34 loc) · 943 Bytes
/
index.js
File metadata and controls
39 lines (34 loc) · 943 Bytes
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
const request = require('request')
const cheerio = require('cheerio')
const { IncomingWebhook } = require('@slack/client')
require('dotenv').config()
const target = process.env.WATCH_URL
request(target, (e, response, body) => {
if (e) {
console.log(e)
}
try {
const $ = cheerio.load(body)
let stock = $('td.txt_right span.stock')
.text()
.trim()
// この文字列が存在したらSlackに通知する
if (stock === '在庫あり' || stock === '残りわずか') {
// slackに通知する
const webhook = new IncomingWebhook(process.env.SLACK_WEBHOOK_URL)
webhook.send('在庫状況:' + stock + '\nURL: ' + target, function (
err,
res
) {
if (err) {
console.log('slack error', err)
} else {
console.log('sent: ', res)
}
})
}
console.log('状態: ' + stock)
} catch (e) {
console.log(e)
}
})