1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 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)