Skip to content

Commit 5c9a3da

Browse files
committed
1.增加任务卡片(taskcard)消息
2.取消sendmsg的agentid参数
1 parent ad32181 commit 5c9a3da

File tree

8 files changed

+152
-127
lines changed

8 files changed

+152
-127
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: go
22

33
go:
44
- 1.9
5-
- 1.8
65

76
sudo: required
87

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func main() {
2929
}
3030

3131
app := wechat.New(cfg)
32+
app.SendText("@all", "Hello,World!")
3233

3334
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
3435
app.VerifyURL(w, r).NewText("客服消息1").Send().NewText("客服消息2").Send().NewText("查询OK").Reply()
@@ -60,28 +61,29 @@ func main() {
6061
cfg := &wechat.WxConfig{
6162
Token: "yourToken",
6263
AppId: "yourCorpID",
64+
AgentId: "yourAgentId",
6365
Secret: "yourSecret",
6466
EncodingAESKey: "yourEncodingAesKey",
6567
AppType: 1,
6668
}
6769
```
6870

69-
### 直接发提醒消息
71+
## 主动推送消息
7072

71-
用户关注后,企业微信可以直接发,服务号需要用户48小时内进入过。
73+
用户关注后,企业微信可以主动推送消息,服务号需要用户48小时内进入过。
7274

7375
```go
74-
app.SendText(to, agentId, msg)
75-
app.SendImage(to, id, mediaId)
76-
app.SendVoice(to, id, mediaId)
77-
app.SendFile(to, id, mediaId)
78-
app.SendVideo(to, id, mediaId, title, desc)
79-
app.SendTextcard(to, id, title, desc, url)
80-
app.SendMusic(to, id, mediaId, title, desc, musicUrl, qhMusicUrl)
81-
app.SendNews(to, id, arts...)
82-
app.SendMpNews(to, id, arts...)
83-
app.SendMpNewsId(to, id, mediaId)
84-
app.SendMarkDown(to, id, content)
76+
app.SendText(to, msg)
77+
app.SendImage(to, mediaId)
78+
app.SendVoice(to, mediaId)
79+
app.SendFile(to, mediaId)
80+
app.SendVideo(to, mediaId, title, desc)
81+
app.SendTextcard(to, title, desc, url)
82+
app.SendMusic(to, mediaId, title, desc, musicUrl, qhMusicUrl)
83+
app.SendNews(to, arts...)
84+
app.SendMpNews(to, arts...)
85+
app.SendMpNewsId(to, mediaId)
86+
app.SendMarkDown(to, content)
8587
```
8688

8789
## 消息回调
@@ -160,7 +162,7 @@ func wxApiPost(c echo.Context) error {
160162
ctx.NewText("客服消息1").Send().NewText("客服消息2").Send()
161163
```
162164

163-
* 被动回复可直接调用ReplySuccess(),表示已收到,然后调用客服消息。
165+
* 被动回复可直接调用Reply(),表示已收到,然后调用客服消息。
164166

165167
#### 文本消息
166168

accesstoken.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ func (s *Server) GetUserAccessToken() string {
4949

5050
func (s *Server) getAccessToken() (err error) {
5151
if s.ExternalTokenHandler != nil {
52-
Printf("远程获取token")
5352
s.accessToken = s.ExternalTokenHandler(s.AppId, s.AppName)
53+
Printf("***%v[%v]远程获取token:%v", util.Substr(s.AppId, 14, 30), s.AgentId, s.accessToken)
5454
return
5555
}
56-
Printf("本地获取token")
5756
url := fmt.Sprintf(s.TokenUrl, s.AppId, s.Secret)
5857
at := new(AccessToken)
5958
if err = util.GetJson(url, at); err != nil {
@@ -62,9 +61,9 @@ func (s *Server) getAccessToken() (err error) {
6261
if at.ErrCode > 0 {
6362
return at.Error()
6463
}
65-
Printf("%v[%v]:%+v", s.AppId, s.AgentId, *at)
6664
at.ExpiresIn = time.Now().Unix() + at.ExpiresIn - 5
6765
s.accessToken = at
66+
Printf("***%v[%v]本地获取token:%v", util.Substr(s.AppId, 14, 30), s.AgentId, s.accessToken)
6867
return
6968

7069
}

context.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,41 @@ type Context struct {
1414
Timestamp string
1515
Nonce string
1616
Msg *WxMsg
17-
MsgEnc *WxMsgEnc
1817
Resp interface{}
1918
Writer http.ResponseWriter
2019
Request *http.Request
21-
repCount int
20+
hasReply bool
2221
}
2322

2423
// Reply 被动回复消息
2524
func (c *Context) Reply() (err error) {
26-
if c.Request.Method != "POST" || c.repCount > 0 {
27-
return errors.New("Reply err: no reply")
25+
if c.hasReply {
26+
return errors.New("重复调用错误")
2827
}
28+
29+
c.hasReply = true
30+
31+
if c.Resp == nil {
32+
return nil
33+
}
34+
2935
Printf("Wechat <== %+v", c.Resp)
3036
if c.SafeMode {
3137
b, err := xml.MarshalIndent(c.Resp, "", " ")
3238
if err != nil {
33-
c.Writer.Write([]byte{})
3439
return err
3540
}
3641
c.Resp, err = c.EncryptMsg(b, c.Timestamp, c.Nonce)
3742
if err != nil {
38-
c.Writer.Write([]byte{})
3943
return err
4044
}
4145
}
4246
c.Writer.Header().Set("Content-Type", "application/xml;charset=UTF-8")
43-
c.repCount++
4447
return xml.NewEncoder(c.Writer).Encode(c.Resp)
4548
}
4649

47-
// ReplySuccess 如果不能在5秒内处理完,应该先回复success,然后通过客服消息通知用户
48-
func (c *Context) ReplySuccess() (err error) {
49-
if c.Request.Method != http.MethodPost || c.repCount > 0 {
50-
return errors.New("Reply err: no reply")
51-
}
52-
_, err = c.Writer.Write([]byte("success"))
53-
return
54-
}
55-
5650
// Send 主动发送消息(客服)
5751
func (c *Context) Send() *Context {
58-
go c.SendMsg(c.Resp)
59-
return c
60-
}
61-
62-
// SendAdd 添加主动消息队列(客服)
63-
func (c *Context) SendAdd() *Context {
6452
c.AddMsg(c.Resp)
6553
return c
6654
}

send.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,76 +25,76 @@ func (s *Server) SendMsg(v interface{}) *WxErr {
2525
if err != nil {
2626
return &WxErr{-1, err.Error()}
2727
}
28-
Printf("发送消息:%+v\n回执:%+v", v, *rst)
28+
Printf("[*] 发送消息:%+v\n[*] 回执:%+v", v, *rst)
2929
return rst
3030
}
3131

3232
// SendText 发送客服text消息,过长时按500长度自动拆分
33-
func (s *Server) SendText(to string, agentId int, msg string, safe ...int) (e *WxErr) {
34-
if len(safe) > 0 && safe[0] == 1 {
35-
s.SafeOpen()
36-
defer s.SafeClose()
37-
}
38-
// m := strings.Join(msg, "")
33+
func (s *Server) SendText(to, msg string) (e *WxErr) {
3934
leng := utf8.RuneCountInString(msg)
4035
n := leng/500 + 1
4136

4237
if n == 1 {
43-
return s.SendMsg(s.NewText(to, agentId, msg))
38+
return s.SendMsg(s.NewText(to, msg))
4439
}
4540
for i := 0; i < n; i++ {
46-
e = s.SendMsg(s.NewText(to, agentId, fmt.Sprintf("%s\n(%v/%v)", util.Substr(msg, i*500, (i+1)*500), i+1, n)))
41+
e = s.SendMsg(s.NewText(to, fmt.Sprintf("%s\n(%v/%v)", util.Substr(msg, i*500, (i+1)*500), i+1, n)))
4742
}
4843

4944
return
5045
}
5146

5247
// SendImage 发送客服Image消息
53-
func (s *Server) SendImage(to string, id int, mediaId string) *WxErr {
54-
return s.SendMsg(s.NewImage(to, id, mediaId))
48+
func (s *Server) SendImage(to string, mediaId string) *WxErr {
49+
return s.SendMsg(s.NewImage(to, mediaId))
5550
}
5651

5752
// SendVoice 发送客服Voice消息
58-
func (s *Server) SendVoice(to string, id int, mediaId string) *WxErr {
59-
return s.SendMsg(s.NewVoice(to, id, mediaId))
53+
func (s *Server) SendVoice(to string, mediaId string) *WxErr {
54+
return s.SendMsg(s.NewVoice(to, mediaId))
6055
}
6156

6257
// SendFile 发送客服File消息
63-
func (s *Server) SendFile(to string, id int, mediaId string) *WxErr {
64-
return s.SendMsg(s.NewFile(to, id, mediaId))
58+
func (s *Server) SendFile(to string, mediaId string) *WxErr {
59+
return s.SendMsg(s.NewFile(to, mediaId))
6560
}
6661

6762
// SendVideo 发送客服Video消息
68-
func (s *Server) SendVideo(to string, id int, mediaId, title, desc string) *WxErr {
69-
return s.SendMsg(s.NewVideo(to, id, mediaId, title, desc))
63+
func (s *Server) SendVideo(to string, mediaId, title, desc string) *WxErr {
64+
return s.SendMsg(s.NewVideo(to, mediaId, title, desc))
7065
}
7166

7267
// SendTextcard 发送客服extcard消息
73-
func (s *Server) SendTextcard(to string, id int, title, desc, url string) *WxErr {
74-
return s.SendMsg(s.NewTextcard(to, id, title, desc, url))
68+
func (s *Server) SendTextcard(to string, title, desc, url string) *WxErr {
69+
return s.SendMsg(s.NewTextcard(to, title, desc, url))
7570
}
7671

7772
// SendMusic 发送客服Music消息
78-
func (s *Server) SendMusic(to string, id int, mediaId, title, desc, musicUrl, qhMusicUrl string) *WxErr {
79-
return s.SendMsg(s.NewMusic(to, id, mediaId, title, desc, musicUrl, qhMusicUrl))
73+
func (s *Server) SendMusic(to string, mediaId, title, desc, musicUrl, qhMusicUrl string) *WxErr {
74+
return s.SendMsg(s.NewMusic(to, mediaId, title, desc, musicUrl, qhMusicUrl))
8075
}
8176

8277
// SendNews 发送客服news消息
83-
func (s *Server) SendNews(to string, id int, arts ...Article) *WxErr {
84-
return s.SendMsg(s.NewNews(to, id, arts...))
78+
func (s *Server) SendNews(to string, arts ...Article) *WxErr {
79+
return s.SendMsg(s.NewNews(to, arts...))
8580
}
8681

8782
// SendMpNews 发送加密新闻mpnews消息(仅企业号可用)
88-
func (s *Server) SendMpNews(to string, id int, arts ...MpArticle) *WxErr {
89-
return s.SendMsg(s.NewMpNews(to, id, arts...))
83+
func (s *Server) SendMpNews(to string, arts ...MpArticle) *WxErr {
84+
return s.SendMsg(s.NewMpNews(to, arts...))
9085
}
9186

9287
// SendMpNewsId 发送加密新闻mpnews消息(直接使用mediaId)
93-
func (s *Server) SendMpNewsId(to string, id int, mediaId string) *WxErr {
94-
return s.SendMsg(s.NewMpNewsId(to, id, mediaId))
88+
func (s *Server) SendMpNewsId(to string, mediaId string) *WxErr {
89+
return s.SendMsg(s.NewMpNewsId(to, mediaId))
90+
}
91+
92+
// SendMarkDown 发送加密新闻mpnews消息(直接使用mediaId)
93+
func (s *Server) SendMarkDown(to string, content string) *WxErr {
94+
return s.SendMsg(s.NewMarkDown(to, content))
9595
}
9696

97-
// SendMarkdown 发送加密新闻mpnews消息(直接使用mediaId)
98-
func (s *Server) SendMarkDown(to string, id int, content string) *WxErr {
99-
return s.SendMsg(s.NewMarkDown(to, id, content))
97+
// SendTaskCard 发送任务卡片taskcard消息
98+
func (s *Server) SendTaskCard(to string, Title, Desc, Url, TaskId, Btn string) *WxErr {
99+
return s.SendMsg(s.NewTaskCard(to, Title, Desc, Url, TaskId, Btn))
100100
}

0 commit comments

Comments
 (0)