Skip to content

Commit

Permalink
add admin, add groupDB and prepare to groups events. many refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Oct 3, 2022
1 parent eca0743 commit 6616ec0
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BOT_TOKEN=
ADMIN_USER_ID=0
DEFAULT_LOCALE=uk-UA
BOT_PREFIX=🤖
2 changes: 1 addition & 1 deletion cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
}
}(database)

err = bot.InitBot(cfg.TelegramLoggerBotToken, translator, database)
err = bot.InitBot(cfg.TelegramLoggerBotToken, cfg.TelegramAdminUserID, translator, database)
if err != nil {
log.Error().Err(err).Send()
panic(err)
Expand Down
10 changes: 5 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type Config struct {
TelegramLoggerBotToken string `mapstructure:"bot_token"`
TelegramLoggerBotChatID int64 `mapstructure:"notifications_chat_id"`
DefaultLocale string `mapstructure:"default_locale"`
BotPrefix string `mapstructure:"bot_prefix"`
TelegramLoggerBotToken string `mapstructure:"bot_token"`
TelegramAdminUserID int64 `mapstructure:"admin_user_id"`
DefaultLocale string `mapstructure:"default_locale"`
BotPrefix string `mapstructure:"bot_prefix"`
}

func LoadConfig(path, configFile, configType string) (config Config, err error) {
Expand All @@ -20,7 +20,7 @@ func LoadConfig(path, configFile, configType string) (config Config, err error)
viper.SetConfigType(configType)

viper.SetDefault("bot_token", "")
viper.SetDefault("notifications_chat_id", 0)
viper.SetDefault("admin_user_id", 0)
viper.SetDefault("default_locale", "uk-UA")
viper.SetDefault("bot_prefix", "🤖")

Expand Down
38 changes: 28 additions & 10 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

type bot struct {
bot *tgbotapi.BotAPI
db *botDB
tl *translations.Translator
bot *tgbotapi.BotAPI
db *botDB
tl *translations.Translator
adminID int64
}

func InitBot(token string, translator *translations.Translator, db *db.DB) error {

func InitBot(token string, adminID int64, translator *translations.Translator, db *db.DB) error {
botAPI, err := tgbotapi.NewBotAPI(token)
if err != nil {
return err
Expand All @@ -27,9 +27,10 @@ func InitBot(token string, translator *translations.Translator, db *db.DB) error
dbState := newBotDB(db)

botState := &bot{
bot: botAPI,
db: dbState,
tl: translator,
bot: botAPI,
db: dbState,
tl: translator,
adminID: adminID,
}
botState.InitUpdates()

Expand All @@ -52,6 +53,13 @@ func (b *bot) InitUpdates() {
}

if update.Message != nil {
if update.Message.MigrateToChatID != 0 && b.db.groupDB().get() == update.Message.Chat.ID {
err := b.db.groupDB().set(update.Message.MigrateToChatID)
if err != nil {
log.Error().Err(err).Send()
}
}

if update.Message.IsCommand() {
switch update.Message.Command() {
case startCommand:
Expand All @@ -62,11 +70,21 @@ func (b *bot) InitUpdates() {
b.BreakCommand(update)
case cancelCommand:
b.CancelCommand(update)
case getID:
b.GetID(update)
case setGroup:
b.SetGroup(update)
case event:
b.Event(update)
}

continue
}

if !update.FromChat().IsPrivate() {
continue
}

fromQueue, err := b.db.queueDB().get(update.Message.From.ID)
if err != nil {
log.Error().Err(err).Send()
Expand Down Expand Up @@ -95,7 +113,7 @@ func (b *bot) InitUpdates() {
}

msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
update.Message.From.ID,
"🤖 Ваше повідомлення було отримано, якщо у вас є що додати - напишіть.\n\nСкоро до вас доєднається оператор технічної підтримки",
)
_, err = b.bot.Send(msg)
Expand Down Expand Up @@ -135,7 +153,7 @@ func (b *bot) InitUpdates() {
log.Error().Err(fmt.Errorf("whoomSend is empty")).Send()
}

msg := tgbotapi.NewCopyMessage(*whoomSend, update.Message.Chat.ID, update.Message.MessageID)
msg := tgbotapi.NewCopyMessage(*whoomSend, update.Message.From.ID, update.Message.MessageID)
if update.Message.ReplyToMessage != nil {
replyToID, err := b.db.messagesIDsDB().get(update.Message.ReplyToMessage.MessageID)
if err != nil && err != badger.ErrKeyNotFound {
Expand Down
54 changes: 53 additions & 1 deletion internal/bot/commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bot

import (
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/log"
)
Expand All @@ -10,10 +12,14 @@ const (
connectCommand = "connect"
breakCommand = "break"
cancelCommand = "cancel"
getID = "getid"

setGroup = "set_group"
event = "event"
)

func (b *bot) StartCommand(update tgbotapi.Update) {
if update.Message == nil {
if !update.FromChat().IsPrivate() {
return
}

Expand Down Expand Up @@ -213,3 +219,49 @@ func (b *bot) CancelCommand(update tgbotapi.Update) {
return
}
}

func (b *bot) GetID(update tgbotapi.Update) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("🤖 %d", update.SentFrom().ID))
_, err := b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}
}

func (b *bot) SetGroup(update tgbotapi.Update) {
user := update.SentFrom()
if user.ID != b.adminID {
return
}

err := b.db.groupDB().set(update.FromChat().ID)
if err != nil {
log.Error().Err(err).Send()
return
}

msg := tgbotapi.NewMessage(
update.FromChat().ID,
b.tl.GetMessage(b.db.languageDB().get(user.ID), "channel_saved"),
)
_, 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()

msg := tgbotapi.NewMessage(
groupID,
"event",
)
_, err := b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}
}
31 changes: 31 additions & 0 deletions internal/bot/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
buffer = "buffer"
messagesIDs = "messagesIDs"
language = "language"
group = "group"
)

type botDB struct {
Expand Down Expand Up @@ -201,6 +202,36 @@ func (db *languageDB) get(userID int64) string {
return lang
}

type groupDB struct {
*botDB
}

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

func (db *groupDB) set(groupID int64) error {
return db.db.Set([]byte(group), groupID)
}

func (db *groupDB) get() int64 {
var groupID int64
err := db.db.Get([]byte(group), &groupID)
if err != nil {
switch err {
case badger.ErrKeyNotFound:
return 0
default:
log.Error().Err(err).Send()
return 0
}
}

return groupID
}

func mergePrefixDB(prefix string, id interface{}) []byte {
return []byte(fmt.Sprintf("%s-%d", prefix, id))
}
1 change: 1 addition & 0 deletions locales/en-US/admin.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"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:"
}
1 change: 1 addition & 0 deletions locales/uk-UA/admin.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"channel_saved": "Канал для поширення повідомлень був збережений",
"user_connected": "Ви були доєднані до користувача [{{ .Name }}](tg:https://user?id={{ .ID }})\nID: {{ .ID }}\nПитання користувача:"
}

0 comments on commit 6616ec0

Please sign in to comment.