message send
This commit is contained in:
parent
2d5f373bea
commit
dfca7748b5
170
qyweixin/app.go
170
qyweixin/app.go
|
@ -1,6 +1,7 @@
|
|||
package qyweixin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -11,7 +12,12 @@ import (
|
|||
"gitlab.batiao8.com/open/gosdk/wechat/cache"
|
||||
"gitlab.batiao8.com/open/gosdk/wechat/message"
|
||||
wutil "gitlab.batiao8.com/open/gosdk/wechat/util"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -19,6 +25,36 @@ import (
|
|||
var (
|
||||
wechatCache cache.Cache = cache.NewMemory()
|
||||
)
|
||||
var (
|
||||
urlQiyeSend = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
|
||||
)
|
||||
|
||||
type MessageRequest struct {
|
||||
ToUser string `json:"touser"`
|
||||
ToParty string `json:"toparty"`
|
||||
ToTag string `json:"totag"`
|
||||
MstType string `json:"msgtype"`
|
||||
AgentId string `json:"agentid"`
|
||||
|
||||
Text struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"text"`
|
||||
|
||||
Image struct {
|
||||
MediaID string `json:"media_id"`
|
||||
} `json:"image"`
|
||||
|
||||
Video struct {
|
||||
MediaID string `json:"media_id"`
|
||||
Title string `json:"title"`
|
||||
Desc string `json:"description"`
|
||||
} `json:"video"`
|
||||
}
|
||||
|
||||
type BaseResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Corpid string
|
||||
|
@ -113,6 +149,140 @@ func (q *App) GetDepartmentUserid(departmentId int) ([]string, error) {
|
|||
return userids, nil
|
||||
}
|
||||
|
||||
func (a *App) SendText(receiver []string, content string) error {
|
||||
reqUrl := fmt.Sprintf("%s?access_token=%s", urlQiyeSend, a.GetToken())
|
||||
req := new(MessageRequest)
|
||||
req.MstType = "text"
|
||||
req.AgentId = a.config.Agent
|
||||
req.Text.Content = content
|
||||
req.ToUser = strings.Join(receiver, "|")
|
||||
|
||||
data, _ := json.Marshal(req)
|
||||
result, err := util.HttpPostJson(reqUrl, nil, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rsp BaseResponse
|
||||
err = json.Unmarshal([]byte(result), &rsp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rsp.ErrCode != 0 {
|
||||
return fmt.Errorf("%d:%s", rsp.ErrCode, rsp.ErrMsg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) SendImage(receiver []string, meidiaId string) error {
|
||||
|
||||
url := fmt.Sprintf("%s?access_token=%s", urlQiyeSend, a.GetToken())
|
||||
req := new(MessageRequest)
|
||||
req.MstType = "image"
|
||||
req.AgentId = a.config.Agent
|
||||
req.ToUser = strings.Join(receiver, "|")
|
||||
req.Image.MediaID = meidiaId
|
||||
|
||||
data, _ := json.Marshal(req)
|
||||
result, err := util.HttpPostJson(url, nil, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rsp BaseResponse
|
||||
err = json.Unmarshal([]byte(result), &rsp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.ErrCode != 0 {
|
||||
return fmt.Errorf("%d:%s", rsp.ErrCode, rsp.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) SendVideo(receiver []string, meidiaId, title, desc string) error {
|
||||
|
||||
url := fmt.Sprintf("%s?access_token=%s", urlQiyeSend, a.GetToken())
|
||||
req := new(MessageRequest)
|
||||
req.MstType = "video"
|
||||
req.AgentId = a.config.Agent
|
||||
req.ToUser = strings.Join(receiver, "|")
|
||||
req.Video.MediaID = meidiaId
|
||||
req.Video.Title = title
|
||||
req.Video.Desc = desc
|
||||
data, _ := json.Marshal(req)
|
||||
result, err := util.HttpPostJson(url, nil, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rsp BaseResponse
|
||||
err = json.Unmarshal([]byte(result), &rsp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rsp.ErrCode != 0 {
|
||||
return fmt.Errorf("%d:%s", rsp.ErrCode, rsp.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) Upload(path, kind string) (string, error) {
|
||||
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer fh.Close()
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||
|
||||
fileWriter, err := bodyWriter.CreateFormFile(kind, filepath.Base(path))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = io.Copy(fileWriter, fh)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bodyWriter.Close()
|
||||
|
||||
url := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s", a.GetToken(), kind)
|
||||
req, err := http.NewRequest("POST", url, bodyBuf)
|
||||
req.Header.Add("Content-Type", bodyWriter.FormDataContentType())
|
||||
urlQuery := req.URL.Query()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req.URL.RawQuery = urlQuery.Encode()
|
||||
client := http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
jsonbody, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
result := make(map[string]interface{})
|
||||
if err := json.Unmarshal(jsonbody, &result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if cast.ToInt(result["errcode"]) != 0 {
|
||||
return "", fmt.Errorf("%d:%s", cast.ToInt(result["errcode"]), cast.ToString(result["errmsg"]))
|
||||
}
|
||||
return cast.ToString(result["media_id"]), nil
|
||||
}
|
||||
|
||||
func (q *App) Callback(ctx *gin.Context) {
|
||||
|
||||
//配置微信参数
|
||||
|
|
Loading…
Reference in New Issue