This repository has been archived by the owner on Apr 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBearyBot.js
94 lines (81 loc) · 2.24 KB
/
BearyBot.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
'use strict';
const http = require('https');
const url = require('url');
class Msg {
constructor(msgObj) {
if (!msgObj) return this;
this.text = msgObj.text || '';
this.markdown = msgObj.markdown || '';
this.channel = msgObj.channel || '';
this.user = msgObj.user || '';
this.attachments = msgObj.attachments || new Array();
}
Text(arg) {
if (arg) return this.text = arg;
else return this.text;
}
Markdown(arg) {
if (arg) return this.markdown = arg;
else return this.markdown;
}
Channel(arg) {
if (arg) return this.channel = arg;
else return this.channel;
}
User(arg) {
if (arg) return this.user = arg;
else return this.user;
}
AttachTitle(arg) {
if (arg) return this.attachments[0].title = arg;
else return this.attachments[0].title;
}
AttachText(arg) {
if (arg) return this.attachments[0].text = arg;
else return this.attachments[0].text;
}
AttachColor(arg) {
if (arg) return this.attachments[0].color = arg;
else return this.attachments[0].color;
}
Img(imgUrl) {
this.attachments[0].images = [{
url: imgUrl
}]
}
}
class Bot {
constructor(WebHookUrl) {
let hookPath = url.parse(WebHookUrl);
this.reqOpt = {
protocol: hookPath.protocol,
method: 'POST',
hostname: hookPath.hostname,
path: hookPath.path,
headers: {
'content-type': 'application/json'
}
}
}
Send(someMsg, callback) {
console.log(JSON.stringify(someMsg));
var req = http.request(this.reqOpt);
req.on('aborted', () => {
console.log(`Message aborted by server :(`);
});
req.end(JSON.stringify(someMsg), 'utf8', () => {
console.log(`Message Sent.`);
});
req.on('response', (res) => {
console.log(`Msg was received. Response: [${res.statusCode}]`);
res.pipe(process.stdout);
res.on('end', e => {
callback && callback(res);
});
});
}
}
module.exports = {
Bot: Bot,
Msg: Msg
}