-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (169 loc) · 4.35 KB
/
index.js
File metadata and controls
200 lines (169 loc) · 4.35 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const request = require('request');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
const key = process.env.BOT_KEY || 'hankey';
const name = process.env.BOT_NAME || 'JimmySanBot';
const inlineImage = process.env.INLINE_IMAGE || 'poo.jpg';
const inlineThumb = process.env.INLINE_THUMB || 'poo_thumb.jpg';
const lName = name.toLowerCase();
const triggerWords = [
"javascript",
"docker",
"systemd",
"wordpress",
"java",
"js",
"php",
"python",
"lisp",
"clojure",
"node",
"angular",
"react",
"npm",
"yarn",
"perl",
"pascal",
"cobol",
"windows",
"dotnet",
"go",
"golang",
"lua",
"rust",
"asp",
"apple",
"mac",
"macos",
"osx",
"chrome",
"safari",
"ie",
"opera",
"dolphin",
"konqueror",
"chromium",
"vivaldi",
"sony",
"ubuntu",
"debian",
"oracle",
"kotlin",
"firefox",
"mysql",
"adobe",
"dart",
"flutter",
"xamarin",
"blackmagic"
];
function formatMessage(item) {
return `${item} merda 💩`;
}
function formatInlineResult(text) {
var formattedText = formatMessage(text);
return {
type: 'article',
id: text,
thumb_url: inlineThumb,
title: name,
description: `\n${formattedText}`,
input_message_content: {
message_text: formattedText
}
};
}
const triggers = new RegExp(`\\b(${triggerWords.join('|')})\\b`, 'gi');
const muted = [];
function sendMessage(chatId, message, replyTo = undefined, markdown = false) {
const msg = {
chat_id: chatId,
text: message
};
if (replyTo !== undefined) {
msg.reply_to_message_id = replyTo;
}
if (markdown) {
msg.parse_mode = 'markdown';
}
request.post(`https://api.telegram.org/bot${key}/sendMessage`, {
json: msg
});
}
if (process.env.NODE_ENV === 'development') {
sendMessage = function (chatId, message, replyTo = undefined, markdown = false) {
console.log(message);
};
}
app.post(`/bot/${key}`, function (req, res) {
const update = req.body;
if (update.inline_query) {
const query = update.inline_query;
const result = {
inline_query_id: query.id,
next_offset: ""
};
if (query.query === undefined || query.query === "") {
result.results = triggerWords.slice(0, 50).map(formatInlineResult)
} else {
result.results = triggerWords
.filter(i => i.includes(query.query))
.slice(0, 49)
.map(formatInlineResult);
result.results.push(formatInlineResult(query.query));
}
result.method = 'answerInlineQuery';
res.send(result);
return;
}
const message = update.message || update.edited_message;
if (message === undefined || message.text === undefined) {
res.sendStatus(200);
return;
}
const text = message.text.toLowerCase();
const chatId = message.chat.id;
const msgId = message.message_id;
const index = muted.indexOf(chatId);
if (index !== -1) {
if (text === '/unmute' || text === `/unmute@${lName}` || message.text === `Start-Service -Name ${name}`) {
muted.splice(index, 1);
sendMessage(chatId, `\`systemctl start ${lName}.service\``, undefined, true);
}
res.sendStatus(200);
return;
}
if (text === '/mute' || text === `/mute@${lName}` || message.text === `Stop-Service -Name ${name}`) {
muted.push(chatId);
sendMessage(chatId, `\`systemctl stop ${lName}.service\``, undefined, true);
res.sendStatus(200);
return;
}
if (text === '/start' || text === `/start@${lName}`) {
sendMessage(chatId, 'This bot replaces [gionniboy](https://github.com/gionniboy) ' +
'in every conversation you add it to! It was written 100% in JavaScript on a Mac, just for extra fun.\n' +
'You can even use it inline!\n\nSource code [here](https://github.com/kipters/jimmybot), submit a PR if you want!',
undefined, true);
res.sendStatus(200);
return;
}
const triggered = text.match(triggers);
if (triggered) {
triggered.forEach(item => {
if (Math.random() >= 0.5) {
sendMessage(chatId, formatMessage(item), msgId);
}
});
}
res.sendStatus(200);
});
app.get('/', function (req, res) {
res.sendStatus(404);
});
app.listen(app.get('port'), function () {
console.log("Node app is running at localhost:" + app.get('port'))
});