Skip to content

Commit

Permalink
Merge pull request #37 from wangbokun/dev
Browse files Browse the repository at this point in the history
update wechat markdown
  • Loading branch information
wangbokun authored Jul 8, 2020
2 parents 7ae63ec + cce544f commit 7216377
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions wechat/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type WechatToken struct {
AccessToken string `json:"access_token"`
}

type weChatMessage struct {
type textWeChatMessage struct {
Text textMessageContent `yaml:"text,omitempty" json:"text,omitempty"`
// Taskcard taskcardMessageContent `yaml:"text,omitempty" json:"text,omitempty"`
ToUser string `yaml:"touser,omitempty" json:"touser,omitempty"`
Expand All @@ -23,7 +23,19 @@ type weChatMessage struct {
Type string `yaml:"msgtype,omitempty" json:"msgtype,omitempty"`
}

type markdownWeChatMessage struct {
Markdown markdownMessageContent `yaml:"markdown,omitempty" json:"markdown,omitempty"`
ToUser string `yaml:"touser,omitempty" json:"touser,omitempty"`
ToParty string `yaml:"toparty,omitempty" json:"toparty,omitempty"`
Totag string `yaml:"totag,omitempty" json:"totag,omitempty"`
AgentID string `yaml:"agentid,omitempty" json:"agentid,omitempty"`
Safe string `yaml:"safe,omitempty" json:"safe,omitempty"`
Type string `yaml:"msgtype,omitempty" json:"msgtype,omitempty"`
}

type markdownMessageContent struct {
Content string `json:"content"`
}

type textMessageContent struct {
Content string `json:"content"`
Expand All @@ -36,29 +48,58 @@ type weChatResponse struct {
}


func SendMsg(user, ctx, token, agentId string){

msg := &weChatMessage{
Text: textMessageContent{
Content: ctx,
},
ToUser: user,
ToParty: "",
Totag: "",
AgentID: agentId,
Type: "text", // msgType: text taskcard markdown ...
Safe: "0",
}

s,err:=types.ToString(msg)
if err!=nil{
log.Error("to string faild, Error: %s",err )
func SendMsg(user, mstype, ctx, token, agentId string) error {
switch mstype {
case "markdown":
msg := &markdownWeChatMessage{
Markdown: markdownMessageContent{
Content: ctx,
},
ToUser: user,
ToParty: "",
Totag: "",
AgentID: agentId,
Type: "markdown", // msgType: text taskcard markdown ...
Safe: "0",
}
s,err:=types.ToString(msg)
if err!=nil{
log.Error("to string faild, Error: %s",err )
return err
}
if err = send(s,token); err !=nil{
return err
}
default:
msg := &textWeChatMessage{
Text: textMessageContent{
Content: ctx,
},
ToUser: user,
ToParty: "",
Totag: "",
AgentID: agentId,
Type: "text", // msgType: text taskcard markdown ...
Safe: "0",
}
s,err:=types.ToString(msg)
if err!=nil{
log.Error("to string faild, Error: %s",err )
return err
}
if err = send(s,token); err !=nil{
return err
}
}
return nil
}

resp, error := client.Post(wxSendTextUrl + token,"application/json;charset=utf-8",s)
if error!=nil{
log.Error("wechat send faild, Error: %s",error )
func send(s,token string) error {
_, err := client.Post(wxSendTextUrl + token,"application/json;charset=utf-8",s)
if err !=nil{
log.Error("wechat send faild, Error: %s", err )
return err
}

log.Debug("wechat send status: %s",resp)
// log.Debug("wechat send status: %s",resp)
return nil
}

0 comments on commit 7216377

Please sign in to comment.