2022-06-03 13:58:41 +03:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.belvedersky.ru/common/logger/service"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/puzpuzpuz/xsync"
|
|
|
|
"gopkg.in/telebot.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bots struct {
|
|
|
|
log *service.LoggerService // Логгер
|
|
|
|
I *xsync.MapOf[*Bot]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMap(Log *service.LoggerService) *Bots {
|
|
|
|
return &Bots{
|
|
|
|
log: Log,
|
|
|
|
I: xsync.NewMapOf[*Bot](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:51:38 +03:00
|
|
|
// Get godoc
|
|
|
|
// @Summary Получение бота
|
|
|
|
// @Description Получение бота по username
|
|
|
|
// @ID bots/get
|
|
|
|
// @Tags Боты
|
|
|
|
// @Param name path string true "Username бота"
|
|
|
|
// @Router /bot/{name} [get]
|
|
|
|
// @Success 200 {object} telebot.User
|
2022-06-03 13:58:41 +03:00
|
|
|
func (b *Bots) Get(c *fiber.Ctx) error {
|
|
|
|
name := c.Params("name")
|
|
|
|
if name != "" {
|
|
|
|
b.log.Log.Println("Запрос на получение бота", name)
|
|
|
|
_b, ok := b.I.Load(name)
|
|
|
|
if !ok {
|
|
|
|
botId, err := strconv.Atoi(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.I.Range(func(key string, bot *Bot) bool {
|
|
|
|
if bot.Self().ID == int64(botId) {
|
|
|
|
_b = bot
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if _b == nil {
|
|
|
|
return c.Status(404).SendString("not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.JSON(_b.Self())
|
|
|
|
}
|
|
|
|
return c.Status(404).SendString("not found")
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:51:38 +03:00
|
|
|
// Get godoc
|
|
|
|
// @Summary Получение списка ботов
|
|
|
|
// @Description Получение списка ботов
|
|
|
|
// @ID bots/list
|
|
|
|
// @Tags Боты
|
|
|
|
// @Router /bot/list [get]
|
|
|
|
// @Success 200 {array} telebot.User
|
|
|
|
func (b *Bots) List(c *fiber.Ctx) error {
|
2022-06-03 13:58:41 +03:00
|
|
|
bots := []*telebot.User{}
|
|
|
|
b.I.Range(func(key string, value *Bot) bool {
|
|
|
|
bot := value
|
|
|
|
bots = append(bots, bot.Self())
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return c.JSON(bots)
|
|
|
|
}
|