diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-13 20:39:39 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-13 20:39:39 +0300 |
commit | 9ea34f2a2e7aa0249b73b5d9bb05795a668f270d (patch) | |
tree | bca37e243209d8371e6a261b214bd0855ff4cad4 /examples/example_longpolling.go | |
parent | 3da482d48583a697e9cd0dab44d3147c9bc19387 (diff) |
Improve longpolling
Added chatID and userID to UpdateInterface
Diffstat (limited to 'examples/example_longpolling.go')
-rw-r--r-- | examples/example_longpolling.go | 32 |
1 files changed, 11 insertions, 21 deletions
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() } |