danboo/bot.own

80 lines
2.0 KiB
Scala
Raw Normal View History

2024-08-15 00:26:29 +03:00
use std, functional, types, regex, date
2024-08-15 00:20:16 +03:00
include "own-modules/telegram-bot/TelegramBot.own"
include "config.own"
include "DanbooruApi.own"
DANBOORU_REGEX = regex("https://danbooru[^0-9 ]+([0-9]{5,9})")
bot = new TelegramBot(config.telegram.token)
api = new DanbooruApi(config.danbooru.auth)
lastUpdateId = -1
while true {
2024-08-16 23:04:03 +03:00
sleep(1200)
2024-08-15 00:20:16 +03:00
updates = bot.getUpdatesSync(lastUpdateId + 1, 50, 58)
if (updates.isEmpty()) continue
2024-08-15 12:10:08 +03:00
for update : updates {
lastUpdateId = update.update_id
processUpdate(update)
}
2024-08-15 00:20:16 +03:00
}
// Confirm updates
bot.getUpdatesSync(lastUpdateId + 1, 1, 10)
thread(def() {
sleep(2000)
exit(0)
})
def processUpdate(update) {
2024-08-15 12:10:08 +03:00
if !arrayKeyExists("message", update) return 0
2024-08-15 00:20:16 +03:00
msg = update.message
text = msg.text ?? ""
2024-08-15 12:10:08 +03:00
if text.isEmpty() return 0
2024-08-15 00:20:16 +03:00
m = DANBOORU_REGEX.matcher(text)
while m.find() {
2024-08-15 12:10:08 +03:00
processPost(m.group(1), msg.chat.id, msg.message_id, msg.from)
sleep(2000)
2024-08-15 00:20:16 +03:00
}
}
2024-08-15 12:10:08 +03:00
def processPost(id, chatId, msgId, from) {
2024-08-15 00:20:16 +03:00
post = api.getPost(id)
2024-08-15 00:26:29 +03:00
url = "https://danbooru.donmai.us/posts/" + id
2024-08-16 23:04:03 +03:00
println "%s: %s (%d %d)".sprintf(newDate(), url, chatId, msgId)
2024-08-15 00:20:16 +03:00
if !post.isImage() return 0
blacklistTags = config.danbooru.blacklistTags
if post.containsTags(blacklistTags) return 0
caption = getCaption(post)
2024-08-15 12:10:08 +03:00
sender = ""
if (from.id != chatId) sender = from.first_name + " "
buttonText = sprintf("%s%s 📈%d", sender, post.getResolution(), post.getScore())
markup = jsonencode({"inline_keyboard": [
[{"text":buttonText, "url":url}]
]})
2024-08-15 00:20:16 +03:00
bot.invoke("sendPhoto", {
"chat_id": chatId,
"photo": post.getImageUrl(),
"parse_mode": "html",
"caption": caption,
2024-08-15 12:10:08 +03:00
"reply_markup": markup
2024-08-15 00:20:16 +03:00
}, def(r) {
2024-08-15 12:10:08 +03:00
bot.invoke("deleteMessage", {
"chat_id": chatId,
"message_id": msgId,
}, def(r) = 1)
2024-08-15 00:20:16 +03:00
if !r.contains("wrong file identifier") return 1
bot.invoke("sendPhoto", {
"chat_id": chatId,
"photo": post.getSampleImageUrl(),
"parse_mode": "html",
"caption": "small " + caption,
2024-08-15 12:10:08 +03:00
"reply_markup": markup
2024-08-15 00:20:16 +03:00
}, def(r) = 1)
})
}