import asyncdispatch, telebot, os, times, httpClient, json type AnimalType* = enum fox, cat, dog, penguin, flauschehorn proc animalCommand* (animalType: AnimalType, animated: bool): proc = return proc(bot: Telebot, command: Command): Future[bool] {.async.} = var httpClient = newAsyncHttpClient() let ext = if animated: ".gif" else: ".image" var tmpFile = getEnv("DATA_PATH") & "/tmp/" & $(toUnix(getTime())) & ext var url = "" var text = "" discard existsOrCreateDir(getEnv("DATA_PATH") & "/tmp") 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(await 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(await httpClient.getContent("https://api.thecatapi.com/v1/images/search?format=json&mime_types=" & filetypes & "&order=RANDOM&limit=1")) url = imginfo[0]["url"].getStr() elif animalType == flauschehorn: let response = parseJson(await httpClient.getContent("https://flauschehorn.sexy/api.php?format=json")) url = response["image"].getStr() text = response["toot"].getStr() else: tmpFile = getEnv("ASSETS_PATH") & "/pics/unknown_animal.webp" if url != "": await httpClient.downloadFile(url, tmpFile) if animated: discard await bot.sendDocument(command.message.chat.id, "file://" & tmpFile, caption = text, replyToMessageId = command.message.messageId) else: discard await bot.sendPhoto(command.message.chat.id, "file://" & tmpFile, caption = text, replyToMessageId = command.message.messageId) removeFile(tmpFile)