diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
commit | 94c96cef29cd83bc60b2eabb8bb89c9fd8c7059b (patch) | |
tree | 676b3e2b6ffb171e102931e5fac01f7a4621c20a /examples/example_longpolling.go | |
parent | d13acd7da9bd1c08de33b91120a479df88d21db2 (diff) |
0.1.8 Release
Diffstat (limited to 'examples/example_longpolling.go')
-rw-r--r-- | examples/example_longpolling.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/example_longpolling.go b/examples/example_longpolling.go new file mode 100644 index 0000000..67a0d7c --- /dev/null +++ b/examples/example_longpolling.go @@ -0,0 +1,62 @@ +// +build ignore + +/** + * Updates loop example + */ +package main + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + + "github.com/neonxp/tamtam" +) + +func main() { + // Initialisation + api := tamtam.New(os.Getenv("TOKEN")) + + // Some methods demo: + info, err := api.Bots.GetBot() + log.Printf("Get me: %#v %#v", info, err) + + 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 + } + + } + }() + go func() { + exit := make(chan os.Signal) + signal.Notify(exit, os.Kill, os.Interrupt) + select { + case <-exit: + cancel() + case <-ctx.Done(): + return + } + }() + + if err := api.UpdatesLoop(ctx); err != nil { + log.Fatalln(err) + } + +} |