52 lines
773 B
Go
52 lines
773 B
Go
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()
|
|
}
|