ctucx.git: ctucxbot

[nimlang] A telegram bot

commit 38cb9d322d555fae75305f85ecb2d18f468658c8
parent 7166845fe2af7ce5f992e239c0777ced3ed28b17
Author: Isabelle K <hi@f2k1.de>
Date: Sat, 15 Aug 2020 21:49:18 +0200

new command: /ds100
2 files changed, 57 insertions(+), 1 deletion(-)
A
src/cmd/ds100.nim
|
55
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
M
src/ctucxbot.nim
|
3
++-
diff --git a/src/cmd/ds100.nim b/src/cmd/ds100.nim
@@ -0,0 +1,54 @@
+import asyncdispatch, telebot, strutils, osproc, json, os, httpClient
+
+proc ds100Command* (bot: Telebot, command: Command): Future[bool] {.async.} =
+  var rückgabewert = ""
+  var location = false
+  var longitude = 0.0
+  var latitude = 0.0
+  let anfrage = toUpperAscii(command.params)
+  if anfrage != "":
+    let daten = parseJson(readFile(getEnv("ASSETS_PATH") & "/db/ds100.json"))
+
+    if daten.hasKey(anfrage):
+      let id = daten[anfrage]["ibnr"].getStr
+      let stationsDaten = parseJson(readFile(getEnv("ASSETS_PATH") & "/db/stations/" & id & ".json"))
+
+      rückgabewert = "Name: " & stationsDaten["name"].getStr & "\n"
+      rückgabewert &= "IBNR:   " & stationsDaten["id"].getStr & "\n"
+      rückgabewert &= "Kategorie: " & $stationsDaten["category"].getInt & "\n"
+      if stationsDaten["hasDBLounge"].getBool: 
+        rückgabewert &= "Mit DB Lounge" & "\n"
+      if stationsDaten["hasSteplessAccess"].getStr == "yes": 
+        rückgabewert &= "Mit stufenfreiem Zugang" & "\n"
+      if stationsDaten["hasSteplessAccess"].getStr == "partial": 
+        rückgabewert &= "Mit teilweise stufenfreiem Zugang" & "\n"
+      if stationsDaten["hasPublicFacilities"].getBool: 
+        rückgabewert &= "Mit Bahnhofstoiletten" & "\n"
+      longitude = stationsDaten["longitude"].getFloat
+      latitude = stationsDaten["latitude"].getFloat
+      location = true
+
+# This Code Works, but only at some Stations. Example: AK is fine, BLO crashes.
+# Exception message: Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 
+#
+#      var httpClient = newAsyncHttpClient()
+
+#      if daten[anfrage].hasKey("RailwayMapId"):
+#        # Debug
+#        echo daten[anfrage]["RailwayMapId"].getInt
+#        let photojson = parseJson(await httpClient.getContent("https://api.railway-stations.org/de/stations/" & $daten[anfrage]["RailwayMapId"].getInt))
+#        if photojson.hasKey("photoUrl"):
+#          let fotourl = replace($photojson["photoUrl"], "\"", "")
+#          # Debug
+#          echo fotourl
+#          rückgabewert &= $fotourl
+
+    else:
+      rückgabewert = "hab ich nicht"
+  else: 
+    rückgabewert = "leere anfrage"
+
+  discard await bot.sendMessage(command.message.chat.id, rückgabewert, replyToMessageId = command.message.messageId, parseMode = "markdown")
+
+  if location:
+    discard await bot.sendLocation(command.message.chat.id, longitude=longitude, latitude=latitude)+
\ No newline at end of file
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -1,6 +1,6 @@
 import asyncdispatch, telebot, posix, os 
 include updateHandler
-import cmd/[animalpics, daysuntilcongress, debuginfo, help, stats, unixtime, utc, uptime, yesorno, whoami, sarcasm, httpstatuscode]
+import cmd/[animalpics, daysuntilcongress, debuginfo, ds100, help, httpstatuscode, sarcasm, stats, unixtime, utc, uptime, whoami, yesorno]
 
 proc CtrlCHook() {.noconv.} =
   echo "Ctrl+C fired! \nStopping bot now!"

@@ -43,6 +43,7 @@ proc main =  # for gcsafe
     bot.onCommand("whoami",               whoamiCommand)
     bot.onCommand("s",                    sarcasmCommand)
     bot.onCommand("httpstatuscode",       httpstatuscodeCommand)
+    bot.onCommand("ds100",                ds100Command)
     bot.poll(timeout=300)
 
   else: