ctucx.git: ctucxbot

[nimlang] A telegram bot

commit 03ddefe7b97118456c3f96334aeaa1b7a6a3d32f
parent b507c33d462b27d17206b12e2fccd9e7b8afb19c
Author: Milan Pässler <me@pbb.lc>
Date: Sat, 25 Jan 2020 00:11:29 +0100

use async functions
11 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/src/cmd/animalpics.nim b/src/cmd/animalpics.nim
@@ -6,7 +6,7 @@ type
 
 proc animalCommand(animalType: AnimalType, animated: bool): proc =
   return proc(bot: Telebot, command: Command) {.async.} =
-    var httpClient = newHttpClient()
+    var httpClient = newAsyncHttpClient()
 
     let ext = if animated: ".gif" else: ".image"
     var tmpFile = getCurrentDir() & "/data/tmp/" & $(toUnix(getTime())) & ext

@@ -18,7 +18,7 @@ proc animalCommand(animalType: AnimalType, animated: bool): proc =
       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"))
+      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 = ""

@@ -27,19 +27,19 @@ proc animalCommand(animalType: AnimalType, animated: bool): proc =
       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"))
+      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()
     else:
       tmpFile = getCurrentDir() & "/data/pics/unknown_animal.webp"
 
     if url != "":
-      httpClient.downloadFile(url, tmpFile)
+      await httpClient.downloadFile(url, tmpFile)
 
 
     template `sendMessage` (messageExp: untyped): untyped =
       var message = messageExp
       message.replyToMessageId = command.message.messageId
-      discard bot.send(message)
+      discard await bot.send(message)
       removeFile(tmpFile)
 
     if animated:
diff --git a/src/cmd/daysuntilcongress.nim b/src/cmd/daysuntilcongress.nim
@@ -7,4 +7,4 @@ proc daysUntilCongressCommand(bot: Telebot, command: Command) {.async.} =
   var message                           =   newMessage(command.message.chat.id, answer)
   message.replyToMessageId              =   command.message.messageId
   message.parseMode                     =   "markdown"
-  discard bot.send(message)
+  discard await bot.send(message)
diff --git a/src/cmd/debuginfo.nim b/src/cmd/debuginfo.nim
@@ -27,4 +27,4 @@ proc debuginfoCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, "```" & pretty(json) & "```")
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/help.nim b/src/cmd/help.nim
@@ -37,4 +37,4 @@ proc helpCommand(bot: Telebot, command: Command) {.async.} =
   var message                           =   newMessage(command.message.chat.id, answer)
   message.replyToMessageId              =   command.message.messageId
   message.parseMode                     =   "markdown"
-  discard bot.send(message)
+  discard await bot.send(message)
diff --git a/src/cmd/invitelinks.nim b/src/cmd/invitelinks.nim
@@ -23,4 +23,4 @@ proc invitelinksCommand(bot: Telebot, command: Command) {.async.} =
 
   var message = newMessage(command.message.chat.id, text)
   message.replyToMessageId = command.message.messageId
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/stats.nim b/src/cmd/stats.nim
@@ -45,7 +45,7 @@ proc statsCommand(bot: Telebot, command: Command) {.async.} =
 
   var message = newMessage(command.message.chat.id, text)
   message.replyToMessageId = command.message.messageId
-  discard bot.send(message)
+  discard await bot.send(message)
 
 proc statsHandler(message: Message, penis: bool) {.async.} =
   let user     = message.fromUser.get
diff --git a/src/cmd/unixtime.nim b/src/cmd/unixtime.nim
@@ -4,4 +4,4 @@ proc unixtimeCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, "```" & $(toUnix(getTime())) & "```")
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/uptime.nim b/src/cmd/uptime.nim
@@ -5,4 +5,4 @@ proc uptimeCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, "```" & uptime & "```")
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/utc.nim b/src/cmd/utc.nim
@@ -5,4 +5,4 @@ proc utcCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, time.format("dd'.'MM'.'yyyy' 'HH':'mm"))
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/whoami.nim b/src/cmd/whoami.nim
@@ -11,4 +11,4 @@ proc whoamiCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, text)
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)+
\ No newline at end of file
diff --git a/src/cmd/yesorno.nim b/src/cmd/yesorno.nim
@@ -21,4 +21,4 @@ proc yesornoCommand(bot: Telebot, command: Command) {.async.} =
   var message = newMessage(command.message.chat.id, answer)
   message.replyToMessageId = command.message.messageId
   message.parseMode = "markdown"
-  discard bot.send(message)-
\ No newline at end of file
+  discard await bot.send(message)