added logic for set task for worker tg chanel creator and init redis in app, update openapi

This commit is contained in:
Pavel 2024-07-11 12:06:24 +03:00
parent 3eaf4999cc
commit 8f3c545b45
7 changed files with 122 additions and 32 deletions

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"github.com/skeris/appInit"
"github.com/themakers/hlog"
@ -23,7 +24,7 @@ import (
"penahub.gitlab.yandexcloud.net/backend/quiz/core/server"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/service"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/tools"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/worker"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/workers"
"penahub.gitlab.yandexcloud.net/external/trashlog/wrappers/zaptrashlog"
"time"
)
@ -71,7 +72,10 @@ type Options struct {
TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"`
ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"`
ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"`
TgBotToken string `env:"TG_BOT_TOKEN" default:"6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8"`
BotID int `env:"BOT_ID"` // 6712573453
RedisHost string `env:"REDIS_HOST" default:"localhost:6379"`
RedisPassword string `env:"REDIS_PASSWORD"`
RedisDB uint64 `env:"REDIS_DB" default:"2"`
}
func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) {
@ -146,6 +150,16 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
Logger: zapLogger,
})
redisClient := redis.NewClient(&redis.Options{
Addr: options.RedisHost,
Password: options.RedisPassword,
DB: int(options.RedisDB),
})
err = redisClient.Ping(ctx).Err()
if err != nil {
panic(fmt.Sprintf("error ping to redis db %v", err))
}
clientData := privilege.Client{
URL: options.HubAdminUrl,
ServiceName: options.ServiceName,
@ -160,12 +174,13 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
panic(fmt.Sprintf("failed init tg clietns: %v", err))
}
tgWC, err := worker.NewTelegramBotWorker(options.TgBotToken)
if err != nil {
panic(fmt.Sprintf("failed init tg listener WC: %v", err))
}
tgWC := workers.NewTgListenerWC(workers.Deps{
BotID: int64(options.BotID),
Redis: redisClient,
Dal: pgdal,
})
go tgWC.Run()
go tgWC.Start(ctx)
// todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время
// сложно будет разобраться что есть где
@ -194,6 +209,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
ServiceName: options.ServiceName,
ChDAL: chDal,
TelegramClient: tgClient,
RedisClient: redisClient,
})
svc.Register(app)

7
go.mod

@ -3,6 +3,7 @@ module penahub.gitlab.yandexcloud.net/backend/quiz/core
go 1.22.4
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/gofiber/fiber/v2 v2.52.4
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
@ -18,9 +19,8 @@ require (
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
gopkg.in/tucnak/telebot.v2 v2.5.0
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990
penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c
@ -34,8 +34,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
@ -49,7 +47,6 @@ require (
github.com/minio/minio-go/v7 v7.0.70 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect

7
go.sum

@ -36,8 +36,6 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM=
@ -138,7 +136,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
@ -288,8 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b h1:rgQIQINsnv4nsTzLXynvFD5u5Wcq8qwWindI4pi1qGA=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o=
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE=
penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008=

@ -787,6 +787,8 @@ components:
format: int32
Target:
type: string
InviteLink:
type: string
Deleted:
type: boolean
CreatedAt:
@ -1636,14 +1638,17 @@ paths:
description: ID квиза, к которому прикреплено это правило (приоритет). Передавать как 0, если правило не прикрепляется к квизу и является общим.
target:
type: string
description: Адресат, куда конкретно слать (для mail - email, для telegram - ID чата, для whatsapp - номер телефона, наверное).
description: Адресат, куда конкретно слать (для mail - email, для telegram - ID канала, передавать не нужно канал сам создаться, для whatsapp - номер телефона, наверное).
name:
type: string
description: имя например для тг канала
responses:
'200':
description: ОК, парвило добавлено
content:
application/json:
schema:
$ref: '#/components/schemas/LeadTarget'
description: ОК, парвило добавлено если тип mail о сразу добавляется если тг то будет добавленно в воркере если ватсап пока тодо
# content:
# application/json:
# schema:
# $ref: '#/components/schemas/LeadTarget'
'400':
description: Bad request, ошибка в теле запроса
content:

@ -2,7 +2,9 @@ package service
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
"penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
@ -251,6 +253,7 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error {
Type string `json:"type"`
QuizID int32 `json:"quizID"`
Target string `json:"target"`
Name string `json:"name"`
}
if err := ctx.BodyParser(&req); err != nil {
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
@ -269,17 +272,39 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error {
return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil")
}
result, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
AccountID: accountID,
Target: req.Target,
Type: model.LeadTargetType(req.Type),
QuizID: req.QuizID,
})
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
switch req.Type {
case "mail":
_, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
AccountID: accountID,
Target: req.Target,
Type: model.LeadTargetType(req.Type),
QuizID: req.QuizID,
})
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return ctx.SendStatus(fiber.StatusOK)
case "telegram":
task := model.TgRedisTask{
Name: req.Name,
QuizID: req.QuizID,
AccountID: accountID,
}
taskKey := fmt.Sprintf("telegram_task:%d", time.Now().UnixNano())
taskData, err := json.Marshal(task)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Failed to marshal task")
}
if err := s.redisClient.Set(ctx.Context(), taskKey, taskData, 0).Err(); err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
case "whatsapp":
return ctx.Status(fiber.StatusOK).SendString("todo")
}
return ctx.Status(fiber.StatusOK).JSON(result)
return nil
}
func (s *Service) DeleteLeadTarget(ctx *fiber.Ctx) error {

@ -1,6 +1,7 @@
package service
import (
"github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers"
@ -16,6 +17,7 @@ type Service struct {
serviceName string
chDAL *dal.ClickHouseDAL
telegramClient *telegram.TelegramClient
redisClient *redis.Client
}
type Deps struct {
@ -25,6 +27,7 @@ type Deps struct {
ServiceName string
ChDAL *dal.ClickHouseDAL
TelegramClient *telegram.TelegramClient
RedisClient *redis.Client
}
func New(deps Deps) *Service {
@ -35,6 +38,7 @@ func New(deps Deps) *Service {
serviceName: deps.ServiceName,
chDAL: deps.ChDAL,
telegramClient: deps.TelegramClient,
redisClient: deps.RedisClient,
}
}

46
workers/tg_worker.go Normal file

@ -0,0 +1,46 @@
package workers
import (
"context"
"github.com/go-redis/redis/v8"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"time"
)
type Deps struct {
BotID int64
Redis *redis.Client
Dal *dal.DAL
}
type TgListenerWorker struct {
botID int64
redis *redis.Client
dal *dal.DAL
}
func NewTgListenerWC(deps Deps) *TgListenerWorker {
return &TgListenerWorker{
botID: deps.BotID,
redis: deps.Redis,
dal: deps.Dal,
}
}
func (wc *TgListenerWorker) Start(ctx context.Context) {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
wc.processTasks(ctx)
case <-ctx.Done():
return
}
}
}
func (wc *TgListenerWorker) processTasks(ctx context.Context) {
}