Add GetMe, use jsoniter
This commit is contained in:
parent
ab9dc80b06
commit
9abe50bebd
7 changed files with 49 additions and 7 deletions
7
LICENSE
Normal file
7
LICENSE
Normal file
|
@ -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.
|
18
api.go
18
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"`
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package tg
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
|
|
@ -2,7 +2,6 @@ package tg
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
|
2
go.mod
2
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
|
||||
|
|
12
go.sum
Normal file
12
go.sum
Normal file
|
@ -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=
|
15
telegram.go
15
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue