Skip to content

Commit

Permalink
Move telegram helpers outside of utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Jan 24, 2024
1 parent 51574ba commit f69e4ea
Show file tree
Hide file tree
Showing 41 changed files with 279 additions and 238 deletions.
3 changes: 2 additions & 1 deletion bot/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)
Expand Down Expand Up @@ -367,7 +368,7 @@ func onMessage(msg *gotgbot.Message) string {
),
)
} else if msg.Photo != nil && len(msg.Photo) > 0 { // Photo: https://core.telegram.org/bots/api#photosize
bestResolutionPhoto := utils.GetBestResolution(msg.Photo)
bestResolutionPhoto := tgUtils.GetBestResolution(msg.Photo)
sb.WriteString(
fmt.Sprintf(
"%s[Foto: %dx%d px]%s ",
Expand Down
41 changes: 21 additions & 20 deletions bot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/rs/xid"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error {
isEdited := msg.EditDate != 0

isAllowed := p.allowService.IsUserAllowed(ctx.EffectiveUser)
if utils.FromGroup(msg) && !isAllowed {
if tgUtils.FromGroup(msg) && !isAllowed {
isAllowed = p.allowService.IsChatAllowed(ctx.EffectiveChat)
}

Expand All @@ -86,7 +87,7 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error {
var err error

if !isEdited {
if utils.IsPrivate(msg) {
if tgUtils.IsPrivate(msg) {
err = p.userService.Create(ctx.EffectiveUser)
} else {
err = p.chatsUsersService.Create(ctx.EffectiveChat, ctx.EffectiveUser)
Expand Down Expand Up @@ -115,7 +116,7 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error {
continue
}

if !utils.FromGroup(msg) && handler.GroupOnly {
if !tgUtils.FromGroup(msg) && handler.GroupOnly {
continue
}

Expand All @@ -132,36 +133,36 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error {
namedMatches[command.SubexpNames()[i]] = name
}
}
case utils.MessageTrigger:
case tgUtils.MessageTrigger:
switch {
// More to be added when needed
case msg.Document != nil:
matched = command == utils.DocumentMsg
matched = command == tgUtils.DocumentMsg
case msg.Photo != nil && len(msg.Photo) > 0:
matched = command == utils.PhotoMsg
matched = command == tgUtils.PhotoMsg
case msg.Location != nil:
matched = command == utils.LocationMsg
matched = command == tgUtils.LocationMsg
case msg.Venue != nil:
matched = command == utils.VenueMsg
matched = command == tgUtils.VenueMsg
case msg.Voice != nil:
matched = command == utils.VoiceMsg
matched = command == tgUtils.VoiceMsg
}

if !matched && utils.ContainsMedia(msg) {
matched = command == utils.AnyMedia
if !matched && tgUtils.ContainsMedia(msg) {
matched = command == tgUtils.AnyMedia
}

if !matched {
matched = command == utils.AnyMsg
matched = command == tgUtils.AnyMsg
}
case utils.EntityType:
case tgUtils.EntityType:
entities := msg.Entities
if entities == nil {
entities = msg.CaptionEntities
}

for _, entity := range entities {
matched = utils.EntityType(entity.Type) == command
matched = tgUtils.EntityType(entity.Type) == command
if matched {
break
}
Expand All @@ -178,12 +179,12 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error {
continue
}

if utils.FromGroup(msg) && p.managerService.IsPluginDisabledForChat(ctx.EffectiveChat, plg.Name()) {
if tgUtils.FromGroup(msg) && p.managerService.IsPluginDisabledForChat(ctx.EffectiveChat, plg.Name()) {
log.Printf("Plugin %s is disabled for this chat", plg.Name())
continue
}

if handler.AdminOnly && !utils.IsAdmin(ctx.EffectiveUser) {
if handler.AdminOnly && !tgUtils.IsAdmin(ctx.EffectiveUser) {
log.Print("User is not an admin.")
continue
}
Expand Down Expand Up @@ -238,7 +239,7 @@ func (p *Processor) onCallback(b *gotgbot.Bot, ctx *ext.Context) error {
}

isAllowed := p.allowService.IsUserAllowed(&ctx.CallbackQuery.From)
if msg != nil && utils.FromGroup(msg) && !isAllowed {
if msg != nil && tgUtils.FromGroup(msg) && !isAllowed {
isAllowed = p.allowService.IsChatAllowed(ctx.EffectiveChat)
}

Expand Down Expand Up @@ -278,7 +279,7 @@ func (p *Processor) onCallback(b *gotgbot.Bot, ctx *ext.Context) error {
return err
}

if utils.FromGroup(msg) && p.managerService.IsPluginDisabledForChat(ctx.EffectiveChat, plg.Name()) {
if tgUtils.FromGroup(msg) && p.managerService.IsPluginDisabledForChat(ctx.EffectiveChat, plg.Name()) {
log.Printf("Plugin %s is disabled for this chat", plg.Name())
_, err := ctx.CallbackQuery.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Dieser Befehl ist nicht verfügbar.",
Expand All @@ -287,7 +288,7 @@ func (p *Processor) onCallback(b *gotgbot.Bot, ctx *ext.Context) error {
return err
}

if handler.AdminOnly && !utils.IsAdmin(ctx.EffectiveUser) {
if handler.AdminOnly && !tgUtils.IsAdmin(ctx.EffectiveUser) {
log.Print("User is not an admin.")
_, err := ctx.CallbackQuery.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Du bist kein Bot-Administrator.",
Expand Down Expand Up @@ -398,7 +399,7 @@ func (p *Processor) onInlineQuery(b *gotgbot.Bot, ctx *ext.Context) error {
return err
}

if handler.AdminOnly && !utils.IsAdmin(ctx.EffectiveUser) {
if handler.AdminOnly && !tgUtils.IsAdmin(ctx.EffectiveUser) {
log.Print("User is not an admin.")
_, err := ctx.InlineQuery.Answer(b, nil, &gotgbot.AnswerInlineQueryOpts{
CacheTime: utils.InlineQueryFailureCacheTime,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"os/signal"
"syscall"

"github.com/Brawl345/gobot/utils"
_ "github.com/joho/godotenv/autoload"

"github.com/Brawl345/gobot/bot"
"github.com/Brawl345/gobot/logger"
"github.com/Brawl345/gobot/model/sql"
"github.com/Brawl345/gobot/utils"
)

var log = logger.New("main")
Expand Down
7 changes: 4 additions & 3 deletions model/sql/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package sql

import (
"errors"

"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"

"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/utils"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func NewAllowService(chatService model.ChatService, userService model.UserServic
}

func (service *allowService) IsUserAllowed(user *gotgbot.User) bool {
if utils.IsAdmin(user) {
if tgUtils.IsAdmin(user) {
return true
}

Expand All @@ -58,7 +59,7 @@ func (service *allowService) AllowUser(user *gotgbot.User) error {
}

func (service *allowService) DenyUser(user *gotgbot.User) error {
if utils.IsAdmin(user) {
if tgUtils.IsAdmin(user) {
return errors.New("cannot deny admin")
}
err := service.userService.Deny(user)
Expand Down
5 changes: 3 additions & 2 deletions model/sql/chats_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"database/sql"
"errors"

"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"

"github.com/Brawl345/gobot/logger"
"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/utils"
"github.com/jmoiron/sqlx"
)

Expand Down Expand Up @@ -131,7 +132,7 @@ func (db *chatsUsersService) insertRelationship(tx *sqlx.Tx, chatId int64, userI
}

func (db *chatsUsersService) IsAllowed(chat *gotgbot.Chat, user *gotgbot.User) bool {
if utils.IsAdmin(user) {
if tgUtils.IsAdmin(user) {
return true
}

Expand Down
11 changes: 6 additions & 5 deletions plugin/afk/afk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
tgUtils "github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/rs/xid"
)
Expand Down Expand Up @@ -62,12 +63,12 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler {
GroupOnly: true,
},
&plugin.CommandHandler{
Trigger: utils.AnyMsg,
Trigger: tgUtils.AnyMsg,
HandlerFunc: p.checkAFK,
GroupOnly: true,
},
&plugin.CommandHandler{
Trigger: utils.EntityTypeMention,
Trigger: tgUtils.EntityTypeMention,
HandlerFunc: p.notifyIfAFK,
GroupOnly: true,
},
Expand Down Expand Up @@ -124,7 +125,7 @@ func (p *Plugin) goAFK(b *gotgbot.Bot, c plugin.GobotContext) error {
}

func (p *Plugin) checkAFK(b *gotgbot.Bot, c plugin.GobotContext) error {
if strings.HasPrefix(utils.AnyText(c.EffectiveMessage), "/afk") {
if strings.HasPrefix(tgUtils.AnyText(c.EffectiveMessage), "/afk") {
return nil
}

Expand Down Expand Up @@ -178,8 +179,8 @@ func (p *Plugin) checkAFK(b *gotgbot.Bot, c plugin.GobotContext) error {

func (p *Plugin) notifyIfAFK(b *gotgbot.Bot, c plugin.GobotContext) error {
var mentionedUsername string
for _, entity := range utils.AnyEntities(c.EffectiveMessage) {
if utils.EntityType(entity.Type) == utils.EntityTypeMention {
for _, entity := range tgUtils.AnyEntities(c.EffectiveMessage) {
if tgUtils.EntityType(entity.Type) == tgUtils.EntityTypeMention {
if mentionedUsername != "" {
return nil // Supports only one username
}
Expand Down
5 changes: 3 additions & 2 deletions plugin/allow/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/rs/xid"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler {
}

func (p *Plugin) OnAllow(b *gotgbot.Bot, c plugin.GobotContext) error {
if utils.IsReply(c.EffectiveMessage) { // Allow user
if tgUtils.IsReply(c.EffectiveMessage) { // Allow user
if c.EffectiveMessage.ReplyToMessage.From.IsBot {
_, err := c.EffectiveMessage.Reply(b, "🤖🤖🤖", utils.DefaultSendOptions())
return err
Expand Down Expand Up @@ -111,7 +112,7 @@ func (p *Plugin) OnAllow(b *gotgbot.Bot, c plugin.GobotContext) error {
}

func (p *Plugin) OnDeny(b *gotgbot.Bot, c plugin.GobotContext) error {
if utils.IsReply(c.EffectiveMessage) { // Deny user
if tgUtils.IsReply(c.EffectiveMessage) { // Deny user
if c.EffectiveMessage.ReplyToMessage.From.IsBot {
_, err := c.EffectiveMessage.Reply(b, "🤖🤖🤖", utils.DefaultSendOptions())
return err
Expand Down
5 changes: 3 additions & 2 deletions plugin/amazon_ref_cleaner/amazon_ref_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Brawl345/gobot/logger"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
tgUtils "github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
)

Expand Down Expand Up @@ -40,8 +41,8 @@ func (p *Plugin) Handlers(*gotgbot.User) []plugin.Handler {

func onAmazonLink(b *gotgbot.Bot, c plugin.GobotContext) error {
var links []string
for _, entity := range utils.AnyEntities(c.EffectiveMessage) {
if utils.EntityType(entity.Type) == utils.EntityTypeURL {
for _, entity := range tgUtils.AnyEntities(c.EffectiveMessage) {
if tgUtils.EntityType(entity.Type) == tgUtils.EntityTypeURL {
amazonUrl, err := url.Parse(c.EffectiveMessage.ParseEntity(entity).Url)

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion plugin/calc/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/httpUtils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/rs/xid"
)
Expand Down Expand Up @@ -107,7 +108,7 @@ func calculate(expr string) (string, error) {
}

func onCalc(b *gotgbot.Bot, c plugin.GobotContext) error {
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil)
_, _ = c.EffectiveChat.SendAction(b, tgUtils.ChatActionTyping, nil)

result, err := calculate(c.Matches[1])
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion plugin/cleverbot/cleverbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/httpUtils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/rs/xid"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler {
}

func (p *Plugin) onCleverbot(b *gotgbot.Bot, c plugin.GobotContext) error {
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil)
_, _ = c.EffectiveChat.SendAction(b, tgUtils.ChatActionTyping, nil)

state, err := p.cleverbotService.GetState(c.EffectiveChat)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions plugin/covid/covid.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"github.com/Brawl345/gobot/utils/httpUtils"
"github.com/Brawl345/gobot/utils/tgUtils"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/rs/xid"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler {
}

func OnCountry(b *gotgbot.Bot, c plugin.GobotContext) error {
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil)
_, _ = c.EffectiveChat.SendAction(b, tgUtils.ChatActionTyping, nil)

var httpError *httpUtils.HttpError
var result countryResult
Expand Down Expand Up @@ -165,7 +166,7 @@ func OnCountry(b *gotgbot.Bot, c plugin.GobotContext) error {
),
)

_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil)
_, _ = c.EffectiveChat.SendAction(b, tgUtils.ChatActionTyping, nil)
var vaccine vaccineResult
err = httpUtils.GetRequest(
fmt.Sprintf(
Expand Down Expand Up @@ -209,7 +210,7 @@ func OnCountry(b *gotgbot.Bot, c plugin.GobotContext) error {
}

func OnRun(b *gotgbot.Bot, c plugin.GobotContext) error {
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil)
_, _ = c.EffectiveChat.SendAction(b, tgUtils.ChatActionTyping, nil)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
Loading

0 comments on commit f69e4ea

Please sign in to comment.