Pārlūkot izejas kodu

sending music and news worked.

master
Patrick Peng Sun pirms 8 gadiem
vecāks
revīzija
f2c9e3c995
2 mainītis faili ar 91 papildinājumiem un 2 dzēšanām
  1. +71
    -0
      kfsend.go
  2. +20
    -2
      kfsend_test.go

+ 71
- 0
kfsend.go Parādīt failu

@@ -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)
}

+ 20
- 2
kfsend_test.go Parādīt failu

@@ -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)
}

Notiek ielāde…
Atcelt
Saglabāt