aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-13 20:39:39 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-13 20:39:39 +0300
commit9ea34f2a2e7aa0249b73b5d9bb05795a668f270d (patch)
treebca37e243209d8371e6a261b214bd0855ff4cad4
parent3da482d48583a697e9cd0dab44d3147c9bc19387 (diff)
Improve longpolling
Added chatID and userID to UpdateInterface
-rw-r--r--api.go52
-rw-r--r--examples/example_longpolling.go32
-rw-r--r--models.go108
3 files changed, 136 insertions, 56 deletions
diff --git a/api.go b/api.go
index 7078665..aa91a48 100644
--- a/api.go
+++ b/api.go
@@ -21,7 +21,6 @@ type Api struct {
Subscriptions *subscriptions
Uploads *uploads
client *client
- updates chan UpdateInterface
timeout int
pause int
}
@@ -38,7 +37,6 @@ func New(key string) *Api {
Messages: newMessages(cl),
Subscriptions: newSubscriptions(cl),
client: cl,
- updates: make(chan UpdateInterface),
timeout: timeout,
pause: 1,
}
@@ -172,33 +170,35 @@ func (a *Api) getUpdates(limit int, timeout int, marker int, types []string) (*U
return result, json.Unmarshal(jb, result)
}
-func (a *Api) UpdatesLoop(ctx context.Context) error {
- for {
- select {
- case <-ctx.Done():
- return nil
- case <-time.After(time.Duration(a.pause) * time.Second):
- var marker int
- for {
- upds, err := a.getUpdates(50, a.timeout, marker, []string{})
- if err != nil {
- return err
- }
- if len(upds.Updates) == 0 {
- break
- }
- for _, u := range upds.Updates {
- a.updates <- a.bytesToProperUpdate(u)
+//GetUpdates returns updates channel
+func (a *Api) GetUpdates(ctx context.Context) chan UpdateInterface {
+ ch := make(chan UpdateInterface)
+ go func() {
+ for {
+ select {
+ case <-ctx.Done():
+ close(ch)
+ return
+ case <-time.After(time.Duration(a.pause) * time.Second):
+ var marker int
+ for {
+ upds, err := a.getUpdates(50, a.timeout, marker, []string{})
+ if err != nil {
+ log.Println(err)
+ break
+ }
+ if len(upds.Updates) == 0 {
+ break
+ }
+ for _, u := range upds.Updates {
+ ch <- a.bytesToProperUpdate(u)
+ }
+ marker = *upds.Marker
}
- marker = *upds.Marker
}
}
- }
-}
-
-//GetUpdates returns updates channel
-func (a *Api) GetUpdates() chan UpdateInterface {
- return a.updates
+ }()
+ return ch
}
//GetHandler returns http handler for webhooks
diff --git a/examples/example_longpolling.go b/examples/example_longpolling.go
index 67a0d7c..3dbc66b 100644
--- a/examples/example_longpolling.go
+++ b/examples/example_longpolling.go
@@ -25,23 +25,17 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
- for {
- select {
- case upd := <-api.GetUpdates():
- log.Printf("Received: %#v", upd)
- switch upd := upd.(type) {
- case *tamtam.MessageCreatedUpdate:
- res, err := api.Messages.SendMessage(0, upd.Message.Sender.UserId, &tamtam.NewMessageBody{
- Text: fmt.Sprintf("Hello, %s! Your message: %s", upd.Message.Sender.Name, upd.Message.Body.Text),
- })
- log.Printf("Answer: %#v %#v", res, err)
- default:
- log.Printf("Unknown type: %#v", upd)
- }
- case <-ctx.Done():
- return
+ for upd := range api.GetUpdates(ctx) {
+ log.Printf("Received: %#v", upd)
+ switch upd := upd.(type) {
+ case *tamtam.MessageCreatedUpdate:
+ res, err := api.Messages.SendMessage(0, upd.Message.Sender.UserId, &tamtam.NewMessageBody{
+ Text: fmt.Sprintf("Hello, %s! Your message: %s", upd.Message.Sender.Name, upd.Message.Body.Text),
+ })
+ log.Printf("Answer: %#v %#v", res, err)
+ default:
+ log.Printf("Unknown type: %#v", upd)
}
-
}
}()
go func() {
@@ -54,9 +48,5 @@ func main() {
return
}
}()
-
- if err := api.UpdatesLoop(ctx); err != nil {
- log.Fatalln(err)
- }
-
+ <-ctx.Done()
}
diff --git a/models.go b/models.go
index 000d520..f449958 100644
--- a/models.go
+++ b/models.go
@@ -365,15 +365,15 @@ type UpdateType string
const (
TypeMessageCallback UpdateType = "message_callback"
- TypeMessageCreated = "message_created"
- TypeMessageRemoved = "message_removed"
- TypeMessageEdited = "message_edited"
- TypeBotAdded = "bot_added"
- TypeBotRemoved = "bot_removed"
- TypeUserAdded = "user_added"
- TypeUserRemoved = "user_removed"
- TypeBotStarted = "bot_started"
- TypeChatTitleChanged = "chat_title_changed"
+ TypeMessageCreated UpdateType = "message_created"
+ TypeMessageRemoved UpdateType = "message_removed"
+ TypeMessageEdited UpdateType = "message_edited"
+ TypeBotAdded UpdateType = "bot_added"
+ TypeBotRemoved UpdateType = "bot_removed"
+ TypeUserAdded UpdateType = "user_added"
+ TypeUserRemoved UpdateType = "user_removed"
+ TypeBotStarted UpdateType = "bot_started"
+ TypeChatTitleChanged UpdateType = "chat_title_changed"
)
// MessageLinkType : Type of linked message
@@ -613,6 +613,8 @@ func (u Update) GetUpdateTime() time.Time {
type UpdateInterface interface {
GetUpdateType() UpdateType
GetUpdateTime() time.Time
+ GetUserID() int
+ GetChatID() int
}
// You will receive this update when bots has been added to chat
@@ -622,6 +624,14 @@ type BotAddedToChatUpdate struct {
User User `json:"user"` // User who added bots to chat
}
+func (b BotAddedToChatUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b BotAddedToChatUpdate) GetChatID() int {
+ return b.ChatId
+}
+
// You will receive this update when bots has been removed from chat
type BotRemovedFromChatUpdate struct {
Update
@@ -629,6 +639,14 @@ type BotRemovedFromChatUpdate struct {
User User `json:"user"` // User who removed bots from chat
}
+func (b BotRemovedFromChatUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b BotRemovedFromChatUpdate) GetChatID() int {
+ return b.ChatId
+}
+
// Bot gets this type of update as soon as user pressed `Start` button
type BotStartedUpdate struct {
Update
@@ -636,6 +654,14 @@ type BotStartedUpdate struct {
User User `json:"user"` // User pressed the 'Start' button
}
+func (b BotStartedUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b BotStartedUpdate) GetChatID() int {
+ return b.ChatId
+}
+
// Object sent to bots when user presses button
type Callback struct {
Timestamp int `json:"timestamp"` // Unix-time when event has occurred
@@ -644,6 +670,14 @@ type Callback struct {
User User `json:"user"` // User pressed the button
}
+func (b Callback) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b Callback) GetChatID() int {
+ return 0
+}
+
// Bot gets this type of update as soon as title has been changed in chat
type ChatTitleChangedUpdate struct {
Update
@@ -652,6 +686,14 @@ type ChatTitleChangedUpdate struct {
Title string `json:"title"` // New title
}
+func (b ChatTitleChangedUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b ChatTitleChangedUpdate) GetChatID() int {
+ return b.ChatId
+}
+
// You will get this `update` as soon as user presses button
type MessageCallbackUpdate struct {
Update
@@ -659,24 +701,56 @@ type MessageCallbackUpdate struct {
Message *Message `json:"message"` // Original message containing inline keyboard. Can be `null` in case it had been deleted by the moment a bots got this update
}
+func (b MessageCallbackUpdate) GetUserID() int {
+ return b.Callback.User.UserId
+}
+
+func (b MessageCallbackUpdate) GetChatID() int {
+ return 0
+}
+
// You will get this `update` as soon as message is created
type MessageCreatedUpdate struct {
Update
Message Message `json:"message"` // Newly created message
}
+func (b MessageCreatedUpdate) GetUserID() int {
+ return b.Message.Sender.UserId
+}
+
+func (b MessageCreatedUpdate) GetChatID() int {
+ return b.Message.Recipient.ChatId
+}
+
// You will get this `update` as soon as message is edited
type MessageEditedUpdate struct {
Update
Message Message `json:"message"` // Edited message
}
+func (b MessageEditedUpdate) GetUserID() int {
+ return b.Message.Sender.UserId
+}
+
+func (b MessageEditedUpdate) GetChatID() int {
+ return b.Message.Recipient.ChatId
+}
+
// You will get this `update` as soon as message is removed
type MessageRemovedUpdate struct {
Update
MessageId string `json:"message_id"` // Identifier of removed message
}
+func (b MessageRemovedUpdate) GetUserID() int {
+ return 0
+}
+
+func (b MessageRemovedUpdate) GetChatID() int {
+ return 0
+}
+
// You will receive this update when user has been added to chat where bots is administrator
type UserAddedToChatUpdate struct {
Update
@@ -685,6 +759,14 @@ type UserAddedToChatUpdate struct {
InviterId int `json:"inviter_id"` // User who added user to chat
}
+func (b UserAddedToChatUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b UserAddedToChatUpdate) GetChatID() int {
+ return b.ChatId
+}
+
// You will receive this update when user has been removed from chat where bots is administrator
type UserRemovedFromChatUpdate struct {
Update
@@ -692,3 +774,11 @@ type UserRemovedFromChatUpdate struct {
User User `json:"user"` // User removed from chat
AdminId int `json:"admin_id"` // Administrator who removed user from chat
}
+
+func (b UserRemovedFromChatUpdate) GetUserID() int {
+ return b.User.UserId
+}
+
+func (b UserRemovedFromChatUpdate) GetChatID() int {
+ return b.ChatId
+}