90 lines
3.0 KiB
Go
90 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/silenceper/wechat/v2"
|
|
workConfig "github.com/silenceper/wechat/v2/work/config"
|
|
"kefu/lib"
|
|
"kefu/lib/wechat_kf_sdk"
|
|
"kefu/models"
|
|
"kefu/tools"
|
|
"log"
|
|
)
|
|
|
|
// 发送企业微信应用通知
|
|
func SendWorkWechatMesage(entId string, visitor models.Visitor, kefu models.User, content string, c *gin.Context) bool {
|
|
currentHost := tools.GetHost(c.Request)
|
|
token := GenUserToken(kefu)
|
|
pcUrl := fmt.Sprintf("%s/sso?token=%s&redirect=%s/main", currentHost, token, currentHost)
|
|
//企业微信群机器人webhook
|
|
webhookUrl := models.FindEntConfig(entId, "WorkWechatWebHookUrl").ConfValue
|
|
if webhookUrl != "" {
|
|
data := map[string]interface{}{
|
|
"msgtype": "markdown",
|
|
"markdown": map[string]interface{}{
|
|
"content": fmt.Sprintf(`### %s
|
|
> 访客名:<font color="warning">%s</font>
|
|
> 客服名:<font color="warning">%s</font>
|
|
> 访问次数:%d
|
|
> 时间:%s
|
|
> 回复:请点击[H5后台](%s/h5/)或[PC后台](%s)
|
|
`, content, visitor.Name, kefu.Nickname, visitor.VisitNum, tools.GetNowTime(), currentHost, pcUrl),
|
|
},
|
|
}
|
|
str, _ := tools.JsonEncode(data)
|
|
url := webhookUrl
|
|
res, err := tools.PostJson(url, []byte(str))
|
|
log.Println(url, str, err, res)
|
|
}
|
|
return true
|
|
wechatConfig, _ := lib.NewWechatLib(entId)
|
|
if wechatConfig.WorkWechatCorpid == "" ||
|
|
wechatConfig.WorkWechatAppAgentId == "" ||
|
|
wechatConfig.WorkWechatAppSecret == "" {
|
|
return false
|
|
}
|
|
wc := wechat.NewWechat()
|
|
cfg := &workConfig.Config{
|
|
CorpID: wechatConfig.WorkWechatCorpid,
|
|
CorpSecret: wechatConfig.WorkWechatAppSecret,
|
|
Cache: memory,
|
|
}
|
|
work := wc.GetWork(cfg)
|
|
accessToken, _ := work.GetContext().GetAccessToken()
|
|
data := map[string]interface{}{
|
|
"touser": "@all",
|
|
"toparty": "",
|
|
"totag": "",
|
|
"msgtype": "markdown",
|
|
"agentid": wechatConfig.WorkWechatAppAgentId,
|
|
"markdown": map[string]interface{}{
|
|
"content": fmt.Sprintf(`### %s
|
|
> 访客名:<font color="warning">%s</font>
|
|
> 客服名:<font color="warning">%s</font>
|
|
> 访客编号:@%d
|
|
> 访问次数:%d
|
|
> 时间:%s
|
|
> 回复:请点击[H5后台](%s/h5/)或[PC后台](%s)
|
|
`, content, visitor.Name, kefu.Nickname, visitor.ID, visitor.VisitNum, tools.GetNowTime(), currentHost, pcUrl),
|
|
},
|
|
"safe": 0,
|
|
"enable_id_trans": 0,
|
|
"enable_duplicate_check": 0,
|
|
"duplicate_check_interval": 1800,
|
|
}
|
|
str, _ := tools.JsonEncode(data)
|
|
|
|
url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken
|
|
res, err := tools.PostJson(url, []byte(str))
|
|
log.Println(url, str, err, res)
|
|
return true
|
|
}
|
|
|
|
// 获取企业微信客服对象
|
|
func GetKefuWework(entId string) *wechat_kf_sdk.KefuWework {
|
|
configs := models.GetEntConfigsMap(entId, "kefuWeworkCorpid", "kefuWeworkSecret", "kefuWeworkToken", "kefuWeworkEncodingAESKey")
|
|
kefuWework := wechat_kf_sdk.NewKefuWework(configs["kefuWeworkCorpid"], configs["kefuWeworkSecret"], configs["kefuWeworkToken"], configs["kefuWeworkEncodingAESKey"])
|
|
return kefuWework
|
|
}
|