Skip to content

Commit

Permalink
Routing: Handle() and Endpoint introduced.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Byrd committed Nov 21, 2017
1 parent ba575e7 commit 0cf9b9a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
37 changes: 31 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"strconv"

"github.com/armon/go-radix"
"github.com/pkg/errors"
)

// Bot represents a separate Telegram bot instance.
type Bot struct {
Token string
Identity *User
Token string
Me *User

Updates chan Update
Messages chan Message
Expand All @@ -23,7 +22,10 @@ type Bot struct {
// will use it to report all occuring errors.
Errors chan error

tree *radix.Tree
// Poller is the update provider.
Poller Poller

handlers map[string]interface{}
}

// NewBot does try to build a Bot with token `token`, which
Expand All @@ -36,7 +38,9 @@ func NewBot(pref Settings) (*Bot, error) {
bot := &Bot{
Token: pref.Token,
Updates: make(chan Update, pref.Updates),
tree: radix.New(),
Poller: pref.Poller,

handlers: make(map[string]interface{}),
}

if pref.Messages != 0 {
Expand All @@ -56,7 +60,7 @@ func NewBot(pref Settings) (*Bot, error) {
return nil, err
}

bot.Identity = user
bot.Me = user
return bot, nil
}

Expand Down Expand Up @@ -95,9 +99,30 @@ type Update struct {
// Start brings bot into motion by consuming incoming
// updates (see Bot.Updates channel).
func (b *Bot) Start() {
if b.Poller == nil {
panic("telebot: can't start without a poller")
}

go b.Poller.Poll(b, b.Updates)

if b.Messages != nil {
go b.handleMessages(b.Messages)
}

if b.Queries != nil {
go b.handleQueries(b.Queries)
}

if b.Callbacks != nil {
go b.handleCallbacks(b.Callbacks)
}

fmt.Println("start ranging...")
for upd := range b.Updates {
fmt.Println(upd)
if b.Messages != nil {
if upd.Message != nil {
fmt.Println("receiving 1:", upd.Message.Text)
b.Messages <- *upd.Message
continue
}
Expand Down
76 changes: 76 additions & 0 deletions handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package telebot

import (
"regexp"
"strings"
)

// Handle lets you set the handler for some command name or
// one of the supported endpoints.
//
// See Endpoint.
func (b *Bot) Handle(endpoint, handler interface{}) {
if cmd, ok := endpoint.(string); ok {
b.handlers[cmd] = handler

} else if end, ok := endpoint.(Endpoint); ok {
b.handlers[string(end)] = handler

} else {
panic("Handle() only supports patterns and endpoints")
}
}

var cmdRx = regexp.MustCompile(`^\/(\w+)(@(\w+))?`)

func (b *Bot) handleMessages(messages chan Message) {
for m := range messages {
// Text message
if m.Text != "" {
match := cmdRx.FindAllStringSubmatch(m.Text, -1)

// Command found
if match != nil {
if b.handleCommand(&m, match[0][1], match[0][3]) {
continue
}
}

// Feeding it to OnMessage if one exists.
if handler, ok := b.handlers[string(OnMessage)]; ok {
if handler, ok := handler.(func(*Message)); ok {
go handler(&m)
continue
}
}
}
}
}

func (b *Bot) handleCommand(m *Message, cmdName, cmdBot string) bool {
// Group-syntax: "/cmd@bot"
if cmdBot != "" && !strings.EqualFold(b.Me.Username, cmdBot) {
return false
}

if handler, ok := b.handlers[cmdName]; ok {
if handler, ok := handler.(func(*Message)); ok {
go handler(m)
return true
}
}

return false
}

func (b *Bot) handleQueries(queries chan Query) {
for _ = range queries {

}
}

func (b *Bot) handleCallbacks(callbacks chan Callback) {
for _ = range callbacks {

}
}
2 changes: 1 addition & 1 deletion poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
//
// All pollers must implement Poll(), which accepts bot
// pointer and subscription channel and start polling
// asynchronously straight away.
// synchronously straight away.
type Poller interface {
// Poll is supposed to take the bot object
// subscription channel and start polling
Expand Down
15 changes: 15 additions & 0 deletions telebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@
//
package telebot

// Endpoint is one of the possible events Handle() can deal with.
//
// For convenience, all Telebot-provided endpoints start with
// an "alert" character \a.
type Endpoint string

const (
OnMessage Endpoint = "\amessage"
OnEditedMessage Endpoint = "\aedited_msg"
OnQuery Endpoint = "\aquery"
OnCallback Endpoint = "\acallback"
OnChannelPost Endpoint = "\achan_post"
OnEditedChannelPost Endpoint = "\achan_post"
)

// ChatAction is a client-side status indicating bot activity.
type ChatAction string

Expand Down

0 comments on commit 0cf9b9a

Please sign in to comment.