From f2c9e3c9953f9cd02e0399e15b6fe6d59484a78c Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Fri, 12 May 2017 22:46:56 +1000 Subject: [PATCH] sending music and news worked. --- kfsend.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ kfsend_test.go | 22 ++++++++++++++-- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/kfsend.go b/kfsend.go index 7d230b7..7baf783 100644 --- a/kfsend.go +++ b/kfsend.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "log" ) func getKfSendURL() (url string) { @@ -109,3 +110,73 @@ func kfSendVideoByMediaID(user, mediaID, title, description, tID string) { j, _ := json.Marshal(s) postJSON(j, u) } + +type sendMusicMsg struct { + ToUser string `json:"touser"` + MsgType string `json:"msgtype"` + Music struct { + MediaID string `json:"media_id"` + ThumbMediaID string `json:"thumb_media_id"` + Title string `json:"title"` + Description string `json:"description"` + URL string `json:"musicurl"` + HqURL string `json:"hqmusicurl"` + } `json:"music"` +} + +func kfSendMusic(user, title, description, thumbID, url, hqurl string) { + u := getKfSendURL() + s := sendMusicMsg{} + s.ToUser = user + s.MsgType = "music" + s.Music.Title = title + s.Music.Description = description + s.Music.ThumbMediaID = thumbID + s.Music.URL = url + s.Music.HqURL = hqurl + j, _ := json.Marshal(s) + log.Println(string(j)) + postJSON(j, u) + +} + +type sendNewsArticle struct { + Title string `json:"title"` + Description string `json:"description"` + URL string `json:"url"` + PicURL string `json:"picurl"` +} + +type sendNewsMsg struct { + ToUser string `json:"touser"` + MsgType string `json:"msgtype"` + News struct { + Articles []sendNewsArticle `json:"articles"` + } `json:"news"` +} + +func kfSendNews(toUser, title, description string, articles []sendNewsArticle) { + count := 0 + s := sendNewsMsg{} + s.ToUser = toUser + s.MsgType = "news" + + //build article + a := []sendNewsArticle{} + for _, v := range articles { + a = append(a, v) + count++ + if count >= 10 { + log.Printf("too many article, only take 10") + break + } + } + s.News.Articles = a + + //marshal + j, _ := json.Marshal(s) + log.Println(string(j)) + //send + u := getKfSendURL() + postJSON(j, u) +} diff --git a/kfsend_test.go b/kfsend_test.go index 3116fa7..fa521b2 100644 --- a/kfsend_test.go +++ b/kfsend_test.go @@ -49,15 +49,33 @@ func TestSendVideo(t *testing.T) { //kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg") kfSendVideoByMediaID(toUser, "xwcgPCY8TRHP_PIy_4qunL8ad9mq7vD3hc9-OpNVRKG1qTwjKkQHN4GKb9mAcJ3J", - "测试标题", - "测试描述信息", + "视频测试标题", + "视频测试描述信息", "6QKTfDxkQS2ACDzVhY0ddKjlIsBTyB6cf9fFWG88uwbJ0Mlh_gSIMxnaGvdqU4y0") } func TestSendMusic(t *testing.T) { SetupConfig() + tID := uploadThumb("media_for_test/music-thumb.jpg") + description := fmt.Sprintf("音乐描述 %s", time.Now().String()) + kfSendMusic(toUser, + "音乐标题", + description, + tID, + "https://biukop.com.au/music.mp3", + "https://biukop.com.au/music.mp3") } func TestSendArticle(t *testing.T) { SetupConfig() + articles := []sendNewsArticle{} + for i := 1; i <= 8; i++ { + a := sendNewsArticle{} + a.Description = fmt.Sprintf("描述 %d ", i) + a.PicURL = "https://placehold.it/360x200.jpg" + a.Title = fmt.Sprintf("标题 %d", i) + a.URL = fmt.Sprintf("https://placehold.it/%dx%d", i*100, i*100) + articles = append(articles, a) + } + kfSendNews(toUser, "总标题", "总描述", articles) }