tamtam/bots.go

51 lines
1.2 KiB
Go
Raw Normal View History

2019-08-09 02:02:05 +03:00
package tamtam
import (
"encoding/json"
"log"
"net/http"
"net/url"
2019-09-05 22:43:32 +03:00
"github.com/neonxp/tamtam/schemes"
2019-08-09 02:02:05 +03:00
)
type bots struct {
client *client
}
func newBots(client *client) *bots {
return &bots{client: client}
}
2019-08-09 02:16:08 +03:00
//GetBot returns info about current bot. Current bot can be identified by access token. Method returns bot identifier, name and avatar (if any)
2019-09-05 22:43:32 +03:00
func (a *bots) GetBot() (*schemes.BotInfo, error) {
result := new(schemes.BotInfo)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodGet, "me", 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)
}
2019-08-09 02:16:08 +03:00
//PatchBot edits current bot info. Fill only the fields you want to update. All remaining fields will stay untouched
2019-09-05 22:43:32 +03:00
func (a *bots) PatchBot(patch *schemes.BotPatch) (*schemes.BotInfo, error) {
result := new(schemes.BotInfo)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodPatch, "me", values, patch)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}