|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/astaxie/beego" |
| 5 | + weixin "github.com/chanyipiaomiao/weixin-kit" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + corpID = beego.AppConfig.String("weixin::corpID") |
| 10 | + appSecret = beego.AppConfig.String("weixin::appSecret") |
| 11 | + accessTokenAPI = beego.AppConfig.String("weixin::accessTokenAPI") |
| 12 | + sendMessageAPIURL = beego.AppConfig.String("weixin::sendMessageAPIURL") |
| 13 | +) |
| 14 | + |
| 15 | +// SendWeixinMessage 发送消息 |
| 16 | +func SendWeixinMessage(msgType, text, toTag, toUser, toParty string, agentIDFromReq int64) (bool, error) { |
| 17 | + var agentID int64 |
| 18 | + if agentIDFromReq == 0 { |
| 19 | + agentIDFromConf, err := beego.AppConfig.Int64("weixin::agentID") |
| 20 | + if err != nil { |
| 21 | + return false, err |
| 22 | + } |
| 23 | + agentID = agentIDFromConf |
| 24 | + } else { |
| 25 | + agentID = agentIDFromReq |
| 26 | + } |
| 27 | + |
| 28 | + message := &weixin.Message{ |
| 29 | + MsgType: msgType, // 目前只支持发送文本消息 |
| 30 | + ToTag: toTag, // ToTag 是在企业微信后台定义的标签ID,标签里面可以包含很多人,多个请用|分开 |
| 31 | + ToUser: toUser, // ToUser 是企业微信后台看到的用户的ID,多个请用|分开 |
| 32 | + ToParty: toParty, // ToParty 是企业微信后台看到的部门的ID,多个请用|分开 |
| 33 | + AgentID: agentID, // 企业应用的id,整型。可在应用的设置页面查看 |
| 34 | + Safe: 0, // 表示是否是保密消息,0表示否,1表示是,默认0 |
| 35 | + Text: &weixin.Text{ |
| 36 | + Content: text, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + client := &weixin.Client{ |
| 41 | + AccessTokenAPI: accessTokenAPI, |
| 42 | + APIURL: sendMessageAPIURL, |
| 43 | + CorpID: corpID, |
| 44 | + CorpSecret: appSecret, |
| 45 | + Message: message, |
| 46 | + } |
| 47 | + _, err := client.SendMessage() |
| 48 | + if err != nil { |
| 49 | + return false, err |
| 50 | + } |
| 51 | + return true, nil |
| 52 | +} |
0 commit comments