From 29acffd4969e439e67639a1b16213f67b343e491 Mon Sep 17 00:00:00 2001 From: bozhang75 Date: Mon, 1 Aug 2022 09:36:11 +0800 Subject: [PATCH] add webhook --- ...webhook_upload.go => qw_webhook_upload.go} | 4 +- examples/example/qy_access_token.go | 12 ++- examples/example/qy_company_department.go | 34 ++++---- examples/example/qy_customer.go | 20 +++-- examples/example/qy_customer_tag.go | 47 +++++------ examples/example/qy_serve.go | 16 ++-- examples/main.go | 28 ++++--- work/webhook/webhook.go | 79 +++++++++++-------- 8 files changed, 121 insertions(+), 119 deletions(-) rename examples/example/{webhook_upload.go => qw_webhook_upload.go} (94%) diff --git a/examples/example/webhook_upload.go b/examples/example/qw_webhook_upload.go similarity index 94% rename from examples/example/webhook_upload.go rename to examples/example/qw_webhook_upload.go index c3a0b67..23d24d1 100644 --- a/examples/example/webhook_upload.go +++ b/examples/example/qw_webhook_upload.go @@ -1,15 +1,15 @@ /** * @Time : 2022/7/29 15:02 * @Author : soupzhb@gmail.com - * @File : webhook_upload.go + * @File : qw_webhook_upload.go * @Software: GoLand */ package example import ( - "../wxconf" "fmt" + "github.com/yijizhichang/wechat-sdk/examples/wxconf" "io" "net/http" "os" diff --git a/examples/example/qy_access_token.go b/examples/example/qy_access_token.go index 08d5dde..a5eca54 100644 --- a/examples/example/qy_access_token.go +++ b/examples/example/qy_access_token.go @@ -12,12 +12,10 @@ import ( "github.com/yijizhichang/wechat-sdk/examples/wxconf" ) -func QyAccessToken() { - token, err := wxconf.QyWechatClint.GetQyAccessToken("VyMmm3hy5n7Q5t23tozkraUtuOt_sdfsadfsfsDCXGEQ") - token2, err2 := wxconf.QyWechatClint.GetQyAccessToken("2rLl96BjFobh7Y_lD6sdfsfdsfsfsfsdjWY3QwIu4") - fmt.Printf("企业微token:",token, err) - fmt.Printf("企业微token2:",token2, err2) - +func QyAccessToken() { + token, err := wxconf.QyWechatClient.GetQyAccessToken("VyMmm3hy5n7Q5t23tozkraUtuOt_sdfsadfsfsDCXGEQ") + token2, err2 := wxconf.QyWechatClient.GetQyAccessToken("2rLl96BjFobh7Y_lD6sdfsfdsfsfsfsdjWY3QwIu4") + fmt.Printf("企业微token:", token, err) + fmt.Printf("企业微token2:", token2, err2) } - diff --git a/examples/example/qy_company_department.go b/examples/example/qy_company_department.go index ac644c2..5a70a37 100644 --- a/examples/example/qy_company_department.go +++ b/examples/example/qy_company_department.go @@ -12,39 +12,35 @@ import ( "github.com/yijizhichang/wechat-sdk/examples/wxconf" ) -func QyGetDepartment(token string){ +func QyGetDepartment(token string) { - department := wxconf.QyWechatClint.GetDepartment() - res, err := department.GetDepartmentList(token,0) + department := wxconf.QyWechatClient.GetDepartment() + res, err := department.GetDepartmentList(token, 0) if err != nil { - fmt.Printf("QyGetDepartment GetDepartmentList Err: %+v",err) + fmt.Printf("QyGetDepartment GetDepartmentList Err: %+v", err) } - fmt.Printf("QyGetDepartment GetDepartmentList Res: %+v",res) + fmt.Printf("QyGetDepartment GetDepartmentList Res: %+v", res) } +func QyGetDepartmentSimpleUserList(token string) { -func QyGetDepartmentSimpleUserList(token string){ - - departmentUser := wxconf.QyWechatClint.GetDepartmentUser() - res, err := departmentUser.GetDepartmentSimpleUserList(token,5,0) + departmentUser := wxconf.QyWechatClient.GetDepartmentUser() + res, err := departmentUser.GetDepartmentSimpleUserList(token, 5, 0) if err != nil { - fmt.Printf("QyGetDepartmentSimpleUserList GetDepartmentSimpleUserList Err: %+v",err) + fmt.Printf("QyGetDepartmentSimpleUserList GetDepartmentSimpleUserList Err: %+v", err) } - fmt.Printf("QyGetDepartmentSimpleUserList GetDepartmentSimpleUserList Res: %+v",res) + fmt.Printf("QyGetDepartmentSimpleUserList GetDepartmentSimpleUserList Res: %+v", res) } -func QyGetDepartmentUserList(token string){ +func QyGetDepartmentUserList(token string) { - departmentUser := wxconf.QyWechatClint.GetDepartmentUser() - res, err := departmentUser.GetDepartmentUserList(token,7,0) + departmentUser := wxconf.QyWechatClient.GetDepartmentUser() + res, err := departmentUser.GetDepartmentUserList(token, 7, 0) if err != nil { - fmt.Printf("QyGetDepartmentUserList GetDepartmentUserList Err: %+v",err) + fmt.Printf("QyGetDepartmentUserList GetDepartmentUserList Err: %+v", err) } - fmt.Printf("QyGetDepartmentUserList GetDepartmentUserList Res: %+v",res) + fmt.Printf("QyGetDepartmentUserList GetDepartmentUserList Res: %+v", res) } - - - diff --git a/examples/example/qy_customer.go b/examples/example/qy_customer.go index 3d8ee77..82a91a7 100644 --- a/examples/example/qy_customer.go +++ b/examples/example/qy_customer.go @@ -12,22 +12,20 @@ import ( "github.com/yijizhichang/wechat-sdk/examples/wxconf" ) -func QyGetCustomerList(token string) { - cus := wxconf.QyWechatClint.GetCustomer() +func QyGetCustomerList(token string) { + cus := wxconf.QyWechatClient.GetCustomer() res, err := cus.GetQyCustomerList(token, "xinyu888") if err != nil { - fmt.Printf("Err: %+v",err) + fmt.Printf("Err: %+v", err) } - fmt.Printf("Res: %+v",res) + fmt.Printf("Res: %+v", res) } -func QyGetCustomerView(token string) { - cus := wxconf.QyWechatClint.GetCustomer() - res, err := cus.GetQyCustomerView(token, "wmt0zCEAAAfyx96crCFRHwLxqtR3_5yA","") //wmt0zCEAAAfyx96crCFRHwLxqtR3_3yA +func QyGetCustomerView(token string) { + cus := wxconf.QyWechatClient.GetCustomer() + res, err := cus.GetQyCustomerView(token, "wmt0zCEAAAfyx96crCFRHwLxqtR3_5yA", "") //wmt0zCEAAAfyx96crCFRHwLxqtR3_3yA if err != nil { - fmt.Printf("Err: %+v",err) + fmt.Printf("Err: %+v", err) } - fmt.Printf("Res: %+v",res) + fmt.Printf("Res: %+v", res) } - - diff --git a/examples/example/qy_customer_tag.go b/examples/example/qy_customer_tag.go index 63ec19d..a47461d 100644 --- a/examples/example/qy_customer_tag.go +++ b/examples/example/qy_customer_tag.go @@ -13,19 +13,19 @@ import ( "github.com/yijizhichang/wechat-sdk/work/customer" ) -func QyGetCustomerTagList(token string){ +func QyGetCustomerTagList(token string) { - customerTag := wxconf.QyWechatClint.GetCustomerTag() - res, err := customerTag.GetCustomerTagList(token, customer.CusTagReq{TagId:[]string{}, GroupId: []string{}}) + customerTag := wxconf.QyWechatClient.GetCustomerTag() + res, err := customerTag.GetCustomerTagList(token, customer.CusTagReq{TagId: []string{}, GroupId: []string{}}) if err != nil { - fmt.Printf("QyGetCustomerTagList GetCustomerTagList Err: %+v",err) + fmt.Printf("QyGetCustomerTagList GetCustomerTagList Err: %+v", err) } - fmt.Printf("QyGetCustomerTagList GetCustomerTagList Res: %+v",res) + fmt.Printf("QyGetCustomerTagList GetCustomerTagList Res: %+v", res) } -func QyCreateCustomerTag(token string){ - customerTag := wxconf.QyWechatClint.GetCustomerTag() +func QyCreateCustomerTag(token string) { + customerTag := wxconf.QyWechatClient.GetCustomerTag() req := new(customer.CreateCusTagReq) req.GroupName = "技术Mark" addTag1 := customer.AddTag{ @@ -39,56 +39,51 @@ func QyCreateCustomerTag(token string){ req.Tag = append(req.Tag, addTag1, addTag2) res, err := customerTag.CreateCustomerTag(token, *req) if err != nil { - fmt.Printf("QyCreateCustomerTag CreateCustomerTag Err: %+v",err) + fmt.Printf("QyCreateCustomerTag CreateCustomerTag Err: %+v", err) } - fmt.Printf("QyCreateCustomerTag CreateCustomerTag Res: %+v",res) + fmt.Printf("QyCreateCustomerTag CreateCustomerTag Res: %+v", res) } -func QyUpdateCustomerTag(token string){ - customerTag := wxconf.QyWechatClint.GetCustomerTag() +func QyUpdateCustomerTag(token string) { + customerTag := wxconf.QyWechatClient.GetCustomerTag() req := new(customer.UpdateCusTagReq) req.Id = "ett0zCEAAAqlT6DRVQUh6CJnUhD2mZTA" req.Name = "m3" req.Order = 5 - res, err := customerTag.UpdateCustomerTag(token, *req) if err != nil { - fmt.Printf("QyUpdateCustomerTag UpdateCustomerTag Err: %+v",err) + fmt.Printf("QyUpdateCustomerTag UpdateCustomerTag Err: %+v", err) } - fmt.Printf("QyUpdateCustomerTag UpdateCustomerTag Res: %+v",res) + fmt.Printf("QyUpdateCustomerTag UpdateCustomerTag Res: %+v", res) } -func QyDelCustomerTag(token string){ - customerTag := wxconf.QyWechatClint.GetCustomerTag() +func QyDelCustomerTag(token string) { + customerTag := wxconf.QyWechatClient.GetCustomerTag() req := new(customer.DelCusTagReq) req.TagId = []string{"ett0zCEAAAqlT6DRVQUh6CJnUhD2mZTA"} - res, err := customerTag.DelCustomerTag(token, *req) if err != nil { - fmt.Printf("QyDelCustomerTag DelCustomerTag Err: %+v",err) + fmt.Printf("QyDelCustomerTag DelCustomerTag Err: %+v", err) } - fmt.Printf("QyDelCustomerTag DelCustomerTag Res: %+v",res) + fmt.Printf("QyDelCustomerTag DelCustomerTag Res: %+v", res) } -func QyMarkTag(token string){ - customerTag := wxconf.QyWechatClint.GetCustomerTag() +func QyMarkTag(token string) { + customerTag := wxconf.QyWechatClient.GetCustomerTag() req := new(customer.MarkTagReq) req.Userid = "xinyu8521" req.ExternalUserid = "wmt0zCEAAAfyx96crCFRHwLxqtR3_3yA" req.AddTag = []string{"ett0zCEAAAw3i1L_kh9gQIIc6A-XZ6tQ"} - res, err := customerTag.MarkCustomerTag(token, *req) if err != nil { - fmt.Printf("QyMarkTag MarkCustomerTag Err: %+v",err) + fmt.Printf("QyMarkTag MarkCustomerTag Err: %+v", err) } - fmt.Printf("QyMarkTag MarkCustomerTag Res: %+v",res) + fmt.Printf("QyMarkTag MarkCustomerTag Res: %+v", res) } - - diff --git a/examples/example/qy_serve.go b/examples/example/qy_serve.go index ebd62e9..32c6ac8 100644 --- a/examples/example/qy_serve.go +++ b/examples/example/qy_serve.go @@ -21,7 +21,7 @@ import ( func QyServe(rw http.ResponseWriter, req *http.Request) { fmt.Println("QyServe 1111") // 传入request和responseWriter - server := wxconf.QyWechatClint.GetQyServer(req, rw) + server := wxconf.QyWechatClient.GetQyServer(req, rw) fmt.Println("QyServe 2222") //设置接收消息的处理方法 server.SetMessageHandler(func(msg message.MixMessage) *response.Reply { @@ -33,8 +33,8 @@ func QyServe(rw http.ResponseWriter, req *http.Request) { fmt.Println("QyServe 3333") fmt.Println("msg %+v", msg) - msgJson,_ := json.Marshal(msg) - fmt.Println("msgJson",string(msgJson)) + msgJson, _ := json.Marshal(msg) + fmt.Println("msgJson", string(msgJson)) //根据微信回调时的消息类型,来相应获取对应消息明细 switch msg.MsgCommon.MsgType { @@ -93,7 +93,7 @@ func QyServe(rw http.ResponseWriter, req *http.Request) { fmt.Println("图文消息:", reStr) msgType = message.MsgTypeNews default: - reStr ="" + reStr = "" msgType = message.MsgTypeNothing } @@ -116,12 +116,11 @@ func QyServe(rw http.ResponseWriter, req *http.Request) { server.Send() } - //与微信交互服务地址,sdk包返回需要给微信的内容,由应用发送给微信 func QyResponseServe(rw http.ResponseWriter, req *http.Request) { // 传入request和responseWriter - server := wxconf.QyWechatClint.GetQyResponseServer(req) + server := wxconf.QyWechatClient.GetQyResponseServer(req) //设置接收消息的处理方法 server.SetMessageHandler(func(msg message.MixMessage) *response.Reply { var reStr interface{} @@ -211,7 +210,7 @@ func QyResponseServe(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(200) //rw.Header().Set("Content-Type",contentType) rw.Write([]byte(str)) - fmt.Println("echostrExist is true",str,contentType) + fmt.Println("echostrExist is true", str, contentType) return } //发送回复的消息 @@ -221,6 +220,5 @@ func QyResponseServe(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(200) rw.Write([]byte(str2)) - fmt.Println("send to wx",str2,contentType2) + fmt.Println("send to wx", str2, contentType2) } - diff --git a/examples/main.go b/examples/main.go index 549d136..1e8c9b3 100644 --- a/examples/main.go +++ b/examples/main.go @@ -7,26 +7,29 @@ import ( "github.com/yijizhichang/wechat-sdk/examples/cache" "github.com/yijizhichang/wechat-sdk/examples/example" "github.com/yijizhichang/wechat-sdk/examples/wxconf" - wxCache "github.com/yijizhichang/wechat-sdk/util/cache" "net/http" ) // 测试参数 // go run example.go -appid='your appdi' -appsecret='your appsecret' -token='your token' -port='80' +/* 测试公众号时打开对应参数 var appid = flag.String("appid", "your AppID", "appid") var appsecret = flag.String("appsecret", "your AppSecret", "appsecret") var token = flag.String("token", "your Token", "token") var aeskey = flag.String("asekey", "your EncodingAesKey", "asekey") var port = flag.String("port", "80", "port") +*/ -//qw +//qw 测试企微时,打开对应参数 // go run main.go -cropid='your cropid' -token='your token' -aeskey='your aeskey' -cropsecret='your cropsecret' -rpkey='your rpkey' -port='80' // go run main.go -cropid='111' -cropsecret='222' -rpkey='333' var cropid = flag.String("cropid", "your CorpID", "cropid") var cropsecret = flag.String("cropsecret", "your CorpSecret", "cropsecret") var rpkey = flag.String("rpkey", "your RasPrivateKey", "rpkey") +var port = flag.String("port", "9999", "port") // 参数配置 +/* func getWxConfig(cacheModel wxCache.Cache) *wechat.Config { config := &wechat.Config{ AppID: *appid, // 开发者ID(AppID) @@ -43,13 +46,15 @@ func getWxConfig(cacheModel wxCache.Cache) *wechat.Config { return config } +*/ + func getQyWechatConfig(fileCache cache.FileClient) *wechat.QyConfig { config := &wechat.QyConfig{ CorpID: *cropid, // 企业ID CorpSecret: *cropsecret, // 应用的凭证密钥; 每个应用有独立的secret,获取到的access_token只能本应用使用,所以每个应用的access_token应该分开来获取 RasPrivateKey: *rpkey, // 消息加密私钥 - Token: *token, // 令牌(Token) - EncodingAESKey: *aeskey, // 消息加解密密钥 EncodingAESKey + Token: "", //*token, // 令牌(Token) + EncodingAESKey: "", //*aeskey, // 消息加解密密钥 EncodingAESKey ThirdAccessToken: false, //是用其他应用生成的access_token Cache: fileCache, //缓存 ProxyUrl: "", //代理地址 @@ -119,16 +124,19 @@ func main() { } fileCache := cache.NewFileClient(file) wxconf.QyWechatClient = wechat.NewQyWechat(getQyWechatConfig(fileCache)) - fmt.Printf("qy param:", *cropid, *token, *aeskey, *cropsecret, *rpkey) + //fmt.Printf("qy param:", *cropid, *token, *aeskey, *cropsecret, *rpkey) //测试 //example.QyAccessToken() //获取企业access_token - token, err := wxconf.QyWechatClient.GetQyAccessToken(*cropsecret) - if err != nil { - fmt.Printf("GetQyAccessToken Err: %+v", err) - } - fmt.Println("token=%s", token) + /* + token, err := wxconf.QyWechatClient.GetQyAccessToken(*cropsecret) + if err != nil { + fmt.Printf("GetQyAccessToken Err: %+v", err) + } + fmt.Println("token=%s", token) + + */ //example.QyGetCustomerTagList(token) //获取企业标签库 //example.QyCreateCustomerTag(token) //创建企业标签 //example.QyUpdateCustomerTag(token) //修改企业标签 diff --git a/work/webhook/webhook.go b/work/webhook/webhook.go index 655bbf4..abf2880 100644 --- a/work/webhook/webhook.go +++ b/work/webhook/webhook.go @@ -216,22 +216,10 @@ type SendTemplateCardTextWebhookReq struct { Title string `json:"title"` QuoteText string `json:"quote_text"` } `json:"quote_area"` - SubTitleText string `json:"sub_title_text"` - HorizontalContentList []struct { - Keyname string `json:"keyname"` - Value string `json:"value"` - Type int `json:"type,omitempty"` - Url string `json:"url,omitempty"` - MediaId string `json:"media_id,omitempty"` - } `json:"horizontal_content_list"` - JumpList []struct { - Type int `json:"type"` - Url string `json:"url,omitempty"` - Title string `json:"title"` - Appid string `json:"appid,omitempty"` - Pagepath string `json:"pagepath,omitempty"` - } `json:"jump_list"` - CardAction struct { + SubTitleText string `json:"sub_title_text"` + HorizontalContentList []HorizontalContentItem `json:"horizontal_content_list"` + JumpList []JumpItem `json:"jump_list"` + CardAction struct { Type int `json:"type"` Url string `json:"url"` Appid string `json:"appid"` @@ -240,6 +228,22 @@ type SendTemplateCardTextWebhookReq struct { } `json:"template_card"` } +type HorizontalContentItem struct { + Keyname string `json:"keyname"` + Value string `json:"value"` + Type int `json:"type,omitempty"` + Url string `json:"url,omitempty"` + MediaId string `json:"media_id,omitempty"` +} + +type JumpItem struct { + Type int `json:"type"` + Url string `json:"url,omitempty"` + Title string `json:"title"` + Appid string `json:"appid,omitempty"` + Pagepath string `json:"pagepath,omitempty"` +} + func (m *Webhook) SendTemplateCardTextMessage(webhook string, req SendTemplateCardTextWebhookReq) (result *util.WxError, err error) { response, err := util.PostJSON(webhook, req, m.ProxyUrl) @@ -291,25 +295,10 @@ type SendTemplateCardNewsWebhookReq struct { Title string `json:"title"` QuoteText string `json:"quote_text"` } `json:"quote_area"` - VerticalContentList []struct { - Title string `json:"title"` - Desc string `json:"desc"` - } `json:"vertical_content_list"` - HorizontalContentList []struct { - Keyname string `json:"keyname"` - Value string `json:"value"` - Type int `json:"type,omitempty"` - Url string `json:"url,omitempty"` - MediaId string `json:"media_id,omitempty"` - } `json:"horizontal_content_list"` - JumpList []struct { - Type int `json:"type"` - Url string `json:"url,omitempty"` - Title string `json:"title"` - Appid string `json:"appid,omitempty"` - Pagepath string `json:"pagepath,omitempty"` - } `json:"jump_list"` - CardAction struct { + VerticalContentList []VerticalContentNewsItem `json:"vertical_content_list"` + HorizontalContentList []HorizontalContentNewsItem `json:"horizontal_content_list"` + JumpList []JumpNewsItem `json:"jump_list"` + CardAction struct { Type int `json:"type"` Url string `json:"url"` Appid string `json:"appid"` @@ -318,6 +307,26 @@ type SendTemplateCardNewsWebhookReq struct { } `json:"template_card"` } +type VerticalContentNewsItem struct { + Title string `json:"title"` + Desc string `json:"desc"` +} +type HorizontalContentNewsItem struct { + Keyname string `json:"keyname"` + Value string `json:"value"` + Type int `json:"type,omitempty"` + Url string `json:"url,omitempty"` + MediaId string `json:"media_id,omitempty"` +} + +type JumpNewsItem struct { + Type int `json:"type"` + Url string `json:"url,omitempty"` + Title string `json:"title"` + Appid string `json:"appid,omitempty"` + Pagepath string `json:"pagepath,omitempty"` +} + func (m *Webhook) SendTemplateCardNewsMessage(webhook string, req SendTemplateCardNewsWebhookReq) (result *util.WxError, err error) { response, err := util.PostJSON(webhook, req, m.ProxyUrl)