* SendMessage
* UploadFile
This commit is contained in:
parent
5bf56e2faf
commit
0ae15ea25f
7 changed files with 191 additions and 2 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -10,3 +10,5 @@
|
|||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
.idea
|
44
README.md
44
README.md
|
@ -1,2 +1,42 @@
|
|||
# icq
|
||||
Go lang lib for ICQ BOT API
|
||||
# ICQ Bot API
|
||||
|
||||
## Working
|
||||
|
||||
* SendMessage
|
||||
* SendFile
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/go-icq/icq"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// New API object
|
||||
b := icq.NewAPI(os.Getenv("ICQ_TOKEN"))
|
||||
|
||||
// Send message
|
||||
r, err := b.SendMessage("429950", "Hello, world!")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println(r.State)
|
||||
|
||||
// Send file
|
||||
f, err := os.Open("./example/icq.png")
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
file, err := b.UploadFile("icq.png", f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b.SendMessage("429950", file)
|
||||
}
|
||||
```
|
||||
|
|
96
api.go
Normal file
96
api.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package icq
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTP Client interface
|
||||
type Doer interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// API
|
||||
type API struct {
|
||||
token string
|
||||
baseUrl string
|
||||
client Doer
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
// NewAPI constructor of API object
|
||||
func NewAPI(token string) *API {
|
||||
return &API{
|
||||
token: token,
|
||||
baseUrl: "https://botapi.icq.net",
|
||||
mu: new(sync.Mutex),
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
}
|
||||
|
||||
// SendMessage with `message` text to `to` participant
|
||||
func (a *API) SendMessage(to string, message string) (*MessageResponse, error) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
v := url.Values{
|
||||
"aimsid": []string{a.token},
|
||||
"r": []string{strconv.FormatInt(time.Now().Unix(), 10)},
|
||||
"t": []string{to},
|
||||
"message": []string{message},
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, a.baseUrl+"/im/sendIM?"+v.Encode(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
r := &Response{}
|
||||
if err := json.Unmarshal(b, r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r.Response.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("failed to send message: %s", r.Response.StatusText)
|
||||
}
|
||||
return r.Response.Data, nil
|
||||
}
|
||||
|
||||
// UploadFile to ICQ servers and returns URL to file
|
||||
func (a *API) UploadFile(fileName string, r io.Reader) (string, error) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
v := url.Values{"aimsid": []string{a.token}, "filename": []string{fileName}}
|
||||
req, err := http.NewRequest(http.MethodPost, a.baseUrl+"/im/sendFile?"+v.Encode(), r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
rb, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
file := struct {
|
||||
Data struct {
|
||||
StaticUrl string `json:"static_url"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(rb, &file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return file.Data.StaticUrl, nil
|
||||
}
|
31
example/example.go
Normal file
31
example/example.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/go-icq/icq"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// New API object
|
||||
b := icq.NewAPI(os.Getenv("ICQ_TOKEN"))
|
||||
|
||||
// Send message
|
||||
r, err := b.SendMessage("429950", "Hello, world!")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println(r.State)
|
||||
|
||||
// Send file
|
||||
f, err := os.Open("./example/icq.png")
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
file, err := b.UploadFile("icq.png", f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b.SendMessage("429950", file)
|
||||
}
|
BIN
example/icq.png
Normal file
BIN
example/icq.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 619 KiB |
1
go.mod
Normal file
1
go.mod
Normal file
|
@ -0,0 +1 @@
|
|||
module github.com/go-icq/icq
|
19
types.go
Normal file
19
types.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package icq
|
||||
|
||||
type Response struct {
|
||||
Response struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
StatusText string `json:"statusText"`
|
||||
RequestId string `json:"requestId"`
|
||||
Data *MessageResponse `json:"data"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
SubCode struct {
|
||||
Error int `json:"error"`
|
||||
} `json:"subCode"`
|
||||
MessageID string `json:"msgId"`
|
||||
HistoryMessageID int64 `json:"histMsgId"`
|
||||
State string `json:"state"`
|
||||
}
|
Loading…
Reference in a new issue