Skip to content

Commit

Permalink
Added topics reply to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani authored and Flecart committed Aug 11, 2023
1 parent 8ac1773 commit 05647af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
23 changes: 20 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ func run(bot *tgbotapi.BotAPI) {
} else {
// text message
for i := 0; i < len(model.Autoreplies); i++ {
if strings.Contains(strings.ToLower(update.Message.Text), strings.ToLower(model.Autoreplies[i].Text)) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, model.Autoreplies[i].Reply)
if strings.Contains(strings.ToLower(update.Message.Text),
strings.ToLower(model.Autoreplies[i].Text)) {
var msg tgbotapi.MessageConfig

if update.Message.IsTopicMessage {
msg = tgbotapi.NewThreadMessage(update.Message.Chat.ID,
update.Message.MessageThreadID, model.Autoreplies[i].Reply)
} else {
msg = tgbotapi.NewMessage(update.Message.Chat.ID,
model.Autoreplies[i].Reply)
}

msg.ReplyToMessageID = update.Message.MessageID
utils.SendHTML(bot, msg)
}
Expand Down Expand Up @@ -82,7 +92,14 @@ func executeCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, commandIndex
newCommand := model.Actions[commandIndex].Data.HandleBotCommand(bot, update.Message)

if newCommand.HasText() {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, newCommand.Text)
var msg tgbotapi.MessageConfig

if update.Message.IsTopicMessage {
msg = tgbotapi.NewThreadMessage(update.Message.Chat.ID,
update.Message.MessageThreadID, newCommand.Text)
} else {
msg = tgbotapi.NewMessage(update.Message.Chat.ID, newCommand.Text)
}
utils.SendHTML(bot, msg)
}

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ go 1.18

require (
github.com/mitchellh/mapstructure v1.5.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/text v0.12.0
github.com/musianisamuele/telegram-bot-api v0.0.4
)

require (
github.com/musianisamuele/telegram-bot-api v0.0.3 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
golang.org/x/exp v0.0.0-20230809094429-853ea248256d
golang.org/x/text v0.12.0
)
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/musianisamuele/telegram-bot-api v0.0.3 h1:8BTfHOPYTMkn0AX4FhicW1CyzU1NQdCIqlggfuT3P/I=
github.com/musianisamuele/telegram-bot-api v0.0.3/go.mod h1:f8epVo400dyxqaQpXt3la1mnhdQW25R1w3yqYT3GQc4=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
github.com/musianisamuele/telegram-bot-api v0.0.4 h1:kQoE4Ih/rnyf2i8iCOwcN+zflx+H1A7+PhZx9Kkqppk=
github.com/musianisamuele/telegram-bot-api v0.0.4/go.mod h1:f8epVo400dyxqaQpXt3la1mnhdQW25R1w3yqYT3GQc4=
golang.org/x/exp v0.0.0-20230809094429-853ea248256d h1:wu5bD43Ana/nF1ZmaLr3lW/FQeJU8CcI+Ln7yWHViXE=
golang.org/x/exp v0.0.0-20230809094429-853ea248256d/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
10 changes: 8 additions & 2 deletions model/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ func ParseActionsBytes(bytes []byte) ([]Action, error) {
actions = append(actions, action)
}

slices.SortFunc(actions, func(a, b Action) bool {
return a.Name < b.Name
slices.SortFunc(actions, func(a, b Action) int {
if a.Name < b.Name {
return -1
} else if a.Name > b.Name {
return 1
} else {
return 0
}
})

return actions, nil
Expand Down

0 comments on commit 05647af

Please sign in to comment.