-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
176 lines (149 loc) · 5.13 KB
/
app.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
"use strict";
var config = require('./config.js')();
if (!config.exists("telegram", "token")) {
console.error("You need to specify a token for the Telegram API");
process.exit(1);
}
var request = require('request');
var TelegramBot = require('node-telegram-bot-api');
// Setup polling way
var bot = new TelegramBot(config.get("telegram", "token"), { polling: true });
var availableProvider = {};
var bing = require('./bing.js')(config.get("bing", "keys"), config.get("bing", "config"));
if (config.get("bing", "keys") && config.get("bing", "keys").length > 0) {
availableProvider["bing"] = {
name: "Bing",
class: bing
};
}
var google = require('./google.js')(config.get("google", "keys"), config.get("google", "config"));
if (config.get("google", "keys") && config.get("google", "keys").length > 0) {
availableProvider["google"] = {
name: "Google",
class: google
};
}
var pr0 = require('./pr0gramm.js')(config.get("pr0gramm","config"));
availableProvider["pr0gramm"] = {
name: "Pr0gramm",
class: pr0
};
if (Object.keys(availableProvider).length == 0) {
console.error("The bot couldn't load any search provider and thus will not work! Have a look at README.md on how to correctly setup search providers.");
process.exit(1);
}
console.log("Bot is running with " + Object.keys(availableProvider).length + " available search providers.");
var gm = require('gm').subClass({imageMagick: true});
var temp = require('temp').track();
var fs = require('fs');
var random = require('./random');
var leet = require('./leet')();
var resolution = config.get("bot", "resolution");
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
function sendPhoto(chatID, path, param){
var tmpout = temp.createWriteStream({suffix: ".png"});
var gms = gm(request(path));
if(resolution){
gms = gms.resize(resolution[0],resolution[1]);
}
gms.stream('png').on('data', function(data) {
tmpout.write(data);
}).on('end', function() {
bot.sendPhoto(chatID, tmpout.path, param)
.then(function() {
tmpout.end();
temp.cleanupSync();
});
});
}
function sendVideo(chatID, path, param){
bot.sendVideo(chatID, request(path), param);
}
function sendText(chatID, text, param){
bot.sendMessage(chatID,text,param);
}
function sendImage(query, msg, nsfw, providername) {
var providername = (providername === undefined || availableProvider[providername] === undefined)
? Object.keys(availableProvider)[random.randomInt(0, Object.keys(availableProvider).length)] : providername;
var provider = availableProvider[providername];
var msgId = msg.message_id;
var chatId = msg.chat.id;
if (provider === undefined) {
bot.sendMessage(chatId, "There are no search providers available!", {
reply_to_message_id: msgId
});
}
console.log("using: " + provider["name"]);
var onSuccess = function(result){
var path = result["contentUrl"];
var caption = result["name"];
console.log(path);
if(path.endsWith("mp4") || path.endsWith("webm") || path.endsWith("gif")){
sendVideo(chatId,path,{
reply_to_message_id: msgId,
caption: caption
})
} else {
sendPhoto(chatId,path,{
reply_to_message_id: msgId,
caption: caption
})
}
};
var onError = function(result){
var message = result.message || "Unknown error!";
bot.sendMessage(chatId, message, {
reply_to_message_id: msgId
});
};
if (provider["class"]) {
provider["class"].getImageData(query, nsfw, onSuccess, onError);
} else {
bot.sendMessage(chatId, "Can't search for images with provider\n```\n" + JSON.stringify(provider) + "\n```", {
reply_to_message_id: msgId
});
}
}
function onCommand(command, query, msg, provider) {
switch (command) {
case "image":
sendImage(query, msg, undefined, provider);
break;
case "nsfw":
sendImage(query, msg, true, provider);
break;
}
}
// Matches /image [whatever]
bot.onText(/\/image (.+)/, function (msg, match) {
onCommand("image", match[1], msg);
});
bot.onText(/\/get (.+)/, function (msg, match) {
onCommand("image", match[1], msg);
});
bot.onText(/\/google (.+)/, function (msg, match) {
onCommand("image", match[1], msg, "google");
});
bot.onText(/\/bing (.+)/, function (msg, match) {
onCommand("image", match[1], msg, "bing");
});
bot.onText(/\/pr0 (.+)/, function (msg, match) {
onCommand("image", match[1], msg, "pr0gramm");
});
bot.onText(/\/wow (.+)/, function (msg, match) {
onCommand("image", "wow such "+match[1], msg);
});
bot.onText(/\/1337 (.+)/, function (msg, match) {
onCommand("image", leet.get(match[1]), msg);
});
if (config.get("bot", "nsfw")) {
// Matches /nsfw [whatever]
bot.onText(/\/nsfw (.+)/, function (msg, match) {
onCommand("nsfw", match[1], msg);
});
}
bot.on('inline_query',function (a,b,c,d) {
console.log(a,b,c,d);
});