diff --git a/README.md b/README.md index 933632b..8f30557 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,35 @@ Simple client library. -## Examples +### First example: get server version -All examples have `-verbose` and `-server` options. +``` +go run examples/serverinfo/main.go +go run examples/serverinfo/main.go -verbose +go run examples/serverinfo/main.go -verbose -server https://demo.tada.team +``` -How to get server url, team uid and chat jid: +Any example have `-verbose` and `-server` options. Default server value is `https://web.tada.team`. -```https://demo.tada.team/dbd248d7-25c2-4e8f-a23a-99baf63223e9/chats/g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5``` +### Authorization token - * https://demo.tada.team — server url - * dbd248d7-25c2-4e8f-a23a-99baf63223e9 – team uid - * g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5 – chat jid +Some examples need authorization token. You have two options: + +#### Bot token -How to get bot token: type "/newbot " command in @TadaBot direct chat. +Create bot by typing `/newbot ` command in @TadaBot direct chat. +Allowed for team admin only. -### Server version +#### Token from your personal account +For server with sms authorization: ``` -go run examples/serverinfo/main.go -go run examples/serverinfo/main.go -verbose -go run examples/serverinfo/main.go -verbose -server https://demo.tada.team +go run examples/smsauth/main.go +``` + +For server with Active Directory authorization: +``` +go run examples/passwordauth/main.go ``` ### Messaging @@ -30,6 +39,13 @@ go run examples/serverinfo/main.go -verbose -server https://demo.tada.team go run examples/message/main.go -team -token -chat -message ``` +How to get team uid and chat jid: + +```https://demo.tada.team/dbd248d7-25c2-4e8f-a23a-99baf63223e9/chats/g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5``` + + * dbd248d7-25c2-4e8f-a23a-99baf63223e9 – team uid + * g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5 – chat jid + ### Contacts ```go run examples/contacts/main.go -team -token ``` diff --git a/examples/passwordauth/main.go b/examples/passwordauth/main.go new file mode 100644 index 0000000..42bf81e --- /dev/null +++ b/examples/passwordauth/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + + "github.com/manifoldco/promptui" + "github.com/tada-team/tdclient" + "github.com/tada-team/tdclient/examples" +) + +func main() { + settings := examples.NewSettings() + settings.Parse() + + client, err := tdclient.NewSession(settings.Server) + if err != nil { + panic(err) + } + + client.SetVerbose(settings.Verbose) + + prompt := promptui.Prompt{Label: "Enter login"} + login, err := prompt.Run() + if err != nil { + panic(err) + } + + prompt = promptui.Prompt{Label: "Enter password", Mask: '*'} + password, err := prompt.Run() + if err != nil { + panic(err) + } + + tokenResp, err := client.AuthByPasswordGetToken(login, password) + if err != nil { + panic(err) + } + + fmt.Println("Your token:", tokenResp.Token) +} diff --git a/json.go b/json.go index 89948f1..f316cbc 100644 --- a/json.go +++ b/json.go @@ -1,5 +1,23 @@ package tdclient -import jsoniter "github.com/json-iterator/go" +import ( + "bytes" + "log" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" +) var JSON = jsoniter.ConfigCompatibleWithStandardLibrary + +func debugJSON(v interface{}) string { + b := new(bytes.Buffer) + debugEncoder := JSON.NewEncoder(b) + debugEncoder.SetIndent("", " ") + debugEncoder.SetEscapeHTML(false) + err := debugEncoder.Encode(v) + if err != nil { + log.Panicln(errors.Wrap(err, "json marshall fail")) + } + return b.String() +} diff --git a/session.go b/session.go index 001e2f7..e4d5de4 100644 --- a/session.go +++ b/session.go @@ -127,9 +127,7 @@ func (s Session) doRaw(method, path string, data, v interface{}) error { return errors.Wrap(err, "read body fail") } - if resp.StatusCode != 200 { - return errors.Wrapf(err, "status code: %d %s", resp.StatusCode, string(respData)) - } + s.logger.Println("resp:", resp.StatusCode, string(respData)) if err := JSON.Unmarshal(respData, &v); err != nil { return errors.Wrapf(err, "unmarshal fail on: %s", string(respData)) diff --git a/v4.go b/v4.go index 578cf31..cfb114a 100644 --- a/v4.go +++ b/v4.go @@ -98,3 +98,25 @@ func (s Session) AuthBySmsGetToken(phone, code string) (tdapi.Auth, error) { return resp.Result, nil } + +func (s Session) AuthByPasswordGetToken(login, password string) (tdapi.Auth, error) { + req := map[string]interface{}{ + "login": login, + "password": password, + } + + resp := new(struct { + tdapi.Resp + Result tdapi.Auth `json:"result"` + }) + + if err := s.doPost("/api/v4/auth/password/get-token", req, resp); err != nil { + return resp.Result, err + } + + if !resp.Ok { + return resp.Result, resp.Error + } + + return resp.Result, nil +}