init code

This commit is contained in:
thuanle
2024-10-24 09:53:23 +07:00
parent a7559b3f9d
commit 92a63c7885
43 changed files with 1115 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package tele
import (
"github.com/rs/zerolog/log"
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/configs/key"
"os"
"sync"
"time"
)
type Service struct {
b *telebot.Bot
}
var (
ts *Service
once sync.Once
)
func Ins() *Service {
if ts != nil {
return ts
}
once.Do(func() {
pref := telebot.Settings{
Token: os.Getenv(key.TelegramToken),
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
}
b, err := telebot.NewBot(pref)
if err != nil {
log.Fatal().Err(err).Msg("Failed to create bot")
}
err = setupCommands(b)
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup commands")
}
ts = &Service{
b: b,
}
})
return ts
}
func (t *Service) Start() {
log.Info().Msg("Starting telegram service")
t.b.Start()
}