2022-04-09 14:47:19 +00:00
|
|
|
import telebot, std/[asyncdispatch, logging, options, strutils, random, with, os], norm/[model, sqlite]
|
2022-04-07 17:41:36 +00:00
|
|
|
|
|
|
|
type
|
2022-04-09 15:18:41 +00:00
|
|
|
CensoredData* = ref object of Model
|
2022-04-07 17:41:36 +00:00
|
|
|
ftype*: string
|
2022-04-09 14:30:05 +00:00
|
|
|
fhash*: string
|
2022-04-07 17:41:36 +00:00
|
|
|
fileid*: string
|
|
|
|
caption*: string
|
|
|
|
|
2022-04-09 15:18:41 +00:00
|
|
|
func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""):
|
|
|
|
CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption)
|
2022-04-07 17:41:36 +00:00
|
|
|
|
|
|
|
let dbConn* = sqlite.open("censordata.db", "", "", "")
|
|
|
|
|
2022-04-09 15:18:41 +00:00
|
|
|
dbConn.createTables(NewCensoredData())
|
2022-04-07 17:41:36 +00:00
|
|
|
|
2022-04-07 10:51:02 +00:00
|
|
|
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
|
|
|
|
addHandler(L)
|
|
|
|
|
2022-04-07 11:45:56 +00:00
|
|
|
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
|
|
|
|
|
2022-04-09 14:47:19 +00:00
|
|
|
const API_KEY = getEnv("TELEGRAM_TOKEN")
|
2022-04-07 10:51:02 +00:00
|
|
|
|
2022-04-09 14:26:17 +00:00
|
|
|
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
2022-04-07 10:51:02 +00:00
|
|
|
var response = u.message.get
|
2022-04-09 14:26:17 +00:00
|
|
|
if response.document.isSome:
|
2022-04-09 14:30:05 +00:00
|
|
|
let filehash = generate_hash()
|
2022-04-09 15:18:41 +00:00
|
|
|
var censordoc = NewCensoredData("document", filehash, response.document.get.fileId, "")
|
2022-04-09 14:26:17 +00:00
|
|
|
with dbConn:
|
|
|
|
insert censordoc
|
|
|
|
if response.video.isSome:
|
2022-04-09 14:30:05 +00:00
|
|
|
let filehash = generate_hash()
|
2022-04-09 15:18:41 +00:00
|
|
|
var censorvid = NewCensoredData("video", filehash, response.video.get.fileId, "")
|
2022-04-09 14:26:17 +00:00
|
|
|
with dbConn:
|
|
|
|
insert censorvid
|
|
|
|
if response.videoNote.isSome:
|
2022-04-09 14:30:05 +00:00
|
|
|
let filehash = generate_hash()
|
2022-04-09 15:18:41 +00:00
|
|
|
var censorvidnote = NewCensoredData("videonote", filehash, response.videoNote.get.fileId, "")
|
2022-04-09 14:26:17 +00:00
|
|
|
with dbConn:
|
|
|
|
insert censorvidnote
|
|
|
|
if response.animation.isSome:
|
2022-04-09 14:30:05 +00:00
|
|
|
let filehash = generate_hash()
|
2022-04-09 15:18:41 +00:00
|
|
|
var censoranimation = NewCensoredData("animation", filehash, response.animation.get.fileId, "")
|
2022-04-09 14:26:17 +00:00
|
|
|
with dbConn:
|
|
|
|
insert censoranimation
|
2022-04-07 10:51:02 +00:00
|
|
|
if response.photo.isSome:
|
2022-04-09 14:30:05 +00:00
|
|
|
let filehash = generate_hash()
|
2022-04-09 15:18:41 +00:00
|
|
|
var censorphoto = NewCensoredData("photo", filehash, response.photo.get[0].fileId, "")
|
2022-04-09 14:26:17 +00:00
|
|
|
with dbConn:
|
|
|
|
insert censorphoto
|
2022-04-07 10:51:02 +00:00
|
|
|
|
|
|
|
let bot = newTeleBot(API_KEY)
|
|
|
|
bot.onUpdate(updateHandler)
|
|
|
|
bot.poll(timeout=300)
|