Skip to content

Commit

Permalink
improvement: update workwechat api
Browse files Browse the repository at this point in the history
  • Loading branch information
ysicing committed Nov 19, 2020
1 parent a5229b5 commit 63c80b3
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 69 deletions.
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2020

Copyright (C) 2020 ysicing <[email protected]>

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.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

企业微信自定义机器人 Golang API.

支持的消息类型:
- markdown 类型
## 企业微信应用

## 使用
使用官方接口

## 微信机器人

```go
go get -u github.com/ysicing/workwxbot
```

## 参数类型

信息类型,机器人id,文本信息,@人
#### 说明

可能不同于企业微信接口,内部<del>信息部</del>作了定制😢
`BotMessage`: 内部定制消息结构体
`WxBotMessage`: 默认官方消息结构体
56 changes: 56 additions & 0 deletions example/wxapp/workwechat.go
Original file line number Diff line number Diff line change
@@ -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)
}
58 changes: 58 additions & 0 deletions example/wxbot/workwxbot.go
Original file line number Diff line number Diff line change
@@ -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)
}
110 changes: 103 additions & 7 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package workwxbot

import "time"
import (
"time"
)

const (
//msgTypeMarkdown = "markdown"
Expand All @@ -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"`
Expand Down Expand Up @@ -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小时
}
19 changes: 0 additions & 19 deletions workwechat_test.go

This file was deleted.

13 changes: 3 additions & 10 deletions workwxbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
26 changes: 0 additions & 26 deletions wxbot_test.go

This file was deleted.

0 comments on commit 63c80b3

Please sign in to comment.