nim-censor-bot/src/nim_censor_bot.nim

65 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(" ")
var fileid = ""
let filehash = generate_hash()
if response.document.isSome:
var censordoc = NewCensoredData("document", filehash, response.document.get.fileId, "")
with dbConn:
insert censordoc
if response.video.isSome:
let filehash = generate_hash()
var censorvid = NewCensoredData("video", filehash, response.video.get.fileId, "")
with dbConn:
insert censorvid
if response.videoNote.isSome:
let filehash = generate_hash()
var censorvidnote = NewCensoredData("videonote", filehash, response.videoNote.get.fileId, "")
with dbConn:
insert censorvidnote
if response.animation.isSome:
let filehash = generate_hash()
var censoranimation = NewCensoredData("animation", filehash, response.animation.get.fileId, "")
with dbConn:
insert censoranimation
if response.photo.isSome:
let filehash = generate_hash()
var censorphoto = NewCensoredData("photo", filehash, response.photo.get[0].fileId, "")
with dbConn:
insert censorphoto
let bot = newTeleBot(API_KEY)
bot.onUpdate(updateHandler)
bot.poll(timeout=300)