master
李光春 3 years ago
parent 0ced166fc8
commit c1572050b1

@ -1,26 +0,0 @@
package dingtalk
import (
"fmt"
msgtype2 "github.com/dtapps/go-library/dingtalk/msgtype"
"testing"
)
func TestName(t *testing.T) {
bot := DingBot{
Secret: "",
AccessToken: "",
}
param := Parameter{
"msgtype": msgtype2.TextStr,
"text": Parameter{
"content": "测试",
},
}
send, err := bot.Send(param)
fmt.Printf("send%v\n", send)
if err != nil {
t.Errorf("err%v\n", err)
return
}
}

@ -1,9 +0,0 @@
package msgtype
const (
TextStr = "text"
LinkStr = "link"
MarkdownStr = "markdown"
ActionCardStr = "actionCard"
FeedCardStr = "feedCard"
)

@ -1,8 +0,0 @@
package msgtype
const (
TextStr = "text"
NewsStr = "news"
MarkdownStr = "markdown"
fileStr = "file"
)

@ -0,0 +1,3 @@
package config
const Api = "https://oapi.dingtalk.com/robot/send"

@ -6,22 +6,15 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
params "github.com/dtapps/go-library/params"
config2 "github.com/dtapps/go-library/service/dingtalk/config"
message2 "github.com/dtapps/go-library/service/dingtalk/message"
djson2 "github.com/dtapps/go-library/utils/djson"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const api = "https://oapi.dingtalk.com/robot/send"
// Parameter 参数
type Parameter map[string]interface{}
// ParameterEncode 参数
type ParameterEncode []string
type DingBot struct {
Secret string
AccessToken string
@ -32,12 +25,16 @@ type response struct {
Errmsg string `json:"errmsg"`
}
func (bot *DingBot) Send(param Parameter) (response, error) {
func (bot *DingBot) Send(msg message2.Message) (response, error) {
timestamp := time.Now().UnixNano() / 1e6
var response response
signStr := sign(timestamp, bot.Secret)
dingUrl := fmt.Sprintf("%s?access_token=%s&timestamp=%d&sign=%s", api, bot.AccessToken, timestamp, signStr)
resp, e := http.Post(dingUrl, "application/json", strings.NewReader(param.getRequestData()))
dingUrl := fmt.Sprintf("%s?access_token=%s&timestamp=%d&sign=%s", config2.Api, bot.AccessToken, timestamp, signStr)
toString, err := djson2.MarshalToString(msg)
if err != nil {
return response, err
}
resp, e := http.Post(dingUrl, "application/json", strings.NewReader(toString))
if e != nil {
return response, e
}
@ -58,14 +55,3 @@ func sign(t int64, secret string) string {
result := hmac256.Sum(nil)
return base64.StdEncoding.EncodeToString(result)
}
// 获取请求数据
func (p Parameter) getRequestData() string {
// 公共参数
args := url.Values{}
// 请求参数
for key, val := range p {
args.Set(key, params.GetParamsString(val))
}
return args.Encode()
}

@ -0,0 +1,26 @@
package dingtalk
import (
"fmt"
message2 "github.com/dtapps/go-library/service/dingtalk/message"
"testing"
)
func TestName(t *testing.T) {
bot := DingBot{
Secret: "gSEC05342ba24a7eb2e1dbeae61b3df997eb1a97b7cda414566876e983f1db0fec0b",
AccessToken: "caad81de7f6b218bb7d085264d4885714c805cc80a460690a0d19db91a05dd174",
}
msg := message2.Message{
MsgType: message2.TextStr,
Text: message2.Text_{
Content: "测试",
},
}
send, err := bot.Send(msg)
fmt.Printf("send%v\n", send)
if err != nil {
t.Errorf("err%v\n", err)
return
}
}

@ -0,0 +1,59 @@
package message
type Message struct {
MsgType MsgType_ `json:"msgtype"`
Text Text_ `json:"text"`
Link Link_ `json:"link"`
Markdown Markdown_ `json:"markdown"`
ActionCard ActionCard_ `json:"actionCard"`
FeedCard FeedCard_ `json:"feedCard"`
}
// Text_ text类型
type Text_ struct {
Content string `json:"content"`
At At_ `json:"at"`
}
// At_ At类型
type At_ struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
// Link_ link类型
type Link_ struct {
Text string `json:"text"`
Title string `json:"title"`
PicUrl string `json:"picUrl"`
MessageUrl string `json:"messageUrl"`
}
// Markdown_ markdown类型
type Markdown_ struct {
Title string `json:"title"`
Text string `json:"text"`
At At_ `json:"at"`
}
// ActionCard_ 整体跳转actionCard
type ActionCard_ struct {
Title string `json:"title"`
Text string `json:"text"`
BtnOrientation string `json:"btnOrientation"`
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleUrl"`
HideAvatar string `json:"hideAvatar"`
BtnS []Btn_ `json:"btns"`
}
// Btn_ Btn类型
type Btn_ struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
}
// FeedCard_ FeedCard类型
type FeedCard_ struct {
Links []Link_ `json:"links"`
}

@ -0,0 +1,11 @@
package message
type MsgType_ string
const (
TextStr MsgType_ = "text"
LinkStr MsgType_ = "link"
MarkdownStr MsgType_ = "markdown"
ActionCardStr MsgType_ = "actionCard"
FeedCardStr MsgType_ = "feedCard"
)

@ -6,7 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"github.com/bitly/go-simplejson"
params2 "github.com/dtapps/go-library/params"
params3 "github.com/dtapps/go-library/utils/params"
"io"
"io/ioutil"
"net/http"
@ -60,7 +60,7 @@ func sign(params Parameter, customerKey string) string {
query := bytes.NewBufferString(customerKey)
for _, k := range keys {
query.WriteString(k)
query.WriteString(params2.GetParamsString(params[k]))
query.WriteString(params3.GetParamsString(params[k]))
}
// MD5加密
h := md5.New()

@ -2,7 +2,7 @@ package kashangwl
import (
"fmt"
_url "github.com/dtapps/go-library/kashangwl/url"
_url2 "github.com/dtapps/go-library/service/kashangwl/url"
"testing"
)
@ -15,7 +15,7 @@ func TestName(t *testing.T) {
param := Parameter{
"order_id": 827669582783,
}
send, err := wl.Send(_url.Order, param)
send, err := wl.Send(_url2.Order, param)
fmt.Printf("send%s\n", send)
if err != nil {
t.Errorf("err%v\n", err)

@ -5,7 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"github.com/bitly/go-simplejson"
params2 "github.com/dtapps/go-library/params"
params3 "github.com/dtapps/go-library/utils/params"
"io"
"io/ioutil"
"net/http"
@ -61,7 +61,7 @@ func sign(params Parameter, clientSecret string) string {
query := bytes.NewBufferString(clientSecret)
for _, k := range keys {
query.WriteString(k)
query.WriteString(params2.GetParamsString(params[k]))
query.WriteString(params3.GetParamsString(params[k]))
}
query.WriteString(clientSecret)
// MD5加密
@ -89,7 +89,7 @@ func (p Parameter) getRequestData() string {
args := url.Values{}
// 请求参数
for key, val := range p {
args.Set(key, params2.GetParamsString(val))
args.Set(key, params3.GetParamsString(val))
}
return args.Encode()
}

@ -2,7 +2,7 @@ package pinduoduo
import (
"fmt"
_type2 "github.com/dtapps/go-library/pinduoduo/type"
"github.com/dtapps/go-library/service/pinduoduo/type"
"testing"
)
@ -14,7 +14,7 @@ func TestName(t *testing.T) {
param := Parameter{
"keyword": "小米",
}
send, err := duo.Send(_type2.GoodsSearch, param)
send, err := duo.Send(_type.GoodsSearch, param)
fmt.Printf("send%v\n", send)
if err != nil {
t.Errorf("错误:%v", err)

@ -0,0 +1,3 @@
package config
const Api = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"

@ -0,0 +1,39 @@
package message
type Message struct {
MsgType MsgType_ `json:"msgtype"`
Text Text_ `json:"text"`
Markdown Markdown_ `json:"markdown"`
News News_ `json:"link"`
File File_ `json:"file"`
}
// Text_ text类型
type Text_ struct {
Content string `json:"content"` // 文本内容最长不超过2048个字节必须是utf8编码
MentionedList []string `json:"mentioned_list"` // userid的列表提醒群中的指定成员(@某个成员)@all表示提醒所有人如果开发者获取不到userid可以使用mentioned_mobile_list
MentionedMobileList []string `json:"mentioned_mobile_list"` // 手机号列表,提醒手机号对应的群成员(@某个成员)@all表示提醒所有人
}
// Markdown_ markdown类型
type Markdown_ struct {
Content string `json:"content"` // markdown内容最长不超过4096个字节必须是utf8编码
}
// News_ news类型
type News_ struct {
Articles []articles `json:"articles"` // 图文消息一个图文消息支持1到8条图文
}
// articles
type articles struct {
Title string `json:"title"` // 标题不超过128个字节超过会自动截断
Description string `json:"description"` // 描述不超过512个字节超过会自动截断
Url string `json:"url"` // 点击后跳转的链接。
Picurl string `json:"picurl"` // 图文消息的图片链接支持JPG、PNG格式较好的效果为大图 1068*455小图150*150。
}
// File_ file类型
type File_ struct {
MediaId string `json:"media_id"` // 文件id通过下文的文件上传接口获取
}

@ -0,0 +1,10 @@
package message
type MsgType_ string
const (
TextStr MsgType_ = "text"
NewsStr MsgType_ = "news"
MarkdownStr MsgType_ = "markdown"
fileStr MsgType_ = "file"
)

@ -3,21 +3,14 @@ package qywechat
import (
"encoding/json"
"fmt"
params2 "github.com/dtapps/go-library/params"
"github.com/dtapps/go-library/service/qywechat/config"
"github.com/dtapps/go-library/service/qywechat/message"
"github.com/dtapps/go-library/utils/djson"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const api = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
// Parameter 参数
type Parameter map[string]interface{}
// ParameterEncode 参数
type ParameterEncode []string
type QyBot struct {
Key string
}
@ -30,10 +23,14 @@ type response struct {
CreatedAt string `json:"created_at"`
}
func (bot *QyBot) Send(param Parameter) (response, error) {
func (bot *QyBot) Send(msg message.Message) (response, error) {
var response response
qyUrl := fmt.Sprintf("%s?key=%s", api, bot.Key)
resp, e := http.Post(qyUrl, "application/json", strings.NewReader(param.getRequestData()))
qyUrl := fmt.Sprintf("%s?key=%s", config.Api, bot.Key)
toString, err := djson.MarshalToString(msg)
if err != nil {
return response, err
}
resp, e := http.Post(qyUrl, "application/json", strings.NewReader(toString))
if e != nil {
return response, e
}
@ -46,14 +43,3 @@ func (bot *QyBot) Send(param Parameter) (response, error) {
}
return response, nil
}
// 获取请求数据
func (p Parameter) getRequestData() string {
// 公共参数
args := url.Values{}
// 请求参数
for key, val := range p {
args.Set(key, params2.GetParamsString(val))
}
return args.Encode()
}

@ -2,7 +2,7 @@ package qywechat
import (
"fmt"
msgtype2 "github.com/dtapps/go-library/qywechat/msgtype"
message2 "github.com/dtapps/go-library/service/qywechat/message"
"testing"
)
@ -10,13 +10,13 @@ func TestName(t *testing.T) {
bot := QyBot{
Key: "",
}
param := Parameter{
"msgtype": msgtype2.TextStr,
"text": Parameter{
"content": "测试",
msg := message2.Message{
MsgType: message2.TextStr,
Text: message2.Text_{
Content: "测试",
},
}
send, err := bot.Send(param)
send, err := bot.Send(msg)
fmt.Printf("send%v\n", send)
if err != nil {
t.Errorf("err%v\n", err)

@ -3,7 +3,7 @@ package dhttp
import (
"encoding/json"
"github.com/bitly/go-simplejson"
dRequest "github.com/dtapps/go-library/drequest"
"github.com/dtapps/go-library/utils/drequest"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/body"
)
@ -21,7 +21,7 @@ func GetJson(url string, data string, headers map[string]interface{}) (res []byt
// 设置请求首部Header
req.SetHeader(key, value.(string))
}
req.SetHeader("User-Agent", dRequest.GetUserAgent())
req.SetHeader("User-Agent", drequest.GetUserAgent())
// 发送请求获取响应对象res
response, err := req.Send()
if err != nil {
@ -49,7 +49,7 @@ func PostJson(url string, data map[string]interface{}, headers map[string]interf
// 设置请求首部Header
req.SetHeader(key, value.(string))
}
req.SetHeader("User-Agent", dRequest.GetUserAgent())
req.SetHeader("User-Agent", drequest.GetUserAgent())
// 发送请求获取响应对象res
response, err := req.Send()
if err != nil {
@ -76,7 +76,7 @@ func PostXml(url string, data map[string]string, headers map[string]interface{})
// 设置请求首部Header
req.SetHeader(key, value.(string))
}
req.SetHeader("User-Agent", dRequest.GetUserAgent())
req.SetHeader("User-Agent", drequest.GetUserAgent())
// 发送请求获取响应对象res
response, err := req.Send()
if err != nil {

@ -9,3 +9,11 @@ func Encode(v interface{}) (string, error) {
}
return string(bytes), nil
}
func MarshalToString(msg interface{}) (string, error) {
j, e := json.Marshal(msg)
if e != nil {
return "", e
}
return string(j), nil
}

@ -2,10 +2,10 @@ package duuid_test
import (
"fmt"
"github.com/dtapps/go-library/duuid"
duuid2 "github.com/dtapps/go-library/utils/duuid"
"testing"
)
func TestName(t *testing.T) {
fmt.Println(duuid.GenUUID())
fmt.Println(duuid2.GenUUID())
}
Loading…
Cancel
Save