From 94c96cef29cd83bc60b2eabb8bb89c9fd8c7059b Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 9 Aug 2019 02:02:05 +0300 Subject: 0.1.8 Release --- examples/example_webhook.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 examples/example_webhook.go (limited to 'examples/example_webhook.go') diff --git a/examples/example_webhook.go b/examples/example_webhook.go new file mode 100644 index 0000000..ad1bdae --- /dev/null +++ b/examples/example_webhook.go @@ -0,0 +1,53 @@ +// +build ignore + +/** + * Webhook example + */ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + + "github.com/neonxp/tamtam" +) + +func main() { + // Initialisation + api := tamtam.New(os.Getenv("TOKEN")) + host := os.Getenv("HOST") + + // Some methods demo: + info, err := api.Bots.GetBot() + log.Printf("Get me: %#v %#v", info, err) + + subs, _ := api.Subscriptions.GetSubscriptions() + for _, s := range subs.Subscriptions { + _, _ = api.Subscriptions.Unsubscribe(s.Url) + } + subscriptionResp, err := api.Subscriptions.Subscribe(host+"/webhook", []string{}) + log.Printf("Subscription: %#v %#v", subscriptionResp, err) + + ch := make(chan interface{}) // Channel with updates from TamTam + + http.HandleFunc("/webhook", api.GetHandler(ch)) + go func() { + for { + upd := <-ch + 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) + } + } + }() + + http.ListenAndServe(":10888", nil) +} -- cgit v1.2.3