nechotron/bot.go

47 lines
777 B
Go
Raw Normal View History

2023-01-19 23:08:59 +03:00
package nechotron
import (
"context"
2023-01-20 00:27:29 +03:00
"sync"
2023-01-19 23:08:59 +03:00
"time"
echo "github.com/NicoNex/echotron/v3"
"github.com/google/uuid"
)
type bot struct {
echo.API
2023-01-24 00:37:08 +03:00
chatID int64
2023-02-10 16:59:42 +03:00
Me *echo.User
2023-01-24 00:37:08 +03:00
data StateData
lock sync.Mutex
2023-02-10 16:59:42 +03:00
State Runnable
2023-01-24 00:37:08 +03:00
handler UpdateHandler
}
func DefaultHandler(u *Update) error {
2023-02-10 16:59:42 +03:00
err := u.Bot.State.Call(u)
2023-01-24 00:37:08 +03:00
return err
2023-01-19 23:08:59 +03:00
}
func (b *bot) Update(u *echo.Update) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
upd := &Update{
U: U(*u),
Bot: b,
UpdateID: uuid.New(),
Ctx: ctx,
}
2023-01-20 00:27:29 +03:00
b.lock.Lock()
defer b.lock.Unlock()
// defer func() {
// if r := recover(); r != nil {
// log.Printf("[%s] Recovered. Error: %s\n", upd.UpdateID.String(), r)
// }
// }()
2023-01-24 00:37:08 +03:00
b.handler(upd)
2023-01-19 23:08:59 +03:00
}