From 9ea34f2a2e7aa0249b73b5d9bb05795a668f270d Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Tue, 13 Aug 2019 20:39:39 +0300 Subject: Improve longpolling Added chatID and userID to UpdateInterface --- examples/example_longpolling.go | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'examples') 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() } -- cgit v1.2.3