diff --git a/.air-unix.toml b/.air-unix.toml index 3a8ba53..14e7760 100644 --- a/.air-unix.toml +++ b/.air-unix.toml @@ -6,6 +6,7 @@ root = "/app" tmp_dir = "tmp" [build] +poll=true # Just plain old shell command. You could use `make` as well. cmd = "go build -o dist/app cmd/main.go" # Binary file yields from `cmd`. diff --git a/.env.example b/.env.example index 665d135..c168e71 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,18 @@ +APP_NAME=bot +APP_MODE=develop APP_PORT=8080 -APP_NAME=app -APP_MODE=develop \ No newline at end of file + +POSTGRES_HOST=127.0.0.1 +POSTGRES_PORT=5432 +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_DB=postgres +POSTGRES_USE_SSL=false +POSTGRES_URI=postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable&search_path=public + +REDIS_URI=redis://127.0.0.1:6379/0 + +TELEGRAM_BOT_TOKEN= +TELEGRAM_BOT_PREFIX=eshop_support_bot + +DEFAULT_LOCALE=ru-RU diff --git a/.env.local b/.env.local index e69de29..74a9db1 100644 --- a/.env.local +++ b/.env.local @@ -0,0 +1,2 @@ +APP_NAME=bot +DEFAULT_LOCALE=ru-RU diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..e388291 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,36 @@ +name: "CodeQL" + +on: + push: + branches: [develop] + pull_request: + branches: [main] + schedule: + - cron: "30 1 * * 0" + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + + permissions: + security-events: write + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + language: ["go"] + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..5b19757 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,28 @@ +name: Run Lint + +on: + push: + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + check-latest: true + + - name: check golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + version: latest + args: --verbose + + - uses: hadolint/hadolint-action@v3.1.0 + name: hadolint for Dockerfile + with: + dockerfile: Dockerfile diff --git a/Dockerfile b/Dockerfile index 7f46d28..ceb0533 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ FROM base as dev WORKDIR /app COPY . . RUN go install -mod=mod github.com/cosmtrek/air -ENTRYPOINT ["air"] +CMD ["air", "-c", ".air-unix.toml", "-d"] # # Test Stage # FROM base as test diff --git a/README.md b/README.md index 59be22e..271c9de 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,85 @@ -# e-shop-support-bot -TODO \ No newline at end of file +
+ Microservice handles support via Telegram Bot +
+This project is licensed under the MIT License.
+ +Made with ❤️ by me \ No newline at end of file diff --git a/assets/img.png b/assets/img.png new file mode 100644 index 0000000..f3747f2 Binary files /dev/null and b/assets/img.png differ diff --git a/assets/img1.png b/assets/img1.png new file mode 100644 index 0000000..e1401e1 Binary files /dev/null and b/assets/img1.png differ diff --git a/assets/img2.png b/assets/img2.png new file mode 100644 index 0000000..c951dde Binary files /dev/null and b/assets/img2.png differ diff --git a/assets/img3.png b/assets/img3.png new file mode 100644 index 0000000..27dada3 Binary files /dev/null and b/assets/img3.png differ diff --git a/cmd/main.go b/cmd/main.go index dd66d1b..253dfb4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,7 +5,7 @@ import ( "os/signal" "syscall" - server "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal" + server "github.com/WildEgor/e-shop-support-bot/internal" ) // @title Swagger Doc diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..b388e1f --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,21 @@ +version: "3.8" + +services: + bot: + hostname: bot-dev + restart: unless-stopped + build: + # if use private libs uncomment this + # args: + # - GITHUB_TOKEN=${GITHUB_TOKEN} + context: ./ + dockerfile: Dockerfile + target: dev + env_file: + - .env + volumes: + - ./:/app + ports: + - 8888:${APP_PORT} + extra_hosts: + - "host.docker.internal:host-gateway" \ No newline at end of file diff --git a/go.mod b/go.mod index dd1d23b..a199aee 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/WildEgor/e-shop-fiber-microservice-boilerplate +module github.com/WildEgor/e-shop-support-bot go 1.22.1 diff --git a/internal/adapters/telegram/bot.go b/internal/adapters/telegram/bot.go index 1a2ca26..da97879 100644 --- a/internal/adapters/telegram/bot.go +++ b/internal/adapters/telegram/bot.go @@ -2,8 +2,8 @@ package telegram import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/configs" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" ) @@ -24,9 +24,6 @@ func NewTelegramBotAdapter(cfg *configs.TelegramConfig) *TelegramBotAdapter { return nil } - // TODO: only for test - bot.Debug = true - if err != nil { slog.Error("error init bot", models.LogEntryAttr(&models.LogEntry{ Err: err, @@ -34,6 +31,8 @@ func NewTelegramBotAdapter(cfg *configs.TelegramConfig) *TelegramBotAdapter { panic(err) } + bot.Debug = cfg.Debug + return &TelegramBotAdapter{ bot: bot, handler: func(ctx context.Context, update tgbotapi.Update) {}, diff --git a/internal/adapters/telegram/listener.go b/internal/adapters/telegram/listener.go index db10437..8de4ba9 100644 --- a/internal/adapters/telegram/listener.go +++ b/internal/adapters/telegram/listener.go @@ -2,6 +2,7 @@ package telegram import ( "context" + "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "regexp" @@ -26,6 +27,7 @@ func NewTelegramListener( ) *TelegramListener { return &TelegramListener{ adapter: adapter, + middlewares: make([]Middleware, 0), messageHandlers: make([]messageHandler, 0), editMessageHandler: func(ctx context.Context, message *tgbotapi.Message) {}, callbackQueryMatcher: make(callbackQueryMatcher), @@ -64,8 +66,6 @@ func (t *TelegramListener) ListenUpdates(ctx context.Context) { slog.Debug("bot is listening") h := func(ctx context.Context, update tgbotapi.Update) { - slog.Debug("Update ID", slog.Int("ID", update.UpdateID)) - var f = t.handleUpdates for i := len(t.middlewares) - 1; i >= 0; i-- { f = t.middlewares[i](f) @@ -74,6 +74,13 @@ func (t *TelegramListener) ListenUpdates(ctx context.Context) { go f(ctx, update) } + slog.Debug("listen updates executed", models.LogEntryAttr(&models.LogEntry{ + Props: map[string]interface{}{ + "count_mw": len(t.middlewares), + "count_h": len(t.messageHandlers), + }, + })) + t.adapter.HandleUpdates(ctx, h) } @@ -81,11 +88,16 @@ func (t *TelegramListener) ListenUpdates(ctx context.Context) { func (t *TelegramListener) handleUpdates(ctx context.Context, update tgbotapi.Update) { switch { case update.Message != nil: + slog.Debug("handle message") t.handleMessage(ctx, update.Message) case update.EditedMessage != nil: + slog.Debug("handle edit message") t.editMessageHandler(ctx, update.EditedMessage) case update.CallbackQuery != nil: + slog.Debug("handle callback") t.handleCallback(ctx, update.CallbackQuery) + default: + slog.Warn("No handler") } } diff --git a/internal/adapters/wire.go b/internal/adapters/wire.go index 410036c..c813577 100644 --- a/internal/adapters/wire.go +++ b/internal/adapters/wire.go @@ -1,7 +1,7 @@ package adapters import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" "github.com/google/wire" ) diff --git a/internal/app.go b/internal/app.go index 1391fa3..c3e68a1 100644 --- a/internal/app.go +++ b/internal/app.go @@ -3,14 +3,14 @@ package pkg import ( "context" "fmt" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - eh "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/errors" - nfm "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/middlewares/not_found" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/router" slogger "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/handlers" "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + eh "github.com/WildEgor/e-shop-support-bot/internal/handlers/errors" + nfm "github.com/WildEgor/e-shop-support-bot/internal/middlewares/not_found" + "github.com/WildEgor/e-shop-support-bot/internal/router" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" "github.com/gofiber/fiber/v3/middleware/recover" @@ -61,6 +61,7 @@ func NewApp( eh *eh.ErrorsHandler, prr *router.PrivateRouter, pbr *router.PublicRouter, + br *router.BotRouter, sr *router.SwaggerRouter, bot *telegram.TelegramListener, pc *configs.PostgresConfig, @@ -90,6 +91,7 @@ func NewApp( })) app.Use(recover.New()) + br.Setup() prr.Setup(app) pbr.Setup(app) sr.Setup(app) diff --git a/internal/configs/telegram.go b/internal/configs/telegram.go index b4d6417..6acb89e 100644 --- a/internal/configs/telegram.go +++ b/internal/configs/telegram.go @@ -9,7 +9,8 @@ import ( // TelegramConfig holds the main app configurations type TelegramConfig struct { Token string `env:"TELEGRAM_BOT_TOKEN,required"` - Prefix string `env:"TELEGRAM_BOT_PREFIX" envDefault:"g_supporter_bot"` + Prefix string `env:"TELEGRAM_BOT_PREFIX,required"` + Debug bool `env:"TELEGRAM_DEBUG" envDefault:"false"` } func NewTelegramConfig(c *Configurator) *TelegramConfig { diff --git a/internal/db/postgres/conn.go b/internal/db/postgres/conn.go index 096d3a6..91d92ab 100644 --- a/internal/db/postgres/conn.go +++ b/internal/db/postgres/conn.go @@ -2,8 +2,8 @@ package postgres import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/configs" "github.com/jackc/pgx/v5/pgxpool" _ "github.com/jackc/pgx/v5/stdlib" "log/slog" diff --git a/internal/db/redis/conn.go b/internal/db/redis/conn.go index a98a6fd..8b85d08 100644 --- a/internal/db/redis/conn.go +++ b/internal/db/redis/conn.go @@ -1,8 +1,8 @@ package redis import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/configs" "github.com/go-redis/redis" "log/slog" ) diff --git a/internal/db/wire.go b/internal/db/wire.go index cf9f1a1..1c6944b 100644 --- a/internal/db/wire.go +++ b/internal/db/wire.go @@ -1,8 +1,8 @@ package db import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/postgres" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/db/postgres" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" "github.com/google/wire" ) diff --git a/internal/handlers/accept_callback/handler.go b/internal/handlers/accept_callback/handler.go index a4a8f5e..a67ac68 100644 --- a/internal/handlers/accept_callback/handler.go +++ b/internal/handlers/accept_callback/handler.go @@ -2,11 +2,11 @@ package accept_callback_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "strings" @@ -52,7 +52,7 @@ func (h *AcceptCallbackHandler) Handle(ctx context.Context, update *tgbotapi.Cal } sopts := h.checkAndGetSupportOpts(ctx, update.ID, update.From.ID) - uopts := h.checkAndGetUserOpts(ctx, existedTopic, update.ID, update.From.ID) + uopts := h.checkAndGetUserOpts(ctx, existedTopic, update.ID) existedTopic.Creator.TelegramChatId = uopts.ChatId existedTopic.Support = models.TopicSupport{ @@ -162,7 +162,7 @@ func (h *AcceptCallbackHandler) checkAndGetSupportOpts(ctx context.Context, call } // checkAndGetUserOpts checks that user's topic still need help -func (h *AcceptCallbackHandler) checkAndGetUserOpts(ctx context.Context, topic *models.Topic, callbackId string, id int64) *models.UserOptions { +func (h *AcceptCallbackHandler) checkAndGetUserOpts(ctx context.Context, topic *models.Topic, callbackId string) *models.UserOptions { ustate := h.uor.CheckUserState(ctx, topic.Creator.TelegramId) uopts, err := h.uor.GetUserOptions(ctx, topic.Creator.TelegramId) diff --git a/internal/handlers/break_action/handler.go b/internal/handlers/break_action/handler.go index 14d2f89..bfc6ec9 100644 --- a/internal/handlers/break_action/handler.go +++ b/internal/handlers/break_action/handler.go @@ -2,11 +2,11 @@ package break_action_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "time" @@ -142,6 +142,10 @@ func (h *BreakActionHandler) notifyTopicClosed(ctx context.Context, msg *tgbotap }) } + if topts == nil { + return + } + tendMsg := h.ts.GetLocalizedMessage(topts.Lang, models.TicketClosedMessageKey) if err := h.tga.SendSimpleChatMessage(msg.Chat.ID, tendMsg); err != nil { slog.Error("error send end ticket", "data", &logModels.LogEntry{ diff --git a/internal/handlers/decline_callback/handler.go b/internal/handlers/decline_callback/handler.go index 95de262..e65d753 100644 --- a/internal/handlers/decline_callback/handler.go +++ b/internal/handlers/decline_callback/handler.go @@ -2,11 +2,11 @@ package decline_callback_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "time" diff --git a/internal/handlers/edit_message/handler.go b/internal/handlers/edit_message/handler.go index 8f8d7e9..32f1bba 100644 --- a/internal/handlers/edit_message/handler.go +++ b/internal/handlers/edit_message/handler.go @@ -2,10 +2,10 @@ package edit_message_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/mappers" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/mappers" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "time" ) diff --git a/internal/handlers/new_message/handler.go b/internal/handlers/new_message/handler.go index c86b5bd..d142f92 100644 --- a/internal/handlers/new_message/handler.go +++ b/internal/handlers/new_message/handler.go @@ -3,12 +3,12 @@ package new_message_handler import ( "context" "errors" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/mappers" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/mappers" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/jackc/pgx/v5" "log/slog" diff --git a/internal/handlers/not_right_callback/handler.go b/internal/handlers/no_right_callback/handler.go similarity index 78% rename from internal/handlers/not_right_callback/handler.go rename to internal/handlers/no_right_callback/handler.go index 5959c0a..f82f55b 100644 --- a/internal/handlers/not_right_callback/handler.go +++ b/internal/handlers/no_right_callback/handler.go @@ -2,11 +2,11 @@ package no_right_callback_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "time" diff --git a/internal/handlers/rating_callback/handler.go b/internal/handlers/rating_callback/handler.go index d4521af..2c440e6 100644 --- a/internal/handlers/rating_callback/handler.go +++ b/internal/handlers/rating_callback/handler.go @@ -2,11 +2,11 @@ package rating_callback_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "time" diff --git a/internal/handlers/start_action/handler.go b/internal/handlers/start_action/handler.go index 1b71430..0f6dfa7 100644 --- a/internal/handlers/start_action/handler.go +++ b/internal/handlers/start_action/handler.go @@ -2,11 +2,11 @@ package start_action_handler import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "time" diff --git a/internal/handlers/wire.go b/internal/handlers/wire.go index f885bee..edebee7 100644 --- a/internal/handlers/wire.go +++ b/internal/handlers/wire.go @@ -1,14 +1,34 @@ package handlers import ( - eh "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/errors" - hch "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/health_check" - rch "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ready_check" + accept_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/accept_callback" + break_action_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/break_action" + decline_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/decline_callback" + edit_message_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/edit_message" + eh "github.com/WildEgor/e-shop-support-bot/internal/handlers/errors" + hch "github.com/WildEgor/e-shop-support-bot/internal/handlers/health_check" + new_message_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/new_message" + no_right_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/no_right_callback" + rating_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/rating_callback" + rch "github.com/WildEgor/e-shop-support-bot/internal/handlers/ready_check" + start_action_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/start_action" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + "github.com/WildEgor/e-shop-support-bot/internal/services" "github.com/google/wire" ) var HandlersSet = wire.NewSet( + services.ServicesSet, + repositories.RepositoriesSet, eh.NewErrorsHandler, hch.NewHealthCheckHandler, rch.NewReadyCheckHandler, + accept_callback_handler.NewAcceptCallbackHandler, + break_action_handler.NewBreakActionHandler, + decline_callback_handler.NewDeclineCallbackHandler, + edit_message_handler.NewEditMessageHandler, + new_message_handler.NewNewMessageHandler, + no_right_callback_handler.NewNoRightCallbackHandler, + rating_callback_handler.NewRatingCallbackHandler, + start_action_handler.NewStartActionHandler, ) diff --git a/internal/mappers/messages.go b/internal/mappers/messages.go index cf776bc..382004b 100644 --- a/internal/mappers/messages.go +++ b/internal/mappers/messages.go @@ -1,7 +1,7 @@ package mappers import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) diff --git a/internal/mappers/topics.go b/internal/mappers/topics.go index eb694b4..8a6cffa 100644 --- a/internal/mappers/topics.go +++ b/internal/mappers/topics.go @@ -1,6 +1,6 @@ package mappers -import "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" +import "github.com/WildEgor/e-shop-support-bot/internal/models" func FromTopicsTableToModel(table *models.TopicTable) *models.Topic { return &models.Topic{ diff --git a/internal/middlewares/auth/handler.go b/internal/middlewares/auth/handler.go index 19e15b9..7d2c760 100644 --- a/internal/middlewares/auth/handler.go +++ b/internal/middlewares/auth/handler.go @@ -2,9 +2,10 @@ package middlewares import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "log/slog" ) // AuthMiddleware check if user admin/support for callback actions @@ -19,6 +20,8 @@ func (m *AuthMiddleware) Next(h telegram.UpdateHandler) telegram.UpdateHandler { return func(ctx context.Context, u tgbotapi.Update) { defer h(ctx, u) + slog.Debug("Update ID", slog.Int("ID", u.UpdateID)) + // FIXME: dirty solution route user to no_right handler // If callback called with non-support privileges then redirect to no_right handler if u.CallbackQuery != nil && (u.CallbackQuery.Data == models.AcceptHelp || u.CallbackQuery.Data == models.DeclineHelp) { diff --git a/internal/middlewares/group/handler.go b/internal/middlewares/group/handler.go index 2ba0136..8f34352 100644 --- a/internal/middlewares/group/handler.go +++ b/internal/middlewares/group/handler.go @@ -2,10 +2,10 @@ package middlewares import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" logModels "github.com/WildEgor/e-shop-gopack/pkg/libs/logger/models" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "log/slog" "strings" @@ -34,10 +34,15 @@ func (m *ExtractGroupMiddleware) Next(h telegram.UpdateHandler) telegram.UpdateH return func(ctx context.Context, u tgbotapi.Update) { defer h(ctx, u) + slog.Debug("", slog.Any("update", u.MyChatMember)) + if u.MyChatMember == nil || u.MyChatMember.NewChatMember.Status != "administrator" { + slog.Debug("ignore") return } + slog.Debug("Try save group") + chatID := u.MyChatMember.Chat.ID if strings.Contains(u.MyChatMember.NewChatMember.User.UserName, m.tcfg.Prefix) { diff --git a/internal/repositories/group.go b/internal/repositories/group.go index ede23b9..48b575d 100644 --- a/internal/repositories/group.go +++ b/internal/repositories/group.go @@ -2,7 +2,7 @@ package repositories import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" "strconv" ) diff --git a/internal/repositories/init_e2e_test.go b/internal/repositories/init_e2e_test.go index 58aa248..a4aaf2b 100644 --- a/internal/repositories/init_e2e_test.go +++ b/internal/repositories/init_e2e_test.go @@ -3,11 +3,11 @@ package repositories_test import ( "context" "fmt" - pkg "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - postgres2 "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/postgres" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/redis" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" + pkg "github.com/WildEgor/e-shop-support-bot/internal" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + postgres2 "github.com/WildEgor/e-shop-support-bot/internal/db/postgres" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/modules/postgres" redis2 "github.com/testcontainers/testcontainers-go/modules/redis" diff --git a/internal/repositories/topics.go b/internal/repositories/topics.go index 06d1351..83f8c8a 100644 --- a/internal/repositories/topics.go +++ b/internal/repositories/topics.go @@ -4,10 +4,10 @@ import ( "context" "encoding/json" "fmt" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/postgres" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/redis" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/mappers" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/db/postgres" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/mappers" + "github.com/WildEgor/e-shop-support-bot/internal/models" "github.com/google/uuid" "github.com/jackc/pgx/v5" "time" diff --git a/internal/repositories/topics_e2e_test.go b/internal/repositories/topics_e2e_test.go index 3e7128a..2cf6e88 100644 --- a/internal/repositories/topics_e2e_test.go +++ b/internal/repositories/topics_e2e_test.go @@ -2,7 +2,7 @@ package repositories_test import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/models" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/repositories/user_state.go b/internal/repositories/user_state.go index b41d342..10820be 100644 --- a/internal/repositories/user_state.go +++ b/internal/repositories/user_state.go @@ -5,8 +5,8 @@ import ( "encoding/json" "errors" "fmt" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db/redis" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/models" rediss "github.com/go-redis/redis" "time" ) diff --git a/internal/repositories/user_state_e2e_test.go b/internal/repositories/user_state_e2e_test.go index 7ce5bf9..f900c95 100644 --- a/internal/repositories/user_state_e2e_test.go +++ b/internal/repositories/user_state_e2e_test.go @@ -2,7 +2,7 @@ package repositories_test import ( "context" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/models" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/repositories/wire.go b/internal/repositories/wire.go index 6c956d2..e07e0c7 100644 --- a/internal/repositories/wire.go +++ b/internal/repositories/wire.go @@ -1,7 +1,7 @@ package repositories import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/db" + "github.com/WildEgor/e-shop-support-bot/internal/db" "github.com/google/wire" ) diff --git a/internal/router/bot_router.go b/internal/router/bot_router.go index 168eb14..3d06627 100644 --- a/internal/router/bot_router.go +++ b/internal/router/bot_router.go @@ -1,20 +1,20 @@ package router import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - accept_callback_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/accept_callback" - break_action_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/break_action" - decline_callback_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/decline_callback" - edit_message_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/edit_message" - new_message_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/new_message" - no_right_callback_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/not_right_callback" - rating_callback_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/rating_callback" - start_action_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/start_action" - middlewares2 "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/middlewares/auth" - middlewares "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/middlewares/group" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/repositories" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + accept_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/accept_callback" + break_action_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/break_action" + decline_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/decline_callback" + edit_message_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/edit_message" + new_message_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/new_message" + no_right_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/no_right_callback" + rating_callback_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/rating_callback" + start_action_handler "github.com/WildEgor/e-shop-support-bot/internal/handlers/start_action" + middlewares2 "github.com/WildEgor/e-shop-support-bot/internal/middlewares/auth" + middlewares "github.com/WildEgor/e-shop-support-bot/internal/middlewares/group" + "github.com/WildEgor/e-shop-support-bot/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" ) type BotRouter struct { @@ -63,7 +63,7 @@ func NewBotRouter( } } -func (r *BotRouter) SetupBotRouter() { +func (r *BotRouter) Setup() { gmw := middlewares.NewExtractGroupMiddleware(r.uor, r.gr, r.tcfg) amw := middlewares2.NewAuthMiddleware() diff --git a/internal/router/public_router.go b/internal/router/public_router.go index 59b4821..9c75ce3 100644 --- a/internal/router/public_router.go +++ b/internal/router/public_router.go @@ -1,8 +1,8 @@ package router import ( - hch "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/health_check" - rch "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ready_check" + hch "github.com/WildEgor/e-shop-support-bot/internal/handlers/health_check" + rch "github.com/WildEgor/e-shop-support-bot/internal/handlers/ready_check" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/healthcheck" "github.com/gofiber/fiber/v3/middleware/limiter" diff --git a/internal/router/wire.go b/internal/router/wire.go index afe7a02..5424be4 100644 --- a/internal/router/wire.go +++ b/internal/router/wire.go @@ -1,7 +1,7 @@ package router import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers" + "github.com/WildEgor/e-shop-support-bot/internal/handlers" "github.com/google/wire" ) @@ -10,4 +10,5 @@ var RouterSet = wire.NewSet( NewPublicRouter, NewPrivateRouter, NewSwaggerRouter, + NewBotRouter, ) diff --git a/internal/services/translator/translator.go b/internal/services/translator/translator.go index 996c634..eb726ff 100644 --- a/internal/services/translator/translator.go +++ b/internal/services/translator/translator.go @@ -1,8 +1,8 @@ package services import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + "github.com/WildEgor/e-shop-support-bot/internal/models" "github.com/kataras/i18n" "os" "reflect" diff --git a/internal/services/translator/translator_test.go b/internal/services/translator/translator_test.go index 679998e..d18dc86 100644 --- a/internal/services/translator/translator_test.go +++ b/internal/services/translator/translator_test.go @@ -1,9 +1,9 @@ package services_test import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/models" - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + "github.com/WildEgor/e-shop-support-bot/internal/models" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/services/wire.go b/internal/services/wire.go index 9bbb839..eae7fdf 100644 --- a/internal/services/wire.go +++ b/internal/services/wire.go @@ -1,7 +1,7 @@ package services import ( - services "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/services/translator" + services "github.com/WildEgor/e-shop-support-bot/internal/services/translator" "github.com/google/wire" ) diff --git a/internal/wire_gen.go b/internal/wire_gen.go index 585dd58..ad0639d 100644 --- a/internal/wire_gen.go +++ b/internal/wire_gen.go @@ -7,12 +7,24 @@ package pkg import ( - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/adapters/telegram" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/configs" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/errors" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/health_check" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ready_check" - "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/router" + "github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" + "github.com/WildEgor/e-shop-support-bot/internal/configs" + "github.com/WildEgor/e-shop-support-bot/internal/db/postgres" + "github.com/WildEgor/e-shop-support-bot/internal/db/redis" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/accept_callback" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/break_action" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/decline_callback" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/edit_message" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/errors" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/health_check" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/new_message" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/no_right_callback" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/rating_callback" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/ready_check" + "github.com/WildEgor/e-shop-support-bot/internal/handlers/start_action" + "github.com/WildEgor/e-shop-support-bot/internal/repositories" + "github.com/WildEgor/e-shop-support-bot/internal/router" + "github.com/WildEgor/e-shop-support-bot/internal/services/translator" "github.com/google/wire" ) @@ -26,12 +38,32 @@ func NewServer() (*Server, error) { healthCheckHandler := health_check_handler.NewHealthCheckHandler() readyCheckHandler := ready_check_handler.NewReadyCheckHandler() publicRouter := router.NewPublicRouter(healthCheckHandler, readyCheckHandler) - swaggerRouter := router.NewSwaggerRouter() telegramConfig := configs.NewTelegramConfig(configurator) telegramBotAdapter := telegram.NewTelegramBotAdapter(telegramConfig) telegramListener := telegram.NewTelegramListener(telegramBotAdapter) + redisConfig := configs.NewRedisConfig(configurator) + redisConnection := redis.NewRedisConnection(redisConfig) + userStateRepository := repositories.NewUserStateRepository(redisConnection) + translatorConfig := configs.NewTranslatorConfig(configurator) + translatorService, err := services.NewTranslatorService(translatorConfig) + if err != nil { + return nil, err + } + startActionHandler := start_action_handler.NewStartActionHandler(telegramBotAdapter, userStateRepository, translatorService) postgresConfig := configs.NewPostgresConfig(configurator) - server := NewApp(appConfig, errorsHandler, privateRouter, publicRouter, swaggerRouter, telegramListener, postgresConfig) + postgresConnection := postgres.NewPostgresConnection(postgresConfig) + topicRepository := repositories.NewTopicsRepository(redisConnection, postgresConnection) + breakActionHandler := break_action_handler.NewBreakActionHandler(telegramBotAdapter, translatorService, topicRepository, userStateRepository) + editMessageHandler := edit_message_handler.NewEditMessageHandler(telegramBotAdapter, userStateRepository) + acceptCallbackHandler := accept_callback_handler.NewAcceptCallbackHandler(telegramBotAdapter, translatorService, userStateRepository, topicRepository) + declineCallbackHandler := decline_callback_handler.NewDeclineCallbackHandler(telegramBotAdapter, translatorService, topicRepository, userStateRepository) + noRightCallbackHandler := no_right_callback_handler.NewNoRightCallbackHandler(translatorService, telegramBotAdapter, userStateRepository) + groupRepository := repositories.NewGroupRepository(redisConnection) + newMessageHandler := new_message_handler.NewNewMessageHandler(telegramBotAdapter, translatorService, topicRepository, userStateRepository, groupRepository) + ratingCallbackHandler := rating_callback_handler.NewRatingCallbackHandler(telegramBotAdapter, translatorService, userStateRepository, topicRepository) + botRouter := router.NewBotRouter(telegramConfig, telegramListener, startActionHandler, breakActionHandler, editMessageHandler, acceptCallbackHandler, declineCallbackHandler, noRightCallbackHandler, newMessageHandler, ratingCallbackHandler, userStateRepository, groupRepository) + swaggerRouter := router.NewSwaggerRouter() + server := NewApp(appConfig, errorsHandler, privateRouter, publicRouter, botRouter, swaggerRouter, telegramListener, postgresConfig) return server, nil }