forked from osdc/osdc-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.js
157 lines (136 loc) · 4.09 KB
/
chatbot.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const Faye = require('faye');
const request = require('request');
const config = require('./config');
const joker = require('./services/jokeService.js');
const wiki = require('./services/wikiService');
const ROOM_ID = config.roomId;
const TOKEN = config.token;
const DEPLOY_FLAG = process.env.DEPLOY || false;
const META_HANDSHAKE_SUFFIX_URL = '/meta/handshake';
const FAYE_CLIENT_URL = 'https://ws.gitter.im/faye';
const SERVER_PREFIX_URL = "http://127.0.0.1:5000";
const SERVER_DEPLOY_URL = `${SERVER_PREFIX_URL}/deploy`;
const SERVER_HOWDOI_PREFIX_URL = `${SERVER_PREFIX_URL}/howdoi?query=`;
const CHATROOM_SUFFIX_URL = `/v1/rooms/${ROOM_ID}/chatMessages`;
const CHATROOM_URL = `https://api.gitter.im${CHATROOM_SUFFIX_URL}`;
const BOT_MENTION_NAME = '@osdc-bot';
const BOT_ACTIONS = {
HELP: 'help',
JOKE: 'joke',
DEPLOY: 'deploy',
HOWDOI: 'howdoi',
WIKI : 'wiki'
};
// Authentication extension
var ClientAuthExt = function() {};
ClientAuthExt.prototype.outgoing = (message, callback) => {
if (message.channel === META_HANDSHAKE_SUFFIX_URL) {
if (!message.ext) { message.ext = {}; }
message.ext.token = TOKEN;
}
callback(message);
};
ClientAuthExt.prototype.incoming = (message, callback) => {
if(message.channel === META_HANDSHAKE_SUFFIX_URL) {
if(message.successful) {
console.log('Successfuly subscribed to room: ', ROOM_ID);
if (DEPLOY_FLAG) {
send("Deployment Successful");
}
} else {
console.log('Something went wrong: ', message.error);
}
}
callback(message);
};
// Faye client
const client = new Faye.Client(FAYE_CLIENT_URL, {
timeout: 60,
retry: 5,
interval: 1
});
// Add Client Authentication extension
client.addExtension(new ClientAuthExt());
// A dummy handler to echo incoming messages
const messageHandler = (msg) => {
if (msg.model && msg.model.fromUser) {
console.log("Message: ", msg.model.text);
console.log("From: ", msg.model.fromUser.displayName);
reply_to_user(msg.model.fromUser, msg.model.text);
}
};
function _getStartsWith(parsedMessage) {
var result = null;
for (var botAction in BOT_ACTIONS) {
if (parsedMessage.startsWith(BOT_ACTIONS[botAction])) {
result = BOT_ACTIONS[botAction];
console.log(`[INFO] In loop ${result}`);
break;
}
}
return result;
}
function _getBotHelp() {
var resultString = "You can:";
for (var botAction in BOT_ACTIONS) {
resultString += `\n- ${BOT_ACTIONS[botAction]}`;
}
return resultString;
}
function reply_to_user(user, message) {
const displayName = user.displayName;
const username = user.username;
if (message.startsWith(BOT_MENTION_NAME)) {
const parsedMessage = message.slice(BOT_MENTION_NAME.length + 1);
console.log(parsedMessage);
const startsWithString = _getStartsWith(parsedMessage);
if (startsWithString === BOT_ACTIONS.HELP) {
send(_getBotHelp(), username);
}
if (startsWithString === BOT_ACTIONS.JOKE) {
joker.getJoke(send, username);
}
if (startsWithString === BOT_ACTIONS.DEPLOY) {
request({
url: SERVER_DEPLOY_URL,
method: "GET"
}, (error, response, body) => {
console.log(response);
});
}
if (startsWithString === BOT_ACTIONS.HOWDOI) {
var query = encodeURIComponent(parsedMessage.slice(7, parsedMessage.length));
console.log(query);
request({
url: SERVER_HOWDOI_PREFIX_URL + query,
method: "GET"
}, (error, response, body) => {
console.log(body);
send(body);
});
}
if (startsWithString === BOT_ACTIONS.WIKI) {
var requestedData = parsedMessage.slice(5, parsedMessage.length);
wiki(send, username, requestedData);
}
}
}
function _postOnChat(message) {
request({
url: CHATROOM_URL,
headers: {
Authorization : `Bearer ${TOKEN}`
},
method: "POST",
json: true,
body: {
text: message
}
}, (error, response, body) => {
console.log(response);
});
}
function send(message, username) {
_postOnChat(username ? `@${username} ${message}` : message);
}
client.subscribe(`/api${CHATROOM_SUFFIX_URL}`, messageHandler, {});