From 63c80b3c06b8f70f2b35c18a8e56ce05ab7513da Mon Sep 17 00:00:00 2001 From: ysicing Date: Thu, 19 Nov 2020 11:24:52 +0800 Subject: [PATCH] improvement: update workwechat api --- LICENSE | 13 +++++ README.md | 14 ++--- example/wxapp/workwechat.go | 56 ++++++++++++++++++ example/wxbot/workwxbot.go | 58 +++++++++++++++++++ message.go | 110 +++++++++++++++++++++++++++++++++--- workwechat_test.go | 19 ------- workwxbot.go | 13 +---- wxbot_test.go | 26 --------- 8 files changed, 240 insertions(+), 69 deletions(-) create mode 100644 LICENSE create mode 100644 example/wxapp/workwechat.go create mode 100644 example/wxbot/workwxbot.go delete mode 100644 workwechat_test.go delete mode 100644 wxbot_test.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7c69e8b --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2020 + + Copyright (C) 2020 ysicing + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md index d0b0090..2491fdc 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,17 @@ 企业微信自定义机器人 Golang API. -支持的消息类型: -- markdown 类型 +## 企业微信应用 -## 使用 +使用官方接口 + +## 微信机器人 ```go go get -u github.com/ysicing/workwxbot ``` -## 参数类型 - -信息类型,机器人id,文本信息,@人 +#### 说明 -可能不同于企业微信接口,内部信息部作了定制😢 \ No newline at end of file +`BotMessage`: 内部定制消息结构体 +`WxBotMessage`: 默认官方消息结构体 \ No newline at end of file diff --git a/example/wxapp/workwechat.go b/example/wxapp/workwechat.go new file mode 100644 index 0000000..611b48c --- /dev/null +++ b/example/wxapp/workwechat.go @@ -0,0 +1,56 @@ +package wxapp + +import ( + "github.com/ysicing/workwxbot" +) + +func main() { + corpid := "xxxx" + agentid := int64(1000000) + secretkey := "xxxx" + client := workwxbot.Client{CropID: corpid, AgentID: agentid, AgentSecret: secretkey} + md := workwxbot.Message{ + ToUser: "xxxx", + MsgType: "markdown", + Markdown: workwxbot.Content{Content: "### 测试"}, + } + client.Send(md) + text := workwxbot.Message{ + ToUser: "xxxx", + MsgType: "text", + Text: workwxbot.Content{Content: "文本测试"}, + } + client.Send(text) + textcard := workwxbot.Message{ + ToUser: "xxx", + MsgType: "textcard", + Textcard: workwxbot.TextCard{ + Title: "hahha", + Description: "xxxx", + Url: "https://jira.baidu.com", + Btntxt: "更多", + }, + } + client.Send(textcard) + news := workwxbot.Message{ + ToUser: "xxxx", + MsgType: "news", + News: workwxbot.News{ + Articles: []workwxbot.Article{ + { + Title: "中秋节礼品领取", + Description: "今年中秋节公司有豪礼相送", + Url: "https://jira.baidu.com", + Picurl: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", + }, + { + Title: "国庆节礼品领取", + Description: "今年国庆节公司有豪礼相送", + Url: "https://wiki.baidu.com", + Picurl: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", + }, + }, + }, + } + client.Send(news) +} diff --git a/example/wxbot/workwxbot.go b/example/wxbot/workwxbot.go new file mode 100644 index 0000000..3de8864 --- /dev/null +++ b/example/wxbot/workwxbot.go @@ -0,0 +1,58 @@ +package wxbot + +import ( + "github.com/ysicing/workwxbot" +) + +//func SendBot() { +// webhook := "http://wr.oa.baidu.com/api/Robot?token=12345678" +// wxbot := workwxbot.NewRobot(webhook) +// configid := "12b9a5fa-xxxx-xxxx-8d7f-a6b4f8c7184f" +// content := "测试 测试哈哈哈哈" +// msg := workwxbot.BotMessage{ +// MsgType: "markdown", +// ProgramType: "OA", +// IsSendNow: true, +// ConfigID: configid, +// Content: content, +// MentionedList: "@all", +// } +// wxbot.Send(msg) +//} + +func main() { + webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=12b9a5fa-xxxx-xxxx-8d7f-a6b4f8c7184f" + wxbot := workwxbot.NewRobot(webhook) + text := workwxbot.WxBotMessage{ + MsgType: "text", + BotText: workwxbot.BotText{ + Content: "测试", + MentionedList: []string{"@all"}, + }, + } + wxbot.Send(text) + markdown := workwxbot.WxBotMessage{ + MsgType: "markdown", + MarkDown: workwxbot.BotMarkDown{Content: "### 哈哈哈"}} + wxbot.Send(markdown) + news := workwxbot.WxBotMessage{ + MsgType: "news", + News: workwxbot.News{ + Articles: []workwxbot.Article{ + { + Title: "中秋节礼品领取", + Description: "今年中秋节公司有豪礼相送", + Url: "https://jira.baidu.com", + Picurl: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", + }, + { + Title: "国庆节礼品领取", + Description: "今年国庆节公司有豪礼相送", + Url: "https://wiki.baidu.com", + Picurl: "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", + }, + }, + }, + } + wxbot.Send(news) +} diff --git a/message.go b/message.go index ccf810e..26ecd8a 100644 --- a/message.go +++ b/message.go @@ -1,6 +1,8 @@ package workwxbot -import "time" +import ( + "time" +) const ( //msgTypeMarkdown = "markdown" @@ -18,6 +20,30 @@ type BotMessage struct { MentionedList string `json:"mentioned_list"` } +type WxBotMessage struct { + MsgType string `json:"msgtype"` + BotText BotText `json:"text"` + MarkDown BotMarkDown `json:"markdown"` + Image BotImage `json:"image"` + News News `json:"news"` + File Media `json:"file"` +} + +type BotText struct { + Content string `json:"content"` + MentionedList []string `json:"mentioned_list,omitempty"` + MentionedMobileList []string `json:"mentioned_mobile_list,omitempty"` +} + +type BotMarkDown struct { + Content string `json:"content"` +} + +type BotImage struct { + Base64 string `json:"base64"` + Md5 string `json:"md5"` +} + // Err 微信返回错误 type Err struct { ErrCode int `json:"errcode"` @@ -53,12 +79,82 @@ type Content struct { Content string `json:"content"` } +//Media 媒体内容 +type Media struct { + MediaID string `json:"media_id"` + Title string `json:"title,omitempty"` // 视频参数 + Description string `json:"description,omitempty"` // 视频参数 +} + +//Card 卡片 +type TextCard struct { + Title string `json:"title"` + Description string `json:"description"` + Url string `json:"url"` + Btntxt string `json:"btntxt"` +} + +//news 图文 +type News struct { + Articles []Article `json:"articles"` +} + +type Article struct { + Title string `json:"title"` + Description string `json:"description"` + Url string `json:"url"` + Picurl string `json:"picurl"` +} + +//mpnews 图文 +type MpNews struct { + Articles []MpArticle `json:"articles"` +} + +type MpArticle struct { + Title string `json:"title"` + ThumbMediaID string `json:"thumb_media_id"` + Author string `json:"author"` + ContentSourceUrl string `json:"content_source_url"` + Content string `json:"content"` + Digest string `json:"digest"` +} + +// 任务卡片 +type TaskCard struct { + Title string `json:"title"` + Description string `json:"description"` + Url string `json:"url"` + TaskID string `json:"task_id"` + Btn []TaskBtn `json:"btn"` +} + +type TaskBtn struct { + Key string `json:"key"` + Name string `json:"name"` + ReplaceName string `json:"replace_name"` + Color string `json:"color"` + IsBold bool `json:"is_bold"` +} + //Message 消息主体参数 type Message struct { - ToUser string `json:"touser"` - ToParty string `json:"toparty"` - ToTag string `json:"totag"` - MsgType string `json:"msgtype"` - AgentID int64 `json:"agentid"` - Markdown Content `json:"markdown"` + ToUser string `json:"touser"` + ToParty string `json:"toparty"` + ToTag string `json:"totag"` + MsgType string `json:"msgtype"` + AgentID int64 `json:"agentid"` + + Text Content `json:"text"` + Image Media `json:"image"` + Voice Media `json:"voice"` + Video Media `json:"video"` + File Media `json:"file"` + Textcard TextCard `json:"textcard"` + News News `json:"news"` + MpNews MpNews `json:"mpnews"` + Markdown Content `json:"markdown"` + Taskcard TaskCard `json:"taskcard"` + // EnableDuplicateCheck bool `json:"enable_duplicate_check"` // 表示是否开启重复消息检查,0表示否,1表示是,默认0 + // DuplicateCheckInterval int `json:"duplicate_check_interval"` // 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时 } diff --git a/workwechat_test.go b/workwechat_test.go deleted file mode 100644 index d6db968..0000000 --- a/workwechat_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package workwxbot_test - -import ( - "github.com/ysicing/workwxbot" - "testing" -) - -func TestClient_Send(t *testing.T) { - corpid := "wwxxxx" - agentid := int64(1000004) - secretkey := "xxxxxxx" - client := workwxbot.Client{CropID: corpid, AgentID: agentid, AgentSecret: secretkey} - msg := workwxbot.Message{ - ToUser: "ysicing", - MsgType: "markdown", - Markdown: workwxbot.Content{Content: "### 测试"}, - } - t.Log(client.Send(msg)) -} diff --git a/workwxbot.go b/workwxbot.go index 8b5d886..f3201f0 100644 --- a/workwxbot.go +++ b/workwxbot.go @@ -10,7 +10,7 @@ import ( // Roboter is the interface implemented by Robot that can send multiple types of messages. type Roboter interface { - SendMarkdown(MsgType string, ConfigID string, Content string, MentionedList string) error + Send(interface{}) error } // Robot represents a workwxbot custom robot that can send messages to groups. @@ -24,15 +24,8 @@ func NewRobot(webhook string) Roboter { } // SendMarkdown send a markdown type message. -func (r Robot) SendMarkdown(MsgType string, ConfigID string, Content string, MentionedList string) error { - return r.send(&BotMessage{ - MsgType: MsgType, - ProgramType: programType, - IsSendNow: isSendNow, - ConfigID: ConfigID, - Content: Content, - MentionedList: MentionedList, - }) +func (r Robot) Send(msg interface{}) error { + return r.send(msg) } type workRsp struct { diff --git a/wxbot_test.go b/wxbot_test.go deleted file mode 100644 index 5fe69bc..0000000 --- a/wxbot_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package workwxbot_test - -import ( - "github.com/ysicing/workwxbot" - "log" - "testing" -) - -func ExampleRobot_SendMarkdown() { - webhook := "http://wr.oa.local.com:89/api/Robot?token=xxxxxxxxxxxx" - wxbot := workwxbot.NewRobot(webhook) - configid := "6eb955fa-xxxx-4e36-xxxx-a6b44817181f" - content := "### 测试\n > 测试哈哈哈哈" - err := wxbot.SendMarkdown("text", configid, content, "all") - if err != nil { - log.Fatal(err) - } -} - -func TestRobot_SendMarkdown(t *testing.T) { - webhook := "http://wr.oa.local.com:89/api/Robot?token=xxxxxxxxxxxxxx" - wxbot := workwxbot.NewRobot(webhook) - configid := "6eb955fa-xxxx-4e36-xxxx-a6b44817181f" - content := "测试 测试哈哈哈哈" - t.Log(wxbot.SendMarkdown("text", configid, content, "@all")) -}