Skip to content

Commit

Permalink
added adding support users
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Oct 12, 2022
1 parent 882d22e commit 223863d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/kataras/i18n v0.0.6
github.com/rs/zerolog v1.27.0
github.com/spf13/viper v1.12.0
golang.org/x/text v0.3.7
)

require (
Expand Down Expand Up @@ -41,6 +40,7 @@ require (
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (b *bot) InitUpdates() {
b.GetID(update)
case setGroup:
b.SetGroup(update)
case addSupport:
b.AddSupport(update, b.adminID)
case event:
b.Event(update)
}
Expand Down
61 changes: 59 additions & 2 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"fmt"
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/log"
Expand All @@ -14,8 +15,9 @@ const (
cancelCommand = "cancel"
getID = "getid"

setGroup = "set_group"
event = "event"
setGroup = "set_group"
addSupport = "add_support"
event = "event"
)

func (b *bot) StartCommand(update tgbotapi.Update, userState state) {
Expand Down Expand Up @@ -260,6 +262,61 @@ func (b *bot) SetGroup(update tgbotapi.Update) {
}
}

func (b *bot) AddSupport(update tgbotapi.Update, adminID int64) {
if update.SentFrom().ID != adminID {
return
}

var userID int64
if update.Message.ReplyToMessage != nil && !update.Message.ReplyToMessage.From.IsBot {
if update.Message.ReplyToMessage.From.ID == adminID {
return
}
userID = update.Message.ReplyToMessage.From.ID
} else {
argumentID, err := strconv.ParseInt(update.Message.CommandArguments(), 10, 64)
if err != nil {
msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
b.tl.GetMessage(b.db.languageDB().get(update.SentFrom().ID), "add_support_fail"),
)
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = tgbotapi.ModeMarkdown
_, err := b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}
return
}
if argumentID == adminID {
return
}
userID = argumentID
}

err := b.db.supportDB().set(userID, true)
if err != nil {
log.Error().Err(err).Send()
return
}

msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
b.tl.GetMessage(
b.db.languageDB().get(update.SentFrom().ID), "add_support_success", map[string]interface{}{
"UserID": userID,
},
),
)
msg.ReplyToMessageID = update.Message.MessageID
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}
}

func (b *bot) Event(update tgbotapi.Update) {
groupID := b.db.groupDB().get()

Expand Down
31 changes: 31 additions & 0 deletions internal/bot/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
messagesIDs = "messagesIDs"
language = "language"
group = "group"
support = "support"
)

type botDB struct {
Expand Down Expand Up @@ -253,6 +254,36 @@ func (db *groupDB) get() int64 {
return groupID
}

type supportDB struct {
*botDB
}

func (db *botDB) supportDB() *supportDB {
return &supportDB{
botDB: db,
}
}

func (db *supportDB) set(supportID int64, isSupport bool) error {
return db.db.Set(mergePrefixDB(support, supportID), isSupport)
}

func (db *supportDB) get(userID int64) bool {
var isSupport bool
err := db.db.Get(mergePrefixDB(support, userID), &isSupport)
if err != nil {
switch err {
case badger.ErrKeyNotFound:
return false
default:
log.Error().Err(err).Send()
return false
}
}

return isSupport
}

func mergePrefixDB(prefix string, id interface{}) []byte {
return []byte(fmt.Sprintf("%s-%d", prefix, id))
}
4 changes: 3 additions & 1 deletion locales/en-US/admin.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"channel_saved": "The message distribution channel has been saved",
"user_connected": "You have been attached to a user [{{ .Name }}](tg:https://user?id={{ .ID }})\nID: {{ .ID }}\nUser question:"
"user_connected": "You have been attached to a user [{{ .Name }}](tg:https://user?id={{ .ID }})\nID: {{ .ID }}\nUser question:",
"add_support_fail": "Command error. You need to reply to the message with this command or type after the command `/add_support 1234567890`",
"add_support_success": "The user {{ .UserID }} has been granted technical support rights"
}
4 changes: 3 additions & 1 deletion locales/uk-UA/admin.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"channel_saved": "Канал для поширення повідомлень був збережений",
"user_connected": "Ви були доєднані до користувача [{{ .Name }}](tg:https://user?id={{ .ID }})\nID: {{ .ID }}\nПитання користувача:"
"user_connected": "Ви були доєднані до користувача [{{ .Name }}](tg:https://user?id={{ .ID }})\nID: {{ .ID }}\nПитання користувача:",
"add_support_fail": "Неправильно введена команда. Потрібно відповісти на повідомлення командою чи ввести ID після команди `/add_support 1234567890`",
"add_support_success": "Користувачу {{ .UserID }} було видано права технічної підтримки"
}

0 comments on commit 223863d

Please sign in to comment.