-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.js
280 lines (260 loc) · 8.34 KB
/
bot.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
process.env["NTBA_FIX_319"] = 1;
const TelegramBot = require('node-telegram-bot-api');
const config = require('./config.js')
const ping = require('./ping/ping.js');
const axios = require('axios')
init();
const bot = new TelegramBot(config.TOKEN, {
polling: true
});
var hosts = [`https://api.telegram.org', 'https://google.co.in`];
const API_URL = `https://wallhaven.cc/api/v1/`;
const API_INDEX = `&apikey=${config.API_WALLHAVEN}`;
const WALL_URL = `${API_URL}w/`
const search = async (query) => {
let args = query.split(" ");
let q = "search?q=" + args.join("+");
try {
let response = await axios.get(
`${API_URL}${q}&sorting=random&purity=110${API_INDEX}`
);
let path = await response.data.data[1].path;
let image = await response.data.data[1].thumbs.large;
let surl = await response.data.data[1].short_url;
return {
path: path,
image: image,
surl: surl,
status: "200"
};
} catch (e) {
console.log(`Error: No file found for ${args}`);
return {
status: "404"
};
}
};
const random = async () => {
let response = await axios.get(
`${API_URL}search?sorting=random&purity=110${API_INDEX}`
);
let path = await response.data.data[1].path;
let image = await response.data.data[1].thumbs.large;
let surl = await response.data.data[1].short_url;
return {
path: path,
image: image,
surl: surl,
};
};
const nsfw = async () => {
let response = await axios.get(
`${API_URL}search?sorting=random&purity=001${API_INDEX}`
);
let path = await response.data.data[1].path;
let image = await response.data.data[1].thumbs.large;
let surl = await response.data.data[1].short_url;
return {
path: path,
image: image,
surl: surl,
};
};
const get_wall_using_id = async (query) => {
let id = query.split(" ");
try {
let response = await axios.get(
`${WALL_URL}${id}?apikey=${config.API_WALLHAVEN}`
);
let path = response.data.data.path;
let image = await response.data.data.thumbs.large;
let surl = await response.data.data.short_url;
return {
path: path,
image: image,
surl: surl,
status: "200"
};
} catch (e) {
console.log(`Error: No file found for ${id}`);
return {
status: "404"
};
}
};
const toplist = async (query) => {
let args = query.split(" ");
let response = await axios.get(
`${API_URL}search?sorting=toplist&topRange=${args}${API_INDEX}`
);
let data = response.data.data.map((obj) => obj.path);
return data;
};
//
// C O M M A N D S S T U F F
//
bot.onText(/^\/start/, (msg) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
bot.sendMessage(msg.chat.id, `Hello there! I am a bot based on NodeJS and Wallhaven\'s API!\
\nHit /help to get a list of available commands.`, {
reply_to_message_id: msg.message_id,
});
}
});
bot.onText(/^\/ping/, (msg) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
ping(hosts).then(function (delta) {
bot.sendMessage(msg.chat.id, `Ping time was ` + String(delta) + ` ms.`, {
reply_to_message_id: msg.message_id,
});
console.log(`Starting ping test. Ping time was ` + String(delta) + ` ms`);
}).catch(function (err) {
console.error(`Could not ping remote URL`, err);
});
}
});
bot.onText(/^\/search (.+)/, async (msg, match) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
const resp = await search(match[1]);
if (resp.status != "404") {
await bot.sendPhoto(msg.chat.id, resp.image, {
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [
[{
text: "View on website",
url: resp.surl,
}]
]
}
});
bot.sendDocument(msg.chat.id, resp.path);
} else {
bot.sendMessage(msg.chat.id, `Try again with some other keyword(s)`, {
reply_to_message_id: msg.message_id,
})
}
}
});
bot.onText(/^\/random/, async (msg) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
const resp = await random();
await bot.sendPhoto(msg.chat.id, resp.image, {
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [
[{
text: "View on website",
url: resp.surl,
}]
]
}
});
bot.sendDocument(msg.chat.id, resp.path);
}
});
bot.onText(/^\/nsfw/, async (msg) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
const resp = await nsfw();
await bot.sendPhoto(msg.chat.id, resp.image, {
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [
[{
text: "View on website",
url: resp.surl,
}]
]
}
});
bot.sendDocument(msg.chat.id, resp.path);
}
});
bot.onText(/^\/getwall (.+)/, async (msg, match) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
const resp = await get_wall_using_id(match[1]);
if (resp.status != "404") {
await bot.sendPhoto(msg.chat.id, resp.image, {
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [
[{
text: "View on website",
url: resp.surl,
}]
]
}
});
bot.sendDocument(msg.chat.id, resp.path);
} else {
bot.sendMessage(msg.chat.id, `Try again with some other ID`, {
reply_to_message_id: msg.message_id,
})
}
}
});
bot.onText(/^\/top (.+)/, async (msg, match) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
const resp = await toplist(match[1]);
for (let i = 0; i < 5; i++) {
bot.sendDocument(msg.chat.id, resp[i], {
reply_to_message_id: msg.message_id,
});
}
}
});
bot.onText(/^\/help/, (msg) => {
if (isAuthorized(msg) < 0) {
sendUnauthorizedMessage(msg);
} else {
bot.sendMessage(
msg.chat.id, `/help : To get this message \
\n\n/search <keyword> : To get a wallpaper related to the keyword \
\n\n/nsfw : To get a random NSFW image from Wallhaven \
\n\n/random : To get any random image from Wallhaven \
\n\n/ping : To test the ping of the bot with telegram/google \
\n\n/getwall <id> : Download the wallpaper using it's id \
\n\n/top <1d/3d/1w/1M/3M/6M/1y> : Returns 5 images based on the specified toprange`, {
reply_to_message_id: msg.message_id,
});
}
});
//
// F U N C T I O N S
//
function isAuthorized(msg) {
for (var i = 0; i < config.AUTH_USERS.length; i++) {
if (config.AUTH_USERS[i] == msg.from.id) return 0;
}
if (config.AUTH_CHATS.indexOf(msg.chat.id) > -1 &&
msg.chat.all_members_are_administrators) return 2;
if (config.AUTH_CHATS.indexOf(msg.chat.id) > -1) return 3;
return -1;
}
function sendUnauthorizedMessage(msg) {
bot.sendMessage(msg.chat.id, `You don't have the permission to use this bot.`, {
reply_to_message_id: msg.message_id,
});
}
function init() {
if (config.TOKEN == undefined || config.API_WALLHAVEN == undefined || config.AUTH_USERS == undefined) {
console.log(new Error(`\n\nOne or more variables missing in config. Exiting..\n`));
process.exit(1);
} else {
console.log(`\nBot is up and working! All variables are set.`);
}
};