Endpoints for media introduced, Message.From -> Message.Sender

pull/108/head
Ian Byrd 7 years ago
parent 612ab345bd
commit 79dccd7227
No known key found for this signature in database
GPG Key ID: 598F598CA3B8055F

@ -35,7 +35,7 @@ func main() {
}
b.Handle("/hello", func(m *tb.Message) {
b.Send(m.From, "hello world")
b.Send(m.Sender, "hello world")
})
b.Start()
@ -50,6 +50,15 @@ and you're ready to go!
```go
b, _ := tb.NewBot(settings)
b.Handle(tb.OnText, func(m *Message) {
// all text messages that weren't captured
// by existing handlers
}
b.Handle(tb.OnPhoto, func(m *Message) {
// photos only
}
b.Handle("/help", func (m *Message) {
// help command handler
})
@ -62,14 +71,9 @@ b.Handle(tb.Callback, func (c *Callback) {
// incoming bot callbacks that weren't
// captured by specific callback handlers.
})
b.Handle(tb.OnMessage, func(m *Message) {
// all messages that couldn't fall into
// any specific endpoint will get here.
}
```
Now there's about 10 supported endpoints (see package consts). Let me know
Now there's a dozen of supported endpoints (see package consts). Let me know
if you'd like to see some endpoint or endpoint idea implemented. This system
is completely extensible, so I can introduce them without braking
backwards-compatibity.

127
bot.go

@ -75,9 +75,9 @@ type Update struct {
//
// Example:
//
// tb.Handle("/help", func (m *tb.Message) {})
// tb.Handle(tb.OnEditedMessage, func (m *tb.Message) {})
// tb.Handle(tb.OnQuery, func (q *tb.Query) {})
// tb.b.handle("/help", func (m *tb.Message) {})
// tb.b.handle(tb.OnEditedMessage, func (m *tb.Message) {})
// tb.b.handle(tb.OnQuery, func (q *tb.Query) {})
//
func (b *Bot) Handle(endpoint string, handler interface{}) {
b.handlers[endpoint] = handler
@ -114,70 +114,52 @@ func (b *Bot) Start() {
if upd.Message != nil {
m := upd.Message
// Text message
// Commands
if m.Text != "" {
// Filtering malicious messsages
if m.Text[0] == '\a' {
continue
}
match := cmdRx.FindAllStringSubmatch(m.Text, -1)
// Command found
if match != nil {
if b.handleCommand(m, match[0][1], match[0][3]) {
continue
}
// Command found - handle and continue
if match != nil && b.handleCommand(m, match[0][1], match[0][3]) {
continue
}
}
wasAdded := m.NewChatMembers != nil &&
isUserInList(b.Me, m.NewChatMembers)
if m.ChatCreated || wasAdded {
if handler, ok := b.handlers[string(OnAddedToGroup)]; ok {
if handler, ok := handler.(func(*Message)); ok {
go handler(m)
continue
}
}
// OnText
b.handle(OnText, m)
continue
}
// on media
if b.handleMedia(m) {
continue
}
// OnMessage
if handler, ok := b.handlers[string(OnMessage)]; ok {
if handler, ok := handler.(func(*Message)); ok {
go handler(m)
continue
}
// OnAddedToGrop
wasAdded := m.NewChatMembers != nil && isUserInList(b.Me, m.NewChatMembers)
if m.GroupCreated || wasAdded {
b.handle(OnAddedToGroup, m)
continue
}
continue
}
if upd.EditedMessage != nil {
if handler, ok := b.handlers[OnEditedMessage]; ok {
if handler, ok := handler.(func(*Message)); ok {
handler(upd.EditedMessage)
}
}
b.handle(OnEditedMessage, upd.EditedMessage)
continue
}
if upd.ChannelPost != nil {
if handler, ok := b.handlers[OnChannelPost]; ok {
if handler, ok := handler.(func(*Message)); ok {
handler(upd.ChannelPost)
}
}
b.handle(OnChannelPost, upd.ChannelPost)
continue
}
if upd.EditedChannelPost != nil {
if handler, ok := b.handlers[OnEditedChannelPost]; ok {
if handler, ok := handler.(func(*Message)); ok {
handler(upd.EditedChannelPost)
}
}
b.handle(OnEditedChannelPost, upd.EditedChannelPost)
continue
}
@ -201,6 +183,69 @@ func (b *Bot) Start() {
}
}
func (b *Bot) handle(end string, m *Message) bool {
handler, ok := b.handlers[end]
if !ok {
return false
}
if handler, ok := handler.(func(*Message)); ok {
go handler(m)
return true
} else {
return false
}
}
func (b *Bot) handleMedia(m *Message) bool {
if m.Photo != nil {
b.handle(OnPhoto, m)
return true
}
if m.Audio != nil {
b.handle(OnAudio, m)
return true
}
if m.Document != nil {
b.handle(OnDocument, m)
return true
}
if m.Sticker != nil {
b.handle(OnSticker, m)
return true
}
if m.Video != nil {
b.handle(OnVideo, m)
return true
}
if m.VideoNote != nil {
b.handle(OnVideoNote, m)
return true
}
if m.Contact != nil {
b.handle(OnContact, m)
return true
}
if m.Location != nil {
b.handle(OnLocation, m)
return true
}
if m.Venue != nil {
b.handle(OnVenue, m)
return true
}
return false
}
// Send accepts 2+ arguments, starting with destination chat, followed by
// some Sendable (or string!) and optional send options.
//

@ -9,12 +9,12 @@ type Message struct {
ID int `json:"message_id"`
// For message sent to channels, Sender will be nil
From *User `json:"from"`
Sender *User `json:"from"`
// Unixtime, use Message.Time() to get time.Time
Unixtime int64 `json:"date"`
// A group chat message belongs to.
// Conversation the message belongs to.
Chat *Chat `json:"chat"`
// (Optional) Time of last edit in Unix
@ -81,6 +81,9 @@ type Message struct {
// For a location, its longitude and latitude.
Location *Location `json:"location"`
// For a venue, information about it.
Venue *Venue `json:"venue"`
// For a service message, represents a user,
// that just got added to chat, this message came from.
//
@ -127,7 +130,7 @@ type Message struct {
// initial group chat members.
//
// Sender would lead to creator of the chat.
ChatCreated bool `json:"group_chat_created"`
GroupCreated bool `json:"group_chat_created"`
// For a service message, true if super group has been created.
//
@ -236,13 +239,12 @@ func (m *Message) FromChannel() bool {
func (m *Message) IsService() bool {
fact := false
fact = fact || (m.UserJoined != nil)
fact = fact || (m.UserLeft != nil)
fact = fact || (m.NewChatTitle != "")
fact = fact || (len(m.NewChatPhoto) > 0)
fact = fact || m.UserJoined != nil
fact = fact || m.UserLeft != nil
fact = fact || m.NewChatTitle != ""
fact = fact || len(m.NewChatPhoto) > 0
fact = fact || m.ChatPhotoDeleted
fact = fact || m.ChatCreated
fact = fact || m.SuperGroupCreated
fact = fact || m.GroupCreated || m.SuperGroupCreated
fact = fact || (m.MigrateTo != m.MigrateFrom)
return fact

@ -18,7 +18,7 @@
// }
//
// b.Handle(tb.OnMessage, func(m *tb.Message) {
// b.Send(m.From, "hello world")
// b.Send(m.Sender, "hello world")
// }
//
// b.Start()
@ -31,13 +31,23 @@ package telebot
// For convenience, all Telebot-provided endpoints start with
// an "alert" character \a.
const (
// Basic generic message handlers.
// Basic message handlers.
//
// Handler: func(*Message)
OnMessage = "\amessage"
OnText = "\atext"
OnPhoto = "\aphoto"
OnAudio = "\aaudio"
OnDocument = "\adocument"
OnSticker = "\asticker"
OnVideo = "\avideo"
OnVoice = "\avoice"
OnVideoNote = "\avideo_note"
OnContact = "\acontact"
OnLocation = "\alocation"
OnVenue = "\avenue"
OnEditedMessage = "\aedited_msg"
OnChannelPost = "\achan_post"
OnEditedChannelPost = "\achan_post"
OnEditedChannelPost = "\achan_edited_post"
// Will fire on callback requests.
//
@ -49,7 +59,8 @@ const (
// Handler: func(*Query)
OnQuery = "\aquery"
// Will fire when bot gets added to some chat,
// Will fire when bot gets added to some group,
// including ones just created.
//
// Handler: func(*Message)
OnAddedToGroup = "\aadded_to_group"

Loading…
Cancel
Save