commit 7387b66befe41a14f3878a1a03ce1c1022a4c3b7
parent 82dd7f5122a1674598c2bc534bb2a043fa60c6db
Author: Milan Pässler <me@pbb.lc>
Date: Thu, 16 Jan 2020 01:41:27 +0100
parent 82dd7f5122a1674598c2bc534bb2a043fa60c6db
Author: Milan Pässler <me@pbb.lc>
Date: Thu, 16 Jan 2020 01:41:27 +0100
new commands: /penguin, /dog, /doggo, /cat, /catgif
2 files changed, 50 insertions(+), 11 deletions(-)
diff --git a/src/cmd/animalpics.nim b/src/cmd/animalpics.nim @@ -1,13 +1,48 @@ -import asyncdispatch, telebot, strutils, os, times, httpClient +import asyncdispatch, telebot, strutils, os, times, httpClient, json -proc foxCommand(bot: Telebot, command: Command) {.async.} = - var httpClient = newHttpClient() +type + AnimalType = enum + fox, cat, dog, penguin - let tmpFile = getCurrentDir() & "/data/tmp/" & $(toUnix(getTime())) & ".image" +proc animalCommand(animalType: AnimalType, animated: bool): proc = + return proc(bot: Telebot, command: Command) {.async.} = + var httpClient = newHttpClient() - httpClient.downloadFile("https://foxrudor.de", tmpFile) + let ext = if animated: ".gif" else: ".image" + var tmpFile = getCurrentDir() & "/data/tmp/" & $(toUnix(getTime())) & ext + var url = "" - var message = newPhoto(command.message.chat.id, "file://" & tmpFile) - message.replyToMessageId = command.message.messageId - discard bot.send(message) - removeFile(tmpFile)- \ No newline at end of file + if animalType == fox and not animated: + url = "https://foxrudor.de" + elif animalType == penguin and not animated: + url = "https://pingudor.de" + elif animalType == dog and not animated: + httpClient.headers = newHttpHeaders({ "x-api-key": "be36a500-d33f-4c64-a100-4269a9dc177f" }) + let imginfo = parseJson(httpClient.getContent("https://api.thedogapi.com/v1/images/search?format=json&mime_types=png,jpg&order=RANDOM&limit=1")) + url = imginfo[0]["url"].getStr() + elif animalType == cat: + var filetypes = "" + if animated: + filetypes = "gif" + else: + filetypes = "png,jpg" + httpClient.headers = newHttpHeaders({ "x-api-key": "fe9beabb-d02d-4eb4-984e-5772f7a49703" }) + let imginfo = parseJson(httpClient.getContent("https://api.thecatapi.com/v1/images/search?format=json&mime_types=" & filetypes & "&order=RANDOM&limit=1")) + url = imginfo[0]["url"].getStr() + else: + tmpFile = getCurrentDir() & "/data/pics/unknown_animal.webp" + + if url != "": + httpClient.downloadFile(url, tmpFile) + + + template `sendMessage` (messageExp: untyped): untyped = + var message = messageExp + message.replyToMessageId = command.message.messageId + discard bot.send(message) + removeFile(tmpFile) + + if animated: + sendMessage(newDocument(command.message.chat.id, "file://" & tmpFile)) + else: + sendMessage(newPhoto(command.message.chat.id, "file://" & tmpFile))
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim @@ -56,7 +56,12 @@ let bot = newTeleBot(API_KEY) bot.onUpdate(updateHandler) bot.onCommand("daysuntilcongress", daysUntilCongressCommand) -bot.onCommand("fox", foxCommand) +bot.onCommand("fox", animalCommand(fox, false)) +#bot.onCommand("penguin", animalCommand(penguin, false)) # pingudor.de is offline +bot.onCommand("dog", animalCommand(dog, false)) +bot.onCommand("doggo", animalCommand(dog, false)) +bot.onCommand("cat", animalCommand(cat, false)) +bot.onCommand("catgif", animalCommand(cat, true)) bot.onCommand("help", helpCommand) bot.onCommand("stats", statsCommand) bot.onCommand("uptime", uptimeCommand)