| import ( | import ( | ||||
| "encoding/json" | "encoding/json" | ||||
| "fmt" | "fmt" | ||||
| "log" | |||||
| ) | ) | ||||
| func getKfSendURL() (url string) { | func getKfSendURL() (url string) { | ||||
| j, _ := json.Marshal(s) | j, _ := json.Marshal(s) | ||||
| postJSON(j, u) | 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) | |||||
| } |
| //kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg") | //kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg") | ||||
| kfSendVideoByMediaID(toUser, | kfSendVideoByMediaID(toUser, | ||||
| "xwcgPCY8TRHP_PIy_4qunL8ad9mq7vD3hc9-OpNVRKG1qTwjKkQHN4GKb9mAcJ3J", | "xwcgPCY8TRHP_PIy_4qunL8ad9mq7vD3hc9-OpNVRKG1qTwjKkQHN4GKb9mAcJ3J", | ||||
| "测试标题", | |||||
| "测试描述信息", | |||||
| "视频测试标题", | |||||
| "视频测试描述信息", | |||||
| "6QKTfDxkQS2ACDzVhY0ddKjlIsBTyB6cf9fFWG88uwbJ0Mlh_gSIMxnaGvdqU4y0") | "6QKTfDxkQS2ACDzVhY0ddKjlIsBTyB6cf9fFWG88uwbJ0Mlh_gSIMxnaGvdqU4y0") | ||||
| } | } | ||||
| func TestSendMusic(t *testing.T) { | func TestSendMusic(t *testing.T) { | ||||
| SetupConfig() | 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) { | func TestSendArticle(t *testing.T) { | ||||
| SetupConfig() | 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) | |||||
| } | } |