Skip to content

Commit

Permalink
add messages with locales
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Oct 2, 2022
1 parent c413c82 commit 3964109
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
BOT_TOKEN=
BOT_TOKEN=
DEFAULT_LOCALE=uk
BOT_PREFIX=🤖
2 changes: 1 addition & 1 deletion cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
}
}(database)

err = bot.InitBot(cfg.TelegramLoggerBotToken, database)
err = bot.InitBot(cfg.TelegramLoggerBotToken, cfg.DefaultLocale, cfg.BotPrefix, database)
if err != nil {
panic(err)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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"`
}

func LoadConfig(path, configFile, configType string) (config Config, err error) {
Expand All @@ -19,6 +21,8 @@ func LoadConfig(path, configFile, configType string) (config Config, err error)

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

viper.AutomaticEnv()

Expand Down
11 changes: 10 additions & 1 deletion internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ import (

"github.com/dgraph-io/badger/v3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"

"github.com/samarets/support-bot/internal/db"
"github.com/samarets/support-bot/internal/log"
"github.com/samarets/support-bot/internal/messages"
)

type bot struct {
bot *tgbotapi.BotAPI
db *botDB
mh *messages.Helper
}

func InitBot(token string, db *db.DB) error {
func InitBot(token, defaultLocale, botPrefix string, db *db.DB) error {
mh, err := messages.NewMessagesHelper(defaultLocale, botPrefix)
if err != nil {
return err
}

botAPI, err := tgbotapi.NewBotAPI(token)
if err != nil {
return err
Expand All @@ -25,6 +33,7 @@ func InitBot(token string, db *db.DB) error {
botState := &bot{
bot: botAPI,
db: dbState,
mh: mh,
}
botState.InitUpdates()

Expand Down
5 changes: 3 additions & 2 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/log"
"github.com/samarets/support-bot/internal/messages"
)

const (
Expand All @@ -21,7 +22,7 @@ func (b *bot) StartCommand(update tgbotapi.Update) {

msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
"🤖 Привіт, напиши своє питання - ми допоможемо",
b.mh.GetMessage(messages.HelloMessage, update.SentFrom().LanguageCode),
)

_, err := b.bot.Send(msg)
Expand Down Expand Up @@ -77,7 +78,7 @@ func (b *bot) ConnectCommand(update tgbotapi.Update) {
return
}

msg := tgbotapi.NewMessage(user.ID, "🤖 До вас доєднався оператор")
msg := tgbotapi.NewMessage(user.ID, b.mh.GetMessage(messages.OperatorConnected, update.SentFrom().LanguageCode))
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
Expand Down
28 changes: 28 additions & 0 deletions internal/messages/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package messages

import "fmt"

type Helper struct {
defaultLocale Locale
botPrefix string
}

func NewMessagesHelper(defaultLocale, botPrefix string) (*Helper, error) {
if !localeExist(Locale(defaultLocale)) {
return nil, fmt.Errorf("default locale is not found among existing locales")
}

return &Helper{
defaultLocale: Locale(defaultLocale),
botPrefix: botPrefix,
}, nil
}

func (h *Helper) GetMessage(code Code, locale string) string {
loc := Locale(locale)
if !localeExist(loc) {
loc = h.defaultLocale
}

return h.botPrefix + " " + fmt.Sprintf(LocalMessages[code][loc])
}
19 changes: 19 additions & 0 deletions internal/messages/locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package messages

type Locale string

const (
LocaleUK Locale = "uk"
LocaleEN Locale = "en"
)

func localeExist(locale Locale) bool {
switch locale {
case LocaleUK:
return true
case LocaleEN:
return true
}

return false
}
19 changes: 19 additions & 0 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package messages

type Code string

const (
HelloMessage Code = "hello"
OperatorConnected Code = "op-conn"
)

var LocalMessages = map[Code]map[Locale]string{
HelloMessage: {
LocaleUK: "Привіт, напиши своє питання - ми допоможемо",
LocaleEN: "Hello, write your question and we will help",
},
OperatorConnected: {
LocaleUK: "До вас доєднався оператор",
LocaleEN: "An operator has joined you",
},
}

0 comments on commit 3964109

Please sign in to comment.