diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/example_1.go | 10 | ||||
-rw-r--r-- | examples/example_2.go | 72 |
2 files changed, 80 insertions, 2 deletions
diff --git a/examples/example_1.go b/examples/example_1.go index 428b99a..a1fb967 100644 --- a/examples/example_1.go +++ b/examples/example_1.go @@ -1,5 +1,8 @@ // +build ignore +/** + * Webhook example + */ package main import ( @@ -23,9 +26,12 @@ func main() { log.Printf("Get chat: %#v %#v", chat, err) msgs, err := api.GetMessages(chats.Chats[0].ChatId, nil, 0, 0, 0) log.Printf("Get messages: %#v %#v", msgs, err) - + subs, _ := api.GetSubscriptions() + for _, s := range subs.Subscriptions { + _, _ = api.Unsubscribe(s.Url) + } subscriptionResp, err := api.Subscribe(&tamtam.SubscriptionRequestBody{ - Url: "https://4940675e.ngrok.io/webhook", // Replace by your webhook! + Url: " https://576df2ec.ngrok.io/webhook", // Replace by your webhook! }) log.Printf("Subscription: %#v %#v", subscriptionResp, err) diff --git a/examples/example_2.go b/examples/example_2.go new file mode 100644 index 0000000..74547fa --- /dev/null +++ b/examples/example_2.go @@ -0,0 +1,72 @@ +// +build ignore + +/** + * Updates loop example + */ +package main + +import ( + "context" + "fmt" + "github.com/neonxp/tamtam" + "log" + "os" + "os/signal" +) + +func main() { + // Initialisation + api := tamtam.New(os.Getenv("TOKEN")) + + // Some methods demo: + info, err := api.GetMe() + log.Printf("Get me: %#v %#v", info, err) + chats, err := api.GetChats(0, 0) + log.Printf("Get chats: %#v %#v", chats, err) + chat, err := api.GetChat(chats.Chats[0].ChatId) + log.Printf("Get chat: %#v %#v", chat, err) + subs, _ := api.GetSubscriptions() + for _, s := range subs.Subscriptions { + _, _ = api.Unsubscribe(s.Url) + } + ch := make(chan interface{}, 1) // Channel with updates from TamTam + + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + for { + select { + case upd := <-ch: + log.Printf("Received: %#v", upd) + switch upd := upd.(type) { + case tamtam.UpdateMessageCreated: + res, err := api.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.GetUpdatesLoop(ctx, ch); err != nil { + log.Fatalln(err) + } + +} |