65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"govd/bot/core"
|
|
"govd/database"
|
|
extractors "govd/ext"
|
|
|
|
"github.com/PaulSonOfLars/gotgbot/v2"
|
|
"github.com/PaulSonOfLars/gotgbot/v2/ext"
|
|
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message"
|
|
)
|
|
|
|
func URLHandler(bot *gotgbot.Bot, ctx *ext.Context) error {
|
|
messageURL := getMessageURL(ctx.EffectiveMessage)
|
|
if messageURL == "" {
|
|
return nil
|
|
}
|
|
dlCtx, err := extractors.CtxByURL(messageURL)
|
|
if err != nil {
|
|
core.HandleErrorMessage(
|
|
bot, ctx, err)
|
|
return nil
|
|
}
|
|
if dlCtx == nil || dlCtx.Extractor == nil {
|
|
return nil
|
|
}
|
|
userID := ctx.EffectiveMessage.From.Id
|
|
if ctx.EffectiveMessage.Chat.Type != "private" {
|
|
settings, err := database.GetGroupSettings(ctx.EffectiveMessage.Chat.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dlCtx.GroupSettings = settings
|
|
}
|
|
if userID != 1087968824 {
|
|
// groupAnonymousBot
|
|
_, err = database.GetUser(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = core.HandleDownloadRequest(bot, ctx, dlCtx)
|
|
if err != nil {
|
|
core.HandleErrorMessage(
|
|
bot, ctx, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func URLFilter(msg *gotgbot.Message) bool {
|
|
return message.Text(msg) && !message.Command(msg) && containsURL(msg)
|
|
}
|
|
|
|
func containsURL(msg *gotgbot.Message) bool {
|
|
return message.Entity("url")(msg)
|
|
}
|
|
|
|
func getMessageURL(msg *gotgbot.Message) string {
|
|
for _, entity := range msg.Entities {
|
|
if entity.Type == "url" {
|
|
return msg.Text[entity.Offset : entity.Offset+entity.Length]
|
|
}
|
|
}
|
|
return ""
|
|
}
|