Dont handle non-message updates
Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
parent
5eac4c95d5
commit
bb22849959
1 changed files with 76 additions and 74 deletions
|
@ -6,11 +6,12 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import telebot
|
import telebot
|
||||||
import std/[asyncdispatch, logging, options, strutils, random, with, os, tables, times, sequtils]
|
import std/[asyncdispatch, logging, options, strutils, random, with, os, tables, times, sequtils, json]
|
||||||
import norm/[model, sqlite]
|
import norm/[model, sqlite]
|
||||||
|
from cgi import encodeUrl
|
||||||
|
|
||||||
# Logging Level
|
# Logging Level
|
||||||
var L = newConsoleLogger(levelThreshold=lvlError, fmtStr="$levelname, [$time] ")
|
var L = newConsoleLogger(levelThreshold=lvlDebug, fmtStr="$levelname, [$time] ")
|
||||||
addHandler(L)
|
addHandler(L)
|
||||||
|
|
||||||
# Custom Types Defined to handle tables on norm
|
# Custom Types Defined to handle tables on norm
|
||||||
|
@ -148,84 +149,85 @@ proc banHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
|
||||||
|
|
||||||
# Main update handler
|
# Main update handler
|
||||||
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
||||||
let response = u.message.get
|
if u.message.isSome:
|
||||||
# Refresh rate-limits
|
let response = u.message.get
|
||||||
ManageRateLimit()
|
# Refresh rate-limits
|
||||||
# Dont bother about rate-limited/banned users
|
ManageRateLimit()
|
||||||
if not dbConn.exists(BannedUsers, "userid = ?", response.chat.id):
|
# Dont bother about rate-limited/banned users
|
||||||
# Update rate-limit counter
|
if not dbConn.exists(BannedUsers, "userid = ?", response.chat.id):
|
||||||
if RateLimiter.contains(response.chat.id):
|
# Update rate-limit counter
|
||||||
RateLimiter[response.chat.id].insert(epochTime())
|
if RateLimiter.contains(response.chat.id):
|
||||||
else:
|
RateLimiter[response.chat.id].insert(epochTime())
|
||||||
RateLimiter[response.chat.id] = @[epochTime()]
|
else:
|
||||||
if not response.text.isSome:
|
RateLimiter[response.chat.id] = @[epochTime()]
|
||||||
var fileid = ""
|
if not response.text.isSome:
|
||||||
var ftype = ""
|
var fileid = ""
|
||||||
var fcaption = ""
|
var ftype = ""
|
||||||
if response.caption.isSome:
|
var fcaption = ""
|
||||||
fcaption = response.caption.get
|
if response.caption.isSome:
|
||||||
if response.document.isSome:
|
fcaption = response.caption.get
|
||||||
fileid = response.document.get.fileId
|
if response.document.isSome:
|
||||||
ftype = "document"
|
fileid = response.document.get.fileId
|
||||||
elif response.video.isSome:
|
ftype = "document"
|
||||||
fileid = response.video.get.fileId
|
elif response.video.isSome:
|
||||||
ftype = "video"
|
fileid = response.video.get.fileId
|
||||||
elif response.videoNote.isSome:
|
ftype = "video"
|
||||||
fileid = response.videoNote.get.fileId
|
elif response.videoNote.isSome:
|
||||||
ftype = "videoNote"
|
fileid = response.videoNote.get.fileId
|
||||||
elif response.animation.isSome:
|
ftype = "videoNote"
|
||||||
fileid = response.animation.get.fileId
|
elif response.animation.isSome:
|
||||||
ftype = "animation"
|
fileid = response.animation.get.fileId
|
||||||
elif response.photo.isSome:
|
ftype = "animation"
|
||||||
fileid = response.photo.get[0].fileId
|
elif response.photo.isSome:
|
||||||
ftype = "photo"
|
fileid = response.photo.get[0].fileId
|
||||||
elif response.sticker.isSome:
|
ftype = "photo"
|
||||||
fileid = response.sticker.get.fileId
|
elif response.sticker.isSome:
|
||||||
ftype = "sticker"
|
fileid = response.sticker.get.fileId
|
||||||
# Workaround for groupmedia using hashtables
|
ftype = "sticker"
|
||||||
# Telegram is sending multiple updates, instead of 1, so just workaround it
|
# Workaround for groupmedia using hashtables
|
||||||
if response.mediaGroupId.isSome:
|
# Telegram is sending multiple updates, instead of 1, so just workaround it
|
||||||
if parseInt(response.mediaGroupId.get) notin GroupMedia.keys().toSeq():
|
if response.mediaGroupId.isSome:
|
||||||
|
if parseInt(response.mediaGroupId.get) notin GroupMedia.keys().toSeq():
|
||||||
|
let filehash = generate_hash()
|
||||||
|
GroupMedia[parseInt(response.mediaGroupId.get)] = filehash
|
||||||
|
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
||||||
|
with dbConn:
|
||||||
|
insert CensoredRow
|
||||||
|
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash))
|
||||||
|
let replymark = newInlineKeyboardMarkup(@[replybutton])
|
||||||
|
discard await b.sendMessage(response.chat.id, "*Censored " & capitalizeAscii(ftype) & "*\n\n[Tap to View](tg://resolve?domain=" & b.username & "&start=" & filehash & ")", replyMarkup = replymark, parseMode="Markdown")
|
||||||
|
else:
|
||||||
|
let filehash = GroupMedia[parseInt(response.mediaGroupId.get)]
|
||||||
|
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
||||||
|
with dbConn:
|
||||||
|
insert CensoredRow
|
||||||
|
|
||||||
|
else:
|
||||||
let filehash = generate_hash()
|
let filehash = generate_hash()
|
||||||
GroupMedia[parseInt(response.mediaGroupId.get)] = filehash
|
|
||||||
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
||||||
with dbConn:
|
with dbConn:
|
||||||
insert CensoredRow
|
insert CensoredRow
|
||||||
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash))
|
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash))
|
||||||
let replymark = newInlineKeyboardMarkup(@[replybutton])
|
let replymark = newInlineKeyboardMarkup(@[replybutton])
|
||||||
discard await b.sendMessage(response.chat.id, "*Censored " & capitalizeAscii(ftype) & "*", replyMarkup = replymark, parseMode="Markdown")
|
discard await b.sendMessage(response.chat.id, "*Censored " & capitalizeAscii(ftype) & "*\n\n[Tap to View](tg://resolve?domain=" & b.username & "&start=" & filehash & ")", replyMarkup = replymark, parseMode="Markdown")
|
||||||
else:
|
OldDataCleanup()
|
||||||
let filehash = GroupMedia[parseInt(response.mediaGroupId.get)]
|
|
||||||
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
|
||||||
with dbConn:
|
|
||||||
insert CensoredRow
|
|
||||||
|
|
||||||
else:
|
when isMainModule:
|
||||||
let filehash = generate_hash()
|
let bot = newTeleBot(getEnv("TELEGRAM_TOKEN"))
|
||||||
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
|
echo "*********************"
|
||||||
with dbConn:
|
echo "CensorBot is running!"
|
||||||
insert CensoredRow
|
echo "*********************"
|
||||||
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash))
|
|
||||||
let replymark = newInlineKeyboardMarkup(@[replybutton])
|
|
||||||
discard await b.sendMessage(response.chat.id, "*Censored " & capitalizeAscii(ftype) & "*", replyMarkup = replymark, parseMode="Markdown")
|
|
||||||
OldDataCleanup()
|
|
||||||
|
|
||||||
# Start the bot
|
var commands = @[
|
||||||
let bot = newTeleBot(getEnv("TELEGRAM_TOKEN"))
|
BotCommand(command: "start" , description: "Start the bot!")
|
||||||
echo "*********************"
|
]
|
||||||
echo "CensorBot is running!"
|
|
||||||
echo "*********************"
|
|
||||||
|
|
||||||
var commands = @[
|
discard waitFor bot.setMyCommands(commands)
|
||||||
BotCommand(command: "start" , description: "Start the bot!")
|
bot.onUpdate(updateHandler)
|
||||||
]
|
bot.onCommand("start", startHandler)
|
||||||
|
bot.onCommand("ban", banHandler)
|
||||||
discard waitFor bot.setMyCommands(commands)
|
bot.onCommand("unban", unbanHandler)
|
||||||
bot.onUpdate(updateHandler)
|
if getEnv("HOOK_DOMAIN") != "":
|
||||||
bot.onCommand("start", startHandler)
|
bot.startWebhook(getEnv("HOOK_SECRET"), getEnv("HOOK_DOMAIN") & "/" & getEnv("HOOK_SECRET"))
|
||||||
bot.onCommand("ban", banHandler)
|
else:
|
||||||
bot.onCommand("unban", unbanHandler)
|
bot.poll(timeout=300)
|
||||||
if getEnv("HOOK_DOMAIN") != "":
|
|
||||||
bot.startWebhook(getEnv("HOOK_SECRET"), getEnv("HOOK_DOMAIN") & "/" & getEnv("HOOK_SECRET"))
|
|
||||||
else:
|
|
||||||
bot.poll(timeout=300)
|
|
||||||
|
|
Loading…
Reference in a new issue