1
This commit is contained in:
@@ -3,6 +3,7 @@ package enterprise_wechat
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.rosy.net.cn/baseapi"
|
"git.rosy.net.cn/baseapi"
|
||||||
|
"git.rosy.net.cn/jx-callback/globals"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -26,7 +27,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetToken(t *testing.T) {
|
func TestGetToken(t *testing.T) {
|
||||||
api = New("ww9a156bfa070e1857", "JQsEmSTltHhNgdPIT320YJFphiYmRs-YZa-rCBwplss")
|
|
||||||
token, err := api.GetAccessToken()
|
token, err := api.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err=", err)
|
fmt.Println("err=", err)
|
||||||
@@ -60,7 +60,6 @@ func TestGetAllDepartmentList(t *testing.T) {
|
|||||||
|
|
||||||
// 创建员工
|
// 创建员工
|
||||||
func TestCreateStaff(t *testing.T) {
|
func TestCreateStaff(t *testing.T) {
|
||||||
api = New("ww9a156bfa070e1857", "0jBdCjSmoFiOoHIXyeCK9VbGQ82fVNJZ8uMl6JNN7X4") // 通讯录
|
|
||||||
err := api.CreateBoss2JxStaff(&CreateBoos2JXStaffReq{
|
err := api.CreateBoss2JxStaff(&CreateBoos2JXStaffReq{
|
||||||
Userid: "jx2022524_6685111",
|
Userid: "jx2022524_6685111",
|
||||||
Name: "门店老板",
|
Name: "门店老板",
|
||||||
@@ -94,7 +93,6 @@ func TestGetUserByMobile(t *testing.T) {
|
|||||||
|
|
||||||
// 创建会话群聊
|
// 创建会话群聊
|
||||||
func TestCreateSession(t *testing.T) {
|
func TestCreateSession(t *testing.T) {
|
||||||
api = New("ww9a156bfa070e1857", "JQsEmSTltHhNgdPIT320YJFphiYmRs-YZa-rCBwplss") // 小程序
|
|
||||||
err := api.CreateAppChat(&CreateAppChatParamReq{
|
err := api.CreateAppChat(&CreateAppChatParamReq{
|
||||||
UserList: []string{"WangXiao", "LiuLei"},
|
UserList: []string{"WangXiao", "LiuLei"},
|
||||||
Name: "刘磊的企业微信群1",
|
Name: "刘磊的企业微信群1",
|
||||||
@@ -104,7 +102,30 @@ func TestCreateSession(t *testing.T) {
|
|||||||
fmt.Println("err======", err)
|
fmt.Println("err======", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSuiteTicket(t *testing.T) {
|
// 发送消息到企业微信
|
||||||
api = New("ww9a156bfa070e1857", "JQsEmSTltHhNgdPIT320YJFphiYmRs-YZa-rCBwplss") // 小程序
|
func TestSendMsg(t *testing.T) {
|
||||||
|
msg := &EnterpriseSendMsgReq{
|
||||||
|
Touser: "LiuLei",
|
||||||
|
Toparty: "",
|
||||||
|
Totag: "",
|
||||||
|
Msgtype: "textcard",
|
||||||
|
Agentid: 1000005,
|
||||||
|
Textcard: TextCardObject{
|
||||||
|
Title: "异步任务",
|
||||||
|
Description: "刚刚的同步任务下载链接",
|
||||||
|
Url: "https://www.baidu.com",
|
||||||
|
Btntxt: "详情",
|
||||||
|
},
|
||||||
|
EnableIdTrans: 0,
|
||||||
|
EnableDuplicateCheck: 0,
|
||||||
|
DuplicateCheckInterval: 0,
|
||||||
|
}
|
||||||
|
err := api.SendMsgToUser(msg)
|
||||||
|
fmt.Println("err === ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取员工信息
|
||||||
|
func TestGetUserId(t *testing.T) {
|
||||||
|
phone, err := api.GetUserIdByMobile("18981810340")
|
||||||
|
globals.SugarLogger.Debugf("phone := %s,err :=%s", phone, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ const (
|
|||||||
CreateBoosToJxStaff = "cgi-bin/user/create" // 将京西老板创建为企业员工
|
CreateBoosToJxStaff = "cgi-bin/user/create" // 将京西老板创建为企业员工
|
||||||
GetDepartmentUserDetail = "cgi-bin/user/list" // 获取部门用户详细情况
|
GetDepartmentUserDetail = "cgi-bin/user/list" // 获取部门用户详细情况
|
||||||
GetUserByMobileUrl = "cgi-bin/user/getuserid" // 通过手机号获取用户id
|
GetUserByMobileUrl = "cgi-bin/user/getuserid" // 通过手机号获取用户id
|
||||||
|
SendMsgToUser = "cgi-bin/message/send" // 给用户发送消息
|
||||||
|
|
||||||
EnterpriseTicketInfo = "/suite/receive" // 企业微信服务器会定时(每十分钟)推送ticket
|
EnterpriseTicketInfo = "/suite/receive" // 企业微信服务器会定时(每十分钟)推送ticket
|
||||||
)
|
)
|
||||||
|
|||||||
73
platformapi/enterprise_wechat/wechat_msg.go
Normal file
73
platformapi/enterprise_wechat/wechat_msg.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package enterprise_wechat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MsgTypeText = "text" // 文本消息
|
||||||
|
MsgTypeImg = "image" // 图片消息
|
||||||
|
MsgTypeVoice = "voice" // 语音消息
|
||||||
|
MsgTypeVideo = "video" // 视频消息
|
||||||
|
MsgTypeFile = "file" // 文件消息
|
||||||
|
MsgTypeTextCard = "textcard" // 文本卡片消息
|
||||||
|
MsgTypeNews = "news" // 图文消息
|
||||||
|
MsgTypeMpNews = "mpnews" // 图文消息
|
||||||
|
MsgTypeMarkdown = "markdown" // markdown消息
|
||||||
|
MsgTypeMiniprogramNotice = "miniprogram_notice" // 小程序通知消息
|
||||||
|
MsgTypeTemplateCard = "template_card" // 模板卡片消息(文本型/图文型/按钮型/投票型/多项选择型)
|
||||||
|
)
|
||||||
|
|
||||||
|
// EnterpriseSendMsgReq 发送企业微信文本卡片消息请求参数
|
||||||
|
type EnterpriseSendMsgReq struct {
|
||||||
|
Touser string `json:"touser"` // 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)
|
||||||
|
Toparty string `json:"toparty"` // 部门ID列表,多个接收者用‘|’分隔,最多支持100个
|
||||||
|
Totag string `json:"totag"` // 标签ID列表,多个接收者用‘|’分隔,最多支持100个
|
||||||
|
Msgtype string `json:"msgtype"` // 消息类型,此时固定为:textcard
|
||||||
|
Agentid int `json:"agentid"` // 企业应用的id
|
||||||
|
Textcard TextCardObject `json:"textcard"`
|
||||||
|
EnableIdTrans int `json:"enable_id_trans"` // 表示是否开启id转译,0表示否,1表示是,默认0
|
||||||
|
EnableDuplicateCheck int `json:"enable_duplicate_check"` // 表示是否开启重复消息检查,0表示否,1表示是,
|
||||||
|
DuplicateCheckInterval int `json:"duplicate_check_interval"` // 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
|
||||||
|
}
|
||||||
|
|
||||||
|
type TextCardObject struct {
|
||||||
|
Title string `json:"title"` // 标题,不超过128个字节
|
||||||
|
Description string `json:"description"` // 描述,不超过512个字节
|
||||||
|
Url string `json:"url"` // 点击后跳转的链接
|
||||||
|
Btntxt string `json:"btntxt"` // 按钮文字
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnterpriseSendMsgRes 返回参数
|
||||||
|
type EnterpriseSendMsgRes struct {
|
||||||
|
Errcode int `json:"errcode"` // 返回码
|
||||||
|
Errmsg string `json:"errmsg"` // 对返回码的文本描述内容
|
||||||
|
Invaliduser string `json:"invaliduser"` // 不合法的userid,不区分大小写,统一转为小写
|
||||||
|
Invalidparty string `json:"invalidparty"` // 不合法的partyid
|
||||||
|
Invalidtag string `json:"invalidtag"` // 不合法的标签id
|
||||||
|
Unlicenseduser string `json:"unlicenseduser"` // 没有基础接口许可(包含已过期)的userid
|
||||||
|
Msgid string `json:"msgid"` // 消息id,用于撤回应用消息
|
||||||
|
ResponseCode string `json:"response_code"` // 仅消息类型为“按钮交互型”,“投票选择型”和“多项选择型”的模板卡片消息返回,应用可使用response_code调用更新模版卡片消息接口,72小时内有效,且只能使用一次
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMsgToUser 将消息发送给企业微信用户
|
||||||
|
func (a *API) SendMsgToUser(msg *EnterpriseSendMsgReq) error {
|
||||||
|
a.CheckAccessTokenExpiresIn()
|
||||||
|
param := utils.Struct2Map(msg, "", false)
|
||||||
|
data, err := a.AccessAPI(WeChatBaseApi, SendMsgToUser, http.MethodPost, param)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := &EnterpriseSendMsgRes{}
|
||||||
|
if err := utils.Map2StructByJson(data, result, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result.Errcode != 0 {
|
||||||
|
return errors.New(result.Errmsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user