-
Notifications
You must be signed in to change notification settings - Fork 1
/
DQ.js
178 lines (113 loc) · 3.69 KB
/
DQ.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
const util = require('util');
const _ = require('underscore');
const request = require('request');
const URL = require('url');
const string = require('string');
const logger = require('intel');
const modulesList = require('./lib/modulesList');
const modules = require('./lib/modules');
// Class constructor
const DQ = function (params) {
this._token = params.token;
this._host = URL.format({
protocol: "https",
host: "api.telegram.org",
pathname: "bot"
});
this._parent = (typeof params.parent === 'undefined') ? null : params.parent;
this._recipient = null;
this._offset = 0;
this._httpGetUpdatesUrl = this._host + this._token + "/getUpdates";
this._sendMessageUrl = this._host + this._token + "/sendMessage";
this._moduleList = (typeof params.moduleList === 'undefined') ? modulesList : params.moduleList;
this._modules = (typeof params.modules === 'undefined') ? modules : params.modules;
this.listen = (cb) => {
setInterval(() => {
this._getUpdates((err) => {
if (err) cb(er);
});
}, 3000);
}
this.send = (data, cb) => {
const to = data.to;
const text = data.text;
const url = this._sendMessageUrl + "?chat_id=" + to + "&text=" + text;
request(url, (err, response, body) => {
if (err) cb(err);
console.log("Message sent.")
});
}
this.initModule = (text) => {
if (this._hasCommand(text)){
const moduleName = this._getCommandName(text);
return this._modules[moduleName](text);
} else return this._modules.default();
}
this._httpGet = (cb) => {
const url = this._httpGetUpdatesUrl + "?offset=" + this._offset;
request(url, (err, res, body) => {
if (err) cb(err);
const bodyObj = JSON.parse(body);
if (bodyObj.ok) {
const messages = bodyObj.result;
if (messages.length > 0) {
this._updateOffset(messages);
cb(null, messages);
} else {
logger.info("No new messages..");
return cb(null);
}
} else return cb("Response looks wrong..");
});
}
this._getUpdates = (cb) => {
this._httpGet((err, messages) => {
if (err) cb(err);
this._eachMessage(messages, (err, message) => {
if (err) logger.error(err);
this.emit('message', message);
});
});
}
this._eachMessage = (msgs, cb) => {
_.each(msgs, (msg) => {
const to = this._recipient = msg.message.from.id;
const text = msg.message.text;
cb(null, {
to,
text
});
});
}
this._hasCommand = (text) => {
const modules = this._moduleList;
for (let key in modules) {
if (modules.hasOwnProperty(key)) {
if (string(text).contains(modules[key])) return true;
}
}
return false;
}
this._getCommandName = (text) => {
const modules = this._moduleList;
for (let key in modules) {
if (modules.hasOwnProperty(key)) {
if (string(text).contains(modules[key])) return key;
}
}
}
this._updateOffset = (messages) => {
this._offset = this._getHighestOffset(messages) + 1;
logger.info("Updating offset..");
}
this._getHighestOffset = (messages) => {
let arr = [];
_.map(messages, (msg) => {
arr.push(msg.update_id);
});
return Math.max.apply(null, arr);
}
};
util.inherits(DQ, require('events').EventEmitter);
module.exports = DQ;