Kaynağa Gözat

upload voice worked and tested with wechat client

master
Patrick Peng Sun 8 yıl önce
ebeveyn
işleme
8ff9e51973
5 değiştirilmiş dosya ile 25 ekleme ve 1 silme
  1. +9
    -0
      mediaID.go
  2. +8
    -0
      outMsg.go
  3. +1
    -0
      server.go
  4. +7
    -0
      upload_test.go
  5. +0
    -1
      wechat_hitxy_access_token

mediaId.go → mediaID.go Dosyayı Görüntüle

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

+ 8
- 0
outMsg.go Dosyayı Görüntüle

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

+ 1
- 0
server.go Dosyayı Görüntüle

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

+ 7
- 0
upload_test.go Dosyayı Görüntüle

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

+ 0
- 1
wechat_hitxy_access_token Dosyayı Görüntüle

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

Yükleniyor…
İptal
Kaydet