ctucx.git: ctucxbot

[nimlang] A telegram bot

commit c0963dc53aa6d43a4af8865072961934a2891e06
parent 35530ea9504ef802d51a7a7e108e360b9dc9e288
Author: ctucx <c@ctu.cx>
Date: Wed, 22 Jan 2020 21:25:38 +0100

new command: invitelinks
2 files changed, 30 insertions(+), 2 deletions(-)
A
src/cmd/invitelinks.nim
|
27
+++++++++++++++++++++++++++
M
src/ctucxbot.nim
|
5
+++--
diff --git a/src/cmd/invitelinks.nim b/src/cmd/invitelinks.nim
@@ -0,0 +1,26 @@
+import asyncdispatch, telebot, strutils, options, os, json
+
+proc invitelinksCommand(bot: Telebot, command: Command) {.async.} =
+  var text = ""
+  var links = ""
+
+  if command.message.chat.kind == "private":
+    let json = parseFile(getCurrentDir() & "/data/invitelinks.json")
+
+    for item in items(json):
+      if not item["allowedUsers"].contains(%command.message.fromUser.get.username.get): continue
+
+      let inviteLink = await bot.exportChatInviteLink(item["chatId"].getStr)
+      links &= item["name"].getStr & ": " & inviteLink & "\n"
+
+    if links != "":
+      text = links
+    else:
+      text = "I have no groups for you, sorry." 
+
+  else:
+    text = "This is not a private chat..."
+
+  var message = newMessage(command.message.chat.id, text)
+  message.replyToMessageId = command.message.messageId
+  discard bot.send(message)+
\ No newline at end of file
diff --git a/src/ctucxbot.nim b/src/ctucxbot.nim
@@ -1,5 +1,5 @@
-import asyncdispatch, telebot, options, strutils, random, os, re, unicode
-include cmd/[animalpics, daysuntilcongress, debuginfo, help, stats, unixtime, utc, uptime, yesorno, whoami]
+import asyncdispatch, telebot, options, strutils, random, os, re, unicode, logging
+include cmd/[animalpics, daysuntilcongress, debuginfo, help, stats, unixtime, utc, uptime, yesorno, whoami, invitelinks]
 
 proc ctrlcHandler() {.noconv.} =
   quit()

@@ -75,6 +75,7 @@ if getEnv("API_KEY") != "":
   bot.onCommand("uptime", uptimeCommand)
   bot.onCommand("yesorno", yesornoCommand)
   bot.onCommand("whoami", whoamiCommand)
+  bot.onCommand("invitelinks", invitelinksCommand)
   bot.poll(timeout=300)
 
 else: