|
|
@@ -0,0 +1,254 @@ |
|
|
|
var Bot = require('node-telegram-bot');
|
|
|
|
var request = require('request');
|
|
|
|
var fs = require("fs");
|
|
|
|
var token = require("./config").token;
|
|
|
|
var maxQueue = require("./config").maxQueue;
|
|
|
|
var peppysentences = require("./peppysentences");
|
|
|
|
var strings = require("./lngthg");
|
|
|
|
var utils = require("./utils");
|
|
|
|
|
|
|
|
var memoryData = {
|
|
|
|
myName: "",
|
|
|
|
users: []
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!String.prototype.format) {
|
|
|
|
String.prototype.format = function() {
|
|
|
|
var args = arguments;
|
|
|
|
return this.replace(/{(\d+)}/g, function(match, number) {
|
|
|
|
return typeof args[number] != 'undefined'
|
|
|
|
? args[number]
|
|
|
|
: match
|
|
|
|
;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Array.prototype.randomElement = function () {
|
|
|
|
return this[Math.floor(Math.random() * this.length)]
|
|
|
|
}
|
|
|
|
|
|
|
|
var newMember = function(memberName, chatID) {
|
|
|
|
if (memberName == memoryData.myName) {
|
|
|
|
sendWelcomeGroup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: chatID,
|
|
|
|
text: strings.userWelcomeInGroup.randomElement().format(memberName)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var sendWelcomeGroup = function(chatID) {
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: chatID,
|
|
|
|
text: strings.welcomeGroup
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var sendWelcomeUser = function(chatID) {
|
|
|
|
var userPref = getUserInMemory(chatID);
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: chatID,
|
|
|
|
text: strings.welcomeUser.format((userPref.last_tags === null ? strings.noPreviousTags : strings.lastTagsWere.format(userPref.last_tags)), userPref.rating, maxQueue)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var sendPic = function(userID, chatID) {
|
|
|
|
// Get the user preferences
|
|
|
|
var user = getUserInMemory(userID);
|
|
|
|
console.log("[BOT STATUS] Sending hentai picture to {0}...".format(userID));
|
|
|
|
// First request, just to get the count of posts.
|
|
|
|
request("http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=0&tags=" + encodeURIComponent((user.last_tags === null? "" : user.last_tags) + " -webm " + (user.rating != "everything"? "rating:" + user.rating : "")), function(err, re, body) {
|
|
|
|
if (err)
|
|
|
|
throw err;
|
|
|
|
utils.parseXml(body, function(err, result) {
|
|
|
|
if (err)
|
|
|
|
throw err;
|
|
|
|
postCount = result.posts.$.count;
|
|
|
|
if (postCount == 0) {
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: userID,
|
|
|
|
text: "I'm sorry, but I was unable to find any images for those tags. Perhaps try again? I have reset the tags for you, btw."
|
|
|
|
});
|
|
|
|
memoryData.users[userID].last_tags = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
postID = utils.r(1, (+postCount));
|
|
|
|
request("http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=1&tags=" + encodeURIComponent((user.last_tags === null? "" : user.last_tags) + " -webm " + (user.rating != "everything"? "rating:" + user.rating : "")) + "&pid=" + postID, function (err, re, body) {
|
|
|
|
utils.parseXml(body, function(err, result) {
|
|
|
|
postUrl = result.posts.post[0].$.file_url;
|
|
|
|
actualPostID = result.posts.post[0].$.id;
|
|
|
|
splitPostUrl = postUrl.split("/");
|
|
|
|
filename = splitPostUrl[splitPostUrl.length - 1];
|
|
|
|
temp_filename = 'tmp/' + utils.r(0, 2000) + filename;
|
|
|
|
request(postUrl, function(err, re, body) {
|
|
|
|
bot.sendPhoto(
|
|
|
|
{
|
|
|
|
chat_id: chatID,
|
|
|
|
files: {
|
|
|
|
photo: temp_filename
|
|
|
|
},
|
|
|
|
caption: "source: http://gelbooru.com/index.php?page=post&s=view&id=" + actualPostID
|
|
|
|
}, function(){
|
|
|
|
fs.unlink(temp_filename, function(){});
|
|
|
|
memoryData.users[userID].queue -= 1;
|
|
|
|
if (memoryData.users[userID].queue > 0) {
|
|
|
|
sendPic(userID, chatID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}).pipe(fs.createWriteStream(temp_filename));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var slashGet = function(userID, chatID, command) {
|
|
|
|
var tags;
|
|
|
|
if (command.indexOf(" ") !== -1)
|
|
|
|
tags = command.trim().substr(command.indexOf(" ")).trim();
|
|
|
|
else
|
|
|
|
tags = null;
|
|
|
|
getUserInMemory(userID);
|
|
|
|
memoryData.users[userID].last_tags = tags;
|
|
|
|
multiple(userID, chatID, 1);
|
|
|
|
};
|
|
|
|
var changeRating = function(userID, chatID, newRating) {
|
|
|
|
getUserInMemory(userID);
|
|
|
|
if (newRating !== "everything" && newRating !== "explicit"
|
|
|
|
&& newRating !== "questionable" && newRating !== "safe")
|
|
|
|
memoryData.users[userID].rating = newRating;
|
|
|
|
else
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: chatID,
|
|
|
|
text: "You what. That isn't a valid rating. Say /start privately to me to know how to do a proper /rating command."
|
|
|
|
});
|
|
|
|
multiple(userID, chatID, 1);
|
|
|
|
};
|
|
|
|
var multiple = function(userID, chatID, times) {
|
|
|
|
// just to create if doesn't exist
|
|
|
|
getUserInMemory(userID);
|
|
|
|
memoryData.users[userID].queue += times;
|
|
|
|
if (memoryData.users[userID].queue > maxQueue) {
|
|
|
|
memoryData.users[userID].queue = maxQueue;
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: chatID,
|
|
|
|
text: "Calm down a bit! You're requesting too many images. I've set the queue to send {0} images, although please do not abuse the system!".format(maxQueue)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
sendPic(userID, chatID);
|
|
|
|
}
|
|
|
|
// reminder to self: in sendPic, add a try {} catch (e) {sendpic again} or whatever system
|
|
|
|
// reminder to self: setting to just give url to gelbooru pic, that is default to true because it reduces bandwith, a lot.
|
|
|
|
var getUserInMemory = function(user) {
|
|
|
|
if (typeof memoryData.users[user] === "undefined") {
|
|
|
|
// default values
|
|
|
|
memoryData.users[user] = {
|
|
|
|
last_tags: "-futanari",
|
|
|
|
rating: "explicit",
|
|
|
|
queue: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return memoryData.users[user];
|
|
|
|
};
|
|
|
|
|
|
|
|
var bot = new Bot({
|
|
|
|
// Token is in config.js. Check top of this script.
|
|
|
|
token: token
|
|
|
|
});
|
|
|
|
bot.on('message', function (message) {
|
|
|
|
//console.log(message); // Comment if too verbose.
|
|
|
|
var isGroup = (message.from.id != message.chat.id);
|
|
|
|
message.text = (message.text ? message.text : "no text");
|
|
|
|
if (isGroup)
|
|
|
|
console.log("{0} => {1}: {2}".format(message.from.first_name, message.chat.title, message.text));
|
|
|
|
else
|
|
|
|
console.log("{0}: {1}".format(message.from.first_name, message.text));
|
|
|
|
if (message.new_chat_participant) {
|
|
|
|
newMember(message.new_chat_participant.first_name, message.chat.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// command
|
|
|
|
if (message.text[0] == "/") {
|
|
|
|
var command = message.text.substr(1).split(" ");
|
|
|
|
// if there's a @boorubot at the end of the first parameter, trim it
|
|
|
|
command[0] = (command[0].substr(-9) == "@boorubot" ? command[0].substr(0, command[0].length - 9) : command[0]);
|
|
|
|
switch (command[0].toLowerCase()) {
|
|
|
|
case "start":
|
|
|
|
case "help":
|
|
|
|
if (isGroup)
|
|
|
|
sendWelcomeGroup(message.chat.id);
|
|
|
|
else
|
|
|
|
sendWelcomeUser(message.chat.id);
|
|
|
|
break;
|
|
|
|
case "peppy":
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: peppysentences.randomElement()
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "get":
|
|
|
|
slashGet(message.from.id, message.chat.id, message.text);
|
|
|
|
break;
|
|
|
|
case "rating":
|
|
|
|
changeRating(message.from.id, message.chat.id, command[1]);
|
|
|
|
break;
|
|
|
|
case "multiple":
|
|
|
|
if (!isGroup) {
|
|
|
|
var actualnum = parseInt(command[1]);
|
|
|
|
if (actualnum === NaN) {
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: "What the fuck is '{0}'?! For sure not a number.".format(command[1])
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (actualnum < 1) {
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: "https://www.youtube.com/watch?v=cjZdFg3F05E"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
multiple(message.from.id, message.chat.id, actualnum);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: "Yo, {0}. I can't send multiple pics in a group, you know.".format(message.from.first_name)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!isGroup)
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: "I-I'm sorry {0}-san, senpai didn't teach me how to reply to that command...".format(message.from.first_name)
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (message.text.toLowerCase().trim()) {
|
|
|
|
case "hentai!":
|
|
|
|
case "hlkup":
|
|
|
|
multiple(message.from.id, message.chat.id, 1);
|
|
|
|
break;
|
|
|
|
case "h":
|
|
|
|
if (!isGroup)
|
|
|
|
multiple(message.from.id, message.chat.id, 1);
|
|
|
|
break;
|
|
|
|
case "ping":
|
|
|
|
bot.sendMessage({
|
|
|
|
chat_id: message.chat.id,
|
|
|
|
text: ["pong I guess?", "Ciao mauro!", "You what, we're not playing bloody ping pong.", "Pinging 8.8.8.8 with 32 bytes of data:\nReply from 8.8.8.8: bytes=32 time=53ms TTL=56", "Hey, I'm alive!", "/me tips fedora"].randomElement()
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.start()
|
|
|
|
.getMe(function(err, res) {
|
|
|
|
if (err !== null)
|
|
|
|
throw err;
|
|
|
|
console.log("Oh hey there young ladies! boorugram here, running on @" + res.username + " under the name " + res.first_name + ". Ready to serve!");
|
|
|
|
memoryData.myName = res.first_name;
|
|
|
|
});
|