Adapt for new tg api changes

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2024-01-10 14:31:07 +05:30
parent f181d97247
commit 42a85588fe
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5
3 changed files with 35 additions and 34 deletions

View file

@ -1,3 +1,5 @@
-d:ssl -d:ssl
--warning:GCUnsafe2:off --warning:GCUnsafe2:off
--threadAnalysis:off --threadAnalysis:off
--deepcopy:on
--gc:orc

View file

@ -11,5 +11,5 @@ bin = @["nim_censor_bot"]
# Dependencies # Dependencies
requires "nim >= 2.0.0" requires "nim >= 2.0.0"
requires "telebot >= 2023.12.30" requires "telebot >= 2024.01.10"
requires "norm >= 2.8.1" requires "norm >= 2.8.1"

View file

@ -8,7 +8,6 @@ import telebot
import std/[asyncdispatch, import std/[asyncdispatch,
logging, logging,
options,
strutils, strutils,
random, random,
os, os,
@ -126,7 +125,7 @@ proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
var inputmedia = newSeq[InputMediaPhoto]() var inputmedia = newSeq[InputMediaPhoto]()
for i in TempData: for i in TempData:
if i.caption != "": if i.caption != "":
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid, caption: some(i.caption))) inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid, caption: i.caption))
else: else:
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid)) inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid))
discard await b.sendMediaGroup($c.message.chat.id, media=inputmedia) discard await b.sendMediaGroup($c.message.chat.id, media=inputmedia)
@ -160,7 +159,7 @@ proc sourceHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
"\n\nYou are welcome to selfhost your own instance of this bot" & "\n\nYou are welcome to selfhost your own instance of this bot" &
"\n\nThe source code is [here](https://git.baalajimaestro.me/baalajimaestro/nim-censor-bot)", "\n\nThe source code is [here](https://git.baalajimaestro.me/baalajimaestro/nim-censor-bot)",
parseMode="Markdown", parseMode="Markdown",
disableWebPagePreview = true) linkPreviewOptions = LinkPreviewOptions(isDisabled: true))
# UnBan Handler # UnBan Handler
proc unbanHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} = proc unbanHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
@ -197,7 +196,7 @@ proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} =
if not db.exists(CensoredData, "fhash = ?",u.query): if not db.exists(CensoredData, "fhash = ?",u.query):
res.title = "Media Not Found" res.title = "Media Not Found"
res.inputMessageContent = InputTextMessageContent( res.inputMessageContent = InputTextMessageContent(
"Media does not exist on database, ask the sender to censor this again!").some "Media does not exist on database, ask the sender to censor this again!")
else: else:
db.select(TempData, "fhash = ?", u.query) db.select(TempData, "fhash = ?", u.query)
if len(TempData) > 1: if len(TempData) > 1:
@ -212,19 +211,19 @@ proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} =
b.username & "?start=" & b.username & "?start=" &
u.query & u.query &
")", ")",
parseMode: some("Markdown"), parseMode: "Markdown",
disableWebPagePreview: some(true)).some linkPreviewOptions: LinkPreviewOptions(isDisabled: true))
else: else:
res.title = "Waiting for File Hash" res.title = "Waiting for File Hash"
res.inputMessageContent = InputTextMessageContent( res.inputMessageContent = InputTextMessageContent(
"Provide the filehash on inline query").some "Provide the filehash on inline query")
results.add(res) results.add(res)
discard await b.answerInlineQuery(u.id, results) discard await b.answerInlineQuery(u.id, results)
# 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.} =
if u.message.isSome: if not u.message.isNil:
let response = u.message.get let response = u.message
# Refresh rate-limits # Refresh rate-limits
ManageRateLimit() ManageRateLimit()
withDb(connPool): withDb(connPool):
@ -236,40 +235,40 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
RateLimiter[response.chat.id].insert(epochTime()) RateLimiter[response.chat.id].insert(epochTime())
else: else:
RateLimiter[response.chat.id] = @[epochTime()] RateLimiter[response.chat.id] = @[epochTime()]
if not response.text.isSome: if response.text.len == 0:
var fileid = "" var fileid = ""
var ftype = "" var ftype = ""
var fcaption = "" var fcaption = ""
if response.caption.isSome: if response.caption.len > 0:
fcaption = response.caption.get fcaption = response.caption
if response.document.isSome: if response.document != nil:
fileid = response.document.get.fileId fileid = response.document.fileId
ftype = "document" ftype = "document"
elif response.video.isSome: elif response.video != nil:
fileid = response.video.get.fileId fileid = response.video.fileId
ftype = "video" ftype = "video"
elif response.videoNote.isSome: elif response.videoNote != nil:
fileid = response.videoNote.get.fileId fileid = response.videoNote.fileId
ftype = "videoNote" ftype = "videoNote"
elif response.animation.isSome: elif response.animation != nil:
fileid = response.animation.get.fileId fileid = response.animation.fileId
ftype = "animation" ftype = "animation"
elif response.photo.isSome: elif response.photo.len > 0:
fileid = response.photo.get[0].fileId fileid = response.photo[0].fileId
ftype = "photo" ftype = "photo"
elif response.sticker.isSome: elif response.sticker != nil:
fileid = response.sticker.get.fileId fileid = response.sticker.fileId
ftype = "sticker" ftype = "sticker"
# Workaround for groupmedia using hashtables # Workaround for groupmedia using hashtables
# Telegram is sending multiple updates, instead of 1, so just workaround it # Telegram is sending multiple updates, instead of 1, so just workaround it
if response.mediaGroupId.isSome: if response.mediaGroupId.len > 0:
if parseInt(response.mediaGroupId.get) notin GroupMedia.keys().toSeq(): if parseInt(response.mediaGroupId) notin GroupMedia.keys().toSeq():
let filehash = generate_hash() let filehash = generate_hash()
GroupMedia[parseInt(response.mediaGroupId.get)] = filehash GroupMedia[parseInt(response.mediaGroupId)] = filehash
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash)) var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton]) let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id, discard await b.sendMessage(response.chat.id,
"*NSFW " & "*NSFW " &
@ -280,10 +279,10 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
filehash & filehash &
")", ")",
replyMarkup = replymark, replyMarkup = replymark,
disableWebPagePreview=true, linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
parseMode="Markdown") parseMode="Markdown")
else: else:
let filehash = GroupMedia[parseInt(response.mediaGroupId.get)] let filehash = GroupMedia[parseInt(response.mediaGroupId)]
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
@ -293,7 +292,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: some(filehash)) var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton]) let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id, discard await b.sendMessage(response.chat.id,
"*NSFW " & "*NSFW " &
@ -304,7 +303,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
filehash & filehash &
")", ")",
replyMarkup = replymark, replyMarkup = replymark,
disableWebPagePreview=true, linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
parseMode="Markdown") parseMode="Markdown")
OldDataCleanup() OldDataCleanup()