50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git/ecom/jira-bot/pkg/utils"
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/go-redis/redis/v8"
|
|
telegram "gopkg.in/tucnak/telebot.v2"
|
|
)
|
|
|
|
func RedisSession(redis *redis.Client, ctx context.Context) *telegram.MiddlewarePoller {
|
|
poller := utils.NewPooler(10)
|
|
session := telegram.NewMiddlewarePoller(poller, func(upd *telegram.Update) bool {
|
|
userid := strconv.Itoa(upd.Message.Sender.ID) + ":" + strconv.Itoa(int(upd.Message.Chat.ID))
|
|
log.Println(userid, upd.Message.Chat.FirstName, ":", upd.Message.Text)
|
|
exist, err := redis.HExists(ctx, "users", userid).Result()
|
|
if err != nil {
|
|
log.Println("Ошибка проверки пользователя", err.Error())
|
|
}
|
|
if exist {
|
|
return true
|
|
}
|
|
|
|
color.Green("Новый пользователь!")
|
|
user, err := json.Marshal(utils.User{
|
|
Id: upd.Message.Sender.ID,
|
|
Role: utils.Client,
|
|
Username: upd.Message.Sender.Username,
|
|
Name: upd.Message.Sender.FirstName + " " + upd.Message.Sender.LastName,
|
|
})
|
|
|
|
if err != nil {
|
|
log.Println("Ошибка Marshal:", err.Error())
|
|
}
|
|
|
|
err = redis.HSet(ctx, "users", userid, user).Err()
|
|
|
|
if err != nil {
|
|
log.Println("Ошибка:", err.Error())
|
|
}
|
|
|
|
return true
|
|
})
|
|
return session
|
|
}
|