From 9abe50bebd8b80230a50db7209e2ddacb1e6bdf2 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 24 Mar 2022 12:31:55 +0100 Subject: [PATCH] Add GetMe, use jsoniter --- LICENSE | 7 +++++++ api.go | 18 ++++++++++++++---- broker.go | 1 - client.go | 1 - go.mod | 2 ++ go.sum | 12 ++++++++++++ telegram.go | 15 ++++++++++++++- 7 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 LICENSE create mode 100644 go.sum diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7ab4768 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +ISC License + +Copyright 2022 Hamcha + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/api.go b/api.go index 0e11308..c89fb29 100644 --- a/api.go +++ b/api.go @@ -2,10 +2,15 @@ package tg // APIUser represents the "User" JSON structure type APIUser struct { - UserID int64 `json:"id"` - FirstName string `json:"first_name"` - LastName string `json:"last_name,omitempty"` - Username string `json:"username,omitempty"` + UserID int64 `json:"id"` + FirstName string `json:"first_name"` + LastName string `json:"last_name,omitempty"` + Username string `json:"username,omitempty"` + IsBot bool `json:"is_bot"` + LanguageCode string `json:"language_code,omitempty"` + CanJoinGroups bool `json:"can_join_groups,omitempty"` + CanReadAllGroupMessages bool `json:"can_read_all_group_messages,omitempty"` + SupportsInlineQueries bool `json:"supports_inline_queries,omitempty"` } // ChatType defines the type of chat @@ -243,3 +248,8 @@ type apiAlbumResponse struct { Ok bool `json:"ok"` Result []APIMessage `json:"result"` } + +type apiUserResponse struct { + Ok bool `json:"ok"` + Result APIUser `json:"result"` +} diff --git a/broker.go b/broker.go index 4950bb3..bde8cd2 100644 --- a/broker.go +++ b/broker.go @@ -2,7 +2,6 @@ package tg import ( "encoding/base64" - "encoding/json" "fmt" "log" "net" diff --git a/client.go b/client.go index 60b6d0b..75ac699 100644 --- a/client.go +++ b/client.go @@ -2,7 +2,6 @@ package tg import ( "bufio" - "encoding/json" "io" "log" ) diff --git a/go.mod b/go.mod index f894e36..9f199a3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.fromouter.space/hamcha/tg go 1.12 + +require github.com/json-iterator/go v1.1.12 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ea794b2 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/telegram.go b/telegram.go index fbecbea..e94b242 100644 --- a/telegram.go +++ b/telegram.go @@ -3,7 +3,6 @@ package tg import ( "bytes" "encoding/base64" - "encoding/json" "errors" "fmt" "io/ioutil" @@ -13,8 +12,12 @@ import ( "net/http" "net/url" "strconv" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigFastest + // APIEndpoint is Telegram's current Bot API base url endpoint const APIEndpoint = "https://api.telegram.org/" @@ -443,6 +446,16 @@ func (t Telegram) GetFile(data FileRequestData, client net.Conn, callback int) { fmt.Fprintln(client, string(clientmsg)) } +func (t Telegram) GetMe() (APIUser, error) { + resp, err := http.Get(t.apiURL("getMe")) + if checkerr("GetMe/get", err) { + return APIUser{}, err + } + var result apiUserResponse + err = json.NewDecoder(resp.Body).Decode(&result) + return result.Result, err +} + func (t Telegram) apiURL(method string) string { return APIEndpoint + "bot" + t.Token + "/" + method }