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
--warning:GCUnsafe2:off
--threadAnalysis:off
--deepcopy:on
--gc:orc

View file

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

View file

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