Fix updates, show more info
This commit is contained in:
parent
380896a0ce
commit
587a68898a
37
bot.own
37
bot.own
@ -12,11 +12,13 @@ lastUpdateId = -1
|
||||
while true {
|
||||
sleep(6200)
|
||||
updates = bot.getUpdatesSync(lastUpdateId + 1, 50, 58)
|
||||
println updates
|
||||
if (updates.isEmpty()) continue
|
||||
|
||||
lastUpdateId = stream(updates)
|
||||
.map(::processUpdate)
|
||||
.reduce(0, def(a, b) = a + b)
|
||||
for update : updates {
|
||||
lastUpdateId = update.update_id
|
||||
processUpdate(update)
|
||||
}
|
||||
}
|
||||
// Confirm updates
|
||||
bot.getUpdatesSync(lastUpdateId + 1, 1, 10)
|
||||
@ -27,22 +29,18 @@ thread(def() {
|
||||
|
||||
|
||||
def processUpdate(update) {
|
||||
if !arrayKeyExists("message", update) {
|
||||
return update.update_id
|
||||
}
|
||||
if !arrayKeyExists("message", update) return 0
|
||||
msg = update.message
|
||||
text = msg.text ?? ""
|
||||
if text.isEmpty() {
|
||||
return update.update_id
|
||||
}
|
||||
if text.isEmpty() return 0
|
||||
m = DANBOORU_REGEX.matcher(text)
|
||||
while m.find() {
|
||||
processPost(m.group(1), msg.chat.id, msg.message_id)
|
||||
processPost(m.group(1), msg.chat.id, msg.message_id, msg.from)
|
||||
sleep(2000)
|
||||
}
|
||||
return update.update_id
|
||||
}
|
||||
|
||||
def processPost(id, chatId, msgId) {
|
||||
def processPost(id, chatId, msgId, from) {
|
||||
post = api.getPost(id)
|
||||
url = "https://danbooru.donmai.us/posts/" + id
|
||||
println "%s: %s".sprintf(newDate(), url)
|
||||
@ -51,21 +49,30 @@ def processPost(id, chatId, msgId) {
|
||||
if post.containsTags(blacklistTags) return 0
|
||||
|
||||
caption = getCaption(post)
|
||||
reply = jsonencode({"message_id": msgId, "chat_id": chatId})
|
||||
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}]
|
||||
]})
|
||||
bot.invoke("sendPhoto", {
|
||||
"chat_id": chatId,
|
||||
"reply_parameters": reply,
|
||||
"photo": post.getImageUrl(),
|
||||
"parse_mode": "html",
|
||||
"caption": caption,
|
||||
"reply_markup": markup
|
||||
}, def(r) {
|
||||
bot.invoke("deleteMessage", {
|
||||
"chat_id": chatId,
|
||||
"message_id": msgId,
|
||||
}, def(r) = 1)
|
||||
if !r.contains("wrong file identifier") return 1
|
||||
bot.invoke("sendPhoto", {
|
||||
"chat_id": chatId,
|
||||
"reply_parameters": reply,
|
||||
"photo": post.getSampleImageUrl(),
|
||||
"parse_mode": "html",
|
||||
"caption": "small " + caption,
|
||||
"reply_markup": markup
|
||||
}, def(r) = 1)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user