nechotron/nechotron.go

55 lines
1.0 KiB
Go
Raw Normal View History

2023-01-19 23:08:59 +03:00
package nechotron
import (
"log"
echo "github.com/NicoNex/echotron/v3"
)
type Nechotron struct {
Token string
2023-01-20 16:32:13 +03:00
DefaultState Runnable
ApiServer string
2023-01-24 00:37:08 +03:00
Handler UpdateHandler
2023-01-19 23:08:59 +03:00
}
func NewTron(token string, defaultState *State) *Nechotron {
state := defaultState
if state == nil {
state = &EchoState
}
return &Nechotron{
Token: token,
DefaultState: state,
2023-01-24 00:37:08 +03:00
Handler: DefaultHandler,
2023-01-19 23:08:59 +03:00
}
}
func (n *Nechotron) newBot(chatID int64) echo.Bot {
a := echo.NewAPI(n.Token)
2023-01-20 16:32:13 +03:00
// a := echo.NewAPI(n.Token)
2023-01-19 23:08:59 +03:00
me, _ := a.GetMe()
// log.Println("New bot active: ", chatID, me.Result)
b := &bot{
2023-01-24 00:37:08 +03:00
chatID: chatID,
me: me.Result,
API: a,
state: n.DefaultState,
data: make(StateData),
handler: n.Handler,
2023-01-19 23:08:59 +03:00
}
b.state = n.DefaultState
return b
}
func (n *Nechotron) DispatchPoll() error {
dispatcher := echo.NewDispatcher(n.Token, n.newBot)
log.Println("Nechotron poll dispatcher started")
return dispatcher.Poll()
}
2023-01-24 00:37:08 +03:00
func (n *Nechotron) Use(mid Middleware) *Nechotron {
n.Handler = mid(n.Handler)
return n
}