53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package unify
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"git.u8t.cn/open/gosdk/util"
|
|
)
|
|
|
|
type Message struct {
|
|
token string
|
|
sender string
|
|
address string
|
|
}
|
|
|
|
func NewMessage(addreess string, token string, sender string) *Message {
|
|
if addreess == "" {
|
|
addreess = "http://127.0.0.1:9281"
|
|
}
|
|
return &Message{
|
|
address: addreess,
|
|
token: token,
|
|
sender: sender,
|
|
}
|
|
}
|
|
|
|
func (m *Message) Send(args ...string) error {
|
|
var receiver, content, fingerprint, count string
|
|
if len(args) == 2 {
|
|
receiver, content = args[0], args[1]
|
|
} else if len(args) == 3 {
|
|
receiver, content, fingerprint = args[0], args[1], args[2]
|
|
} else if len(args) > 3 {
|
|
receiver, content, fingerprint, count = args[0], args[1], args[2], args[3]
|
|
}
|
|
reqUrl := fmt.Sprintf("%s/admin/message/send?sender=%s&receiver=%s&fingerprint=%s&count=%s&content=%s", m.address, m.sender, receiver, fingerprint, count, url.QueryEscape(content))
|
|
body, err := util.HttpGet(reqUrl, map[string]string{
|
|
"x-token": m.token,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var rsp util.Response
|
|
if err := json.Unmarshal(body, &rsp); err != nil {
|
|
return err
|
|
}
|
|
if rsp.Code != 0 {
|
|
return fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
}
|
|
return nil
|
|
}
|