-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (55 loc) · 1.61 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
const {IncomingWebhook} = require('@slack/client');
const {WebClient} = require('@slack/client');
/**
* 可以發送IncomingWebhook與WebClient訊息的物件
*/
class SlackApiHelper {
/**
* 建構子 沒用到
*/
constructor() {
}
/**
* 建立一個可以發送IncomingWebhook與WebClient訊息的物件
* @param {string} url IncomingWebhook的url
*/
async establishWebhook( url = '' ) {
this.url = url;
this.webhook = new IncomingWebhook(this.url);
}
/**
* 發送IncomingWebhook訊息.
* @param {string} text 要發送的訊息
*/
async sendWebhookMessage(text = '') {
this.webhook.send(text, function(err, res) {
if (err) {
console.log('Error:', err);
} else {
// success
}
});
}
/**
* 建立WebClient
* @param {string} token 一個合法的token
*/
async establishWebClient(token = '') {
this.token = token;
this.web = new WebClient(this.token);
}
/**
* 發送WebClient訊息(利用web api)
* @param {string} to 要發送給誰,can be a channel ID(or name), a DM ID, a MPDM ID, or a group ID
* @param {string} text 文字訊息
* @param {json} attach 附加訊息,如按鈕,圖片等.
*/
async sendWebClientMessage(to = '', text = '', attach = {}) {
this.web.chat.postMessage(to, text, attach)
.then((res) => {
console.log('Message sent: ', res.ts);
})
.catch(console.error);
}
}
module.exports = SlackApiHelper;