diff --git a/mediaId.go b/mediaID.go similarity index 90% rename from mediaId.go rename to mediaID.go index 28717cf..088e815 100644 --- a/mediaId.go +++ b/mediaID.go @@ -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") diff --git a/outMsg.go b/outMsg.go index d71075a..4296f2a 100644 --- a/outMsg.go +++ b/outMsg.go @@ -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 { ` } +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 diff --git a/server.go b/server.go index fb9f673..bdbcbc9 100644 --- a/server.go +++ b/server.go @@ -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) diff --git a/upload_test.go b/upload_test.go index 2a33b61..a04de6f 100644 --- a/upload_test.go +++ b/upload_test.go @@ -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) +} diff --git a/wechat_hitxy_access_token b/wechat_hitxy_access_token deleted file mode 100644 index 59b6f9b..0000000 --- a/wechat_hitxy_access_token +++ /dev/null @@ -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"} \ No newline at end of file