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 --- subscriptions.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 subscriptions.go (limited to 'subscriptions.go') diff --git a/subscriptions.go b/subscriptions.go new file mode 100644 index 0000000..d32d985 --- /dev/null +++ b/subscriptions.go @@ -0,0 +1,68 @@ +// Package tamtam implements TamTam Bot API +// Copyright (c) 2019 Alexander Kiryukhin +package tamtam + +import ( + "encoding/json" + "log" + "net/http" + "net/url" +) + +type subscriptions struct { + client *client +} + +func newSubscriptions(client *client) *subscriptions { + return &subscriptions{client: client} +} +func (a *subscriptions) GetSubscriptions() (*GetSubscriptionsResult, error) { + result := new(GetSubscriptionsResult) + values := url.Values{} + body, err := a.client.request(http.MethodGet, "subscriptions", values, nil) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *subscriptions) Subscribe(subscribeURL string, updateTypes []string) (*SimpleQueryResult, error) { + subscription := &SubscriptionRequestBody{ + Url: subscribeURL, + UpdateTypes: updateTypes, + Version: a.client.version, + } + result := new(SimpleQueryResult) + values := url.Values{} + body, err := a.client.request(http.MethodPost, "subscriptions", values, subscription) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *subscriptions) Unsubscribe(subscriptionURL string) (*SimpleQueryResult, error) { + result := new(SimpleQueryResult) + values := url.Values{} + values.Set("url", subscriptionURL) + body, err := a.client.request(http.MethodDelete, "subscriptions", values, nil) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} -- cgit v1.2.3