| @@ -35,6 +35,15 @@ func uploadVideo(filename string) (mediaid string) { | |||
| } | |||
| func uploadVoice(path string) (mediaID string) { | |||
| url, _ := getPostVoiceURL() | |||
| jstr, _ := postFileForm(path, url, "media") | |||
| var m = MediaID{} | |||
| json.Unmarshal([]byte(jstr), &m) | |||
| mediaID = m.MediaID | |||
| return | |||
| } | |||
| func checkImageSanity() bool { | |||
| //check file size should < 2M | |||
| fmt.Println(" should check image file size") | |||
| @@ -86,6 +86,7 @@ func buildUploadPicMsg(ToUserName, path string) (msg string) { | |||
| msg = buildPicMsg(ToUserName, mediaID) | |||
| return | |||
| } | |||
| func buildPicMsg(ToUserName, mediaID string) (msg string) { | |||
| msg = fmt.Sprintf(picMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID) | |||
| return | |||
| @@ -102,6 +103,13 @@ func picMsgTemplate() string { | |||
| </xml>` | |||
| } | |||
| func buildUploadVoiceMsg(ToUserName, path string) (msg string) { | |||
| mediaID := uploadVoice(path) | |||
| log.Println("get media id " + mediaID) | |||
| msg = buildVoiceMsg(ToUserName, mediaID) | |||
| return | |||
| } | |||
| func buildVoiceMsg(ToUserName, mediaID string) (msg string) { | |||
| msg = fmt.Sprintf(voiceMsgTemplate(), ToUserName, APIConfig.PublicAccountID, int32(time.Now().Unix()), mediaID) | |||
| return | |||
| @@ -76,6 +76,7 @@ func answerWechatPost(w http.ResponseWriter, r *http.Request) { | |||
| reply = buildVideoMsg(h.FromUserName, mediaID, "标题123", a.Event+"/"+a.EventKey) | |||
| reply = buildUploadPicMsg(h.FromUserName, "media_for_test/640x480.jpg") | |||
| reply = buildUploadVoiceMsg(h.FromUserName, "media_for_test/example.amr") | |||
| } | |||
| w.Header().Set("Content-Type", "text/xml; charset=utf-8") | |||
| fmt.Fprint(w, reply) | |||
| @@ -53,3 +53,10 @@ func TestUploadVideo(t *testing.T) { | |||
| AssertEqual(t, err, nil, "error ro compare file should be nil ") | |||
| AssertEqual(t, equal, true, "video file should be equal ") | |||
| } | |||
| func TestUploadVoice(t *testing.T) { | |||
| SetupConfig() | |||
| src := "media_for_test/example.amr" | |||
| mediaID := uploadVoice(src) | |||
| log.Println(mediaID) | |||
| } | |||
| @@ -1 +0,0 @@ | |||
| {"access_token":"GAMFvLGWol69qo3-zGVswpXzlWsmt_4xUGUJf4Gsesk2-gJuiIxUlOdBaVJU-xVCoAz7OheyR2nJ36_RP0lJK9ARl8yJeJD62pE5XoLuHkdCXrEeTRip-MFEvC6SkYuNSWZiAGALPS","expires_in":7200,"created_at":"2017-05-10T02:52:24.014027459+10:00"} | |||