nechotron/nechotron.go

55 lines
1.0 KiB
Go

package nechotron
import (
"log"
echo "github.com/NicoNex/echotron/v3"
)
type Nechotron struct {
Token string
DefaultState Runnable
ApiServer string
Handler UpdateHandler
}
func NewTron(token string, defaultState *State) *Nechotron {
state := defaultState
if state == nil {
state = &EchoState
}
return &Nechotron{
Token: token,
DefaultState: state,
Handler: DefaultHandler,
}
}
func (n *Nechotron) newBot(chatID int64) echo.Bot {
a := echo.NewAPI(n.Token)
// a := echo.NewAPI(n.Token)
me, _ := a.GetMe()
// log.Println("New bot active: ", chatID, me.Result)
b := &bot{
chatID: chatID,
Me: me.Result,
API: a,
State: n.DefaultState,
data: make(StateData),
handler: n.Handler,
}
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()
}
func (n *Nechotron) Use(mid Middleware) *Nechotron {
n.Handler = mid(n.Handler)
return n
}