Skip to content

Commit

Permalink
add ./test/main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
happyh committed Apr 19, 2018
1 parent af04a52 commit 416410a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
4 changes: 2 additions & 2 deletions accesstoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

func TestAccessToken(t *testing.T) {
appId := "**********"
appSecret := "**********"
appId := "wx5ea56cd113c8a5b4"
appSecret := "3c17e5664dd3f9f05f960c49b0aca5f0"
RefreshAccessToken(appId, appSecret)

time.Sleep(3 * time.Second)
Expand Down
10 changes: 9 additions & 1 deletion custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
CustomHeadingURL = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=%s&kf_account=%s"
CustomListURL = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=%s"
CustomMsgURL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s"

TempleteURL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"
)

// Custom 客服帐号
Expand Down Expand Up @@ -172,5 +174,11 @@ func SendCustomMsg(openId string, msg CustMsg) (err error) {
openId, msgType, msgType, js)

url := fmt.Sprintf(CustomMsgURL, AccessToken())
return Post(url, []byte(jsonStr), nil)
return PostJs(url, []byte(jsonStr), nil)
}

func SendTempleMsg(openId string, CustMsg string) (err error) {
url := fmt.Sprintf(TempleteURL, AccessToken())
return PostJs(url, []byte(CustMsg), nil)

}
8 changes: 8 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func Post(url string, v interface{}, wxr wxResp) (err error) {
return parseWXResp(resp, wxr)
}

func PostJs(url string, js []byte, wxr wxResp) (err error) {
resp, err := client.Post(url, contentType, bytes.NewBuffer(js))
if err != nil {
return err
}
return parseWXResp(resp, wxr)
}

// Upload 工具类, 上传文件
func Upload(url, fieldName string, file *os.File, wxr wxResp, desc ...string) (err error) {
buf := &bytes.Buffer{}
Expand Down
6 changes: 5 additions & 1 deletion menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ func TestCreateMenu(t *testing.T) {

err := CreateMenu(buttons)
if err != nil {
t.Error(err)
t.Error("CreateMenu:", err)
t.FailNow()
} else {
t.Logf("CreateMenu OK")
}
}

/*
func TestGetMenu(t *testing.T) {
buttons, err := GetMenu()
if err != nil {
Expand Down Expand Up @@ -90,3 +93,4 @@ func TestGetMenuInfo(t *testing.T) {
t.Logf("%v", mi)
}
*/
2 changes: 1 addition & 1 deletion test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"github.com/happyh/weixin"
"time"
)

func main() {

appId := "wx5ea56cd113c8a5b4"
appSecret := "3c17e5664dd3f9f05f960c49b0aca5f0"
weixin.RefreshAccessToken(appId, appSecret)

time.Sleep(3 * time.Second)
openIds, total, count, nextOpenId, err := weixin.GetUserList()

if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%#v %#v %#v %#v", openIds, total, count, nextOpenId)
}

// msg := `{"touser":"oFIl_0QmonGnhRTP_sOwDF1jm4xM","template_id":"7ofyI1mXCvBdzgIy91H1uFbK31MNA6dAAvTs62dzBT4","url":"","topcolor":"#FF0000","data":{"User": {"value":"黄先生","color":"#173177"}}}`

msg := `
{
"touser": "oFIl_0QmonGnhRTP_sOwDF1jm4xM",
"template_id": "7ofyI1mXCvBdzgIy91H1uFbK31MNA6dAAvTs62dzBT4",
"url": "",
"topcolor": "#FF0000",
"data": {
"User": {
"value": "阿广",
"color": "#173177"
},
"Time": {
"value": "20180419 11:47:30",
"color": "#173177"
},
"Direct": {
"value": "买入",
"color": "#173177"
},
"Stock": {
"value": "sh600016",
"color": "#173177"
},
"Count": {
"value": "1000",
"color": "#173177"
},
"Price": {
"value": "7.85",
"color": "#173177"
}
}
}
`
err = weixin.SendTempleMsg(nextOpenId, msg)
if err != nil {
fmt.Println(err)
}

ct := &weixin.CustText{
Content: "我们都是好孩子",
}

err = weixin.SendCustomMsg(nextOpenId, ct)
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 416410a

Please sign in to comment.