ctucx.git: ctucxbot

[nimlang] A telegram bot

commit d71683ed720e254b4fb4102e37c1bd1b9a237611
parent ed18e82a1eb4f152ac0f01f45cbcdda2974ef0a7
Author: ctucx <c@ctu.cx>
Date: Tue, 21 Jul 2020 11:31:12 +0200

add flauschehorn
2 files changed, 33 insertions(+), 22 deletions(-)
M
src/cmd/animalpics.nim
|
20
+++++++++++++++-----
M
src/ctucxbot.nim
|
35
++++++++++++++++++-----------------
diff --git a/src/cmd/animalpics.nim b/src/cmd/animalpics.nim
@@ -2,26 +2,30 @@ import asyncdispatch, telebot, os, times, httpClient, json
 
 type
   AnimalType* = enum
-    fox, cat, dog, penguin
+    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"
+    let ext     = if animated: ".gif" else: ".image"
     var tmpFile = getEnv("DATA_PATH") & "/tmp/" & $(toUnix(getTime())) & ext
-    var url = ""
+    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:

@@ -31,6 +35,12 @@ proc animalCommand* (animalType: AnimalType, animated: bool): proc =
       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"
 

@@ -38,8 +48,8 @@ proc animalCommand* (animalType: AnimalType, animated: bool): proc =
       await httpClient.downloadFile(url, tmpFile)
 
     if animated:
-      discard await bot.sendDocument(command.message.chat.id, "file://" & tmpFile, replyToMessageId = command.message.messageId)
+      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, replyToMessageId = command.message.messageId)
+      discard await bot.sendPhoto(command.message.chat.id, "file://" & tmpFile, caption = text, replyToMessageId = command.message.messageId)
 
     removeFile(tmpFile) 
\ No newline at end of file
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -25,23 +25,24 @@ proc main =  # for gcsafe
     let bot = newTeleBot(getEnv("API_KEY"))
 
     bot.onUpdate(updateHandler)
-    bot.onCommand("daysuntilcongress", daysUntilCongressCommand)
-    bot.onCommand("fox", animalCommand(fox, false))
-    bot.onCommand("penguin", animalCommand(penguin, false))
-    bot.onCommand("dog", animalCommand(dog, false))
-    bot.onCommand("doggo", animalCommand(dog, false))
-    bot.onCommand("debuginfo", debuginfoCommand)
-    bot.onCommand("cat", animalCommand(cat, false))
-    bot.onCommand("catgif", animalCommand(cat, true))
-    bot.onCommand("help", helpCommand)
-    bot.onCommand("stats", statsCommand)
-    bot.onCommand("unixtime", unixtimeCommand)
-    bot.onCommand("utc", utcCommand)
-    bot.onCommand("uptime", uptimeCommand)
-    bot.onCommand("yesorno", yesornoCommand)
-    bot.onCommand("whoami", whoamiCommand)
-    bot.onCommand("s", sarcasmCommand)
-    bot.onCommand("httpstatuscode", httpstatuscodeCommand)
+    bot.onCommand("daysuntilcongress",    daysUntilCongressCommand)
+    bot.onCommand("fox",                  animalCommand(fox, false))
+    bot.onCommand("penguin",              animalCommand(penguin, false))
+    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("flauschehorn",         animalCommand(flauschehorn, false))
+    bot.onCommand("debuginfo",            debuginfoCommand)
+    bot.onCommand("help",                 helpCommand)
+    bot.onCommand("stats",                statsCommand)
+    bot.onCommand("unixtime",             unixtimeCommand)
+    bot.onCommand("utc",                  utcCommand)
+    bot.onCommand("uptime",               uptimeCommand)
+    bot.onCommand("yesorno",              yesornoCommand)
+    bot.onCommand("whoami",               whoamiCommand)
+    bot.onCommand("s",                    sarcasmCommand)
+    bot.onCommand("httpstatuscode",       httpstatuscodeCommand)
     bot.poll(timeout=300)
 
   else: