nim-censor-bot/src/nim_censor_bot.nim

71 lines
2.1 KiB
Nim
Raw Normal View History

import telebot, std/[asyncdispatch, logging, options, strutils, random, with, os], norm/[model, sqlite]
type
CensoredData* = ref object of Model
ftype*: string
fhash*: string
fileid*: string
caption*: string
func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""):
CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption)
let dbConn* = sqlite.open("censordata.db", "", "", "")
dbConn.createTables(NewCensoredData())
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
addHandler(L)
proc generate_hash(): string=
result = newString(7)
const charset = {'a' .. 'z', 'A' .. 'Z', '0' .. '9'}
for i in 0..6:
result[i] = sample(charset)
return result
const API_KEY = getEnv("TELEGRAM_TOKEN")
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
let response = u.message.get
if response.text.isSome:
let message = response.text.get
if message.contains("/start"):
let deeplink = message.split(" ")
else:
var fileid = ""
var ftype = ""
var fcaption = ""
if response.caption.isSome:
fcaption = response.caption.get
if response.document.isSome:
fileid = response.document.get.fileId
ftype = "document"
if response.video.isSome:
fileid = response.video.get.fileId
ftype = "video"
if response.videoNote.isSome:
fileid = response.videoNote.get.fileId
ftype = "videonote"
if response.animation.isSome:
fileid = response.animation.get.fileId
ftype = "animation"
if response.photo.isSome:
fileid = response.photo.get[0].fileId
ftype = "photo"
if response.mediaGroupId.isSome:
fileid = response.mediaGroupId.get
let filehash = generate_hash()
var CensoredRow = NewCensoredData(ftype, filehash, fileid, fcaption)
with dbConn:
insert CensoredRow
discard b.sendMessage(response.chat.id, "Censored --> " & "tg://resolve?domain=botbotbotnotabot%26start=" & filehash)
let bot = newTeleBot(API_KEY)
bot.onUpdate(updateHandler)
bot.poll(timeout=300)