Skip to content

Commit

Permalink
fix: README.md, package name, deps in wire
Browse files Browse the repository at this point in the history
  • Loading branch information
WildEgor committed May 4, 2024
1 parent ae29cdc commit b9e9f5f
Show file tree
Hide file tree
Showing 49 changed files with 382 additions and 117 deletions.
1 change: 1 addition & 0 deletions .air-unix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
19 changes: 17 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
APP_NAME=bot
APP_MODE=develop
APP_PORT=8080
APP_NAME=app
APP_MODE=develop

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:[email protected]: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
2 changes: 2 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_NAME=bot
DEFAULT_LOCALE=ru-RU
36 changes: 36 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
name: hadolint for Dockerfile
with:
dockerfile: Dockerfile
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 85 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
# e-shop-support-bot
TODO
<h1 align="center"> eShop - support bot </h1> <br>
<hr>
<p align="center">
Microservice handles support via Telegram Bot
</p>
<hr>

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Run Lint and Testing](https://github.com/WildEgor/e-shop-support-bot/actions/workflows/lint.yml/badge.svg)](https://github.com/WildEgor/e-shop-support-bot/actions/workflows/lint.yml)
[![codecov](https://codecov.io/gh/WildEgor/e-shop-support-bot/branch/develop/graph/badge.svg)](https://codecov.io/gh/WildEgor/e-shop-support-bot/branch/develop)
[![Go Report Card](https://goreportcard.com/badge/github.com/WildEgor/e-shop-support-bot)](https://goreportcard.com/report/github.com/WildEgor/e-shop-support-bot)

## Table of Contents
- [Introduction](#introduction)
- [Features](#features)
- [Requirements](#requirements)
- [Quick Start](#quick-start)
- [Contributing](#contributing)

## Introduction

The service allows receiving messages from users of the chatbot with inquiries and creating a "ticket" for the support.
Support agents can receive feedback gathered for analytics.

## Features

- **Question Submission**: Users can submit questions through the Telegram bot interface;
- **Question Processing**: Incoming questions stored in a database and processed by support group;
- **Analytics**: Optionally collects statistics like feedbacks for insights into user behavior;

## Requirements

- [Git](http://git-scm.com/)
- [Go >= 1.22](https://go.dev/dl/)
- [Docker](https://www.docker.com/products/docker-desktop/)
- [Postgres and Redis](https://github.com/WildEgor/e-shop-dot/blob/develop/docker-compose.yaml)

## Quick start

1. Создайте бота с помощью [BotFather](https://t.me/BotFather) ([video instruction](https://www.youtube.com/watch?v=UQrcOj63S2o))
For example, [eShopSupport](t.me/eshop_support_bot)
2. Run application locally
Clone repository, modify environment and run it
```shell
git pull https://github.com/WildEgor/e-shop-support-bot &&
cd e-shop-support-bot &&
cp .env.example .env &&
go mod download &&
go run cmd/main.go
```
or using Docker
```shell
docker-compose up bot
```

3. Create group in Telegram, add bot and make it admin. Next you can check saved group id using redis-cli
![img.png](assets/img.png)

In a support group, you will receive "tickets" with user questions. Group users can "accept" or "reject" a ticket. If a ticket is "rejected," a new one can be created.
![img.png](assets/img1.png)

If the ticket is "accepted," users can discuss the issue through messages to the bot. Messages "duplicate" because I'm testing the functionality with the same account (pay attention to the sender of the message).
![img.png](assets/img2.png)

You can find "tickets" in the database table ```public.topics```
![img.png](assets/img3.png)

## Contributing

Please, use git cz for commit messages!

```shell
git clone https://github.com/WildEgor/e-shop-support-bot
cd e-shop-support-bot
git checkout -b feature-or-fix-branch
git add .
git cz
git push --set-upstream-to origin/feature-or-fix-branch
```

## License

<p>This project is licensed under the <a href="LICENSE">MIT License</a>.</p>

Made with ❤️ by me
Binary file added assets/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions internal/adapters/telegram/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -24,16 +24,15 @@ 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,
}))
panic(err)
}

bot.Debug = cfg.Debug

return &TelegramBotAdapter{
bot: bot,
handler: func(ctx context.Context, update tgbotapi.Update) {},
Expand Down
16 changes: 14 additions & 2 deletions internal/adapters/telegram/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand All @@ -74,18 +74,30 @@ 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)
}

// handleUpdates catch and route update to handlers
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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/wire.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down
14 changes: 8 additions & 6 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewApp(
}))
app.Use(recover.New())

br.Setup()
prr.Setup(app)
pbr.Setup(app)
sr.Setup(app)
Expand Down
3 changes: 2 additions & 1 deletion internal/configs/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/db/redis/conn.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
Loading

0 comments on commit b9e9f5f

Please sign in to comment.