Using nodejs for WeChat development
Uploaded temporary materials
The reply mechanism to the video is as follows:
if(content === '视频'){
var data = yield wechatApi.uploadMaterial('video',__dirname+'\public\video\3.mp4')
console.log('data')
console.log(data)
reply = {
type:'video',
title:'回复的视频',
description:'视频描述',
mediaId:data.media_id
}
}
After entering "video" in the mobile WeChat client,
the background display is as follows:
If such a code appears, it should mean that the upload was successful, right? However, the message displayed on the mobile phone is that the official account is temporarily unavailable. Please try again later? why is that?
ps: I also found that when uploading, it was uploaded three times? I only posted one "video", how come it was uploaded three times?
The uploaded code is as follows:
Wechat.prototype.uploadMaterial = function(type,filepath){
var that = this;
var form = {
media: fs.createReadStream(filepath)
}
return new Promise(function(resolve,reject){
that
.fetchAccessToken()
.then(function(data){
console.log(data);
var url = api.upload +'access_token='+data.access_token + '&type='+type;
request({method:'POST',url:url,formData:form,json:true}).then(function(response){
var _data = response.body;
//console.log(data);
if(_data){
resolve(_data)
}
else{
throw new Error('upload Material fail')
}
})
.catch(function(err){
reject(err)
})
})
})
}
As for the upload interface, it is like this:
var wechatApi = new Wechat(config.wechat)
This is the instantiation of the above, and the data required for the specific instantiation is obtained from the config.
When uploading, use the wechatApi.uploadMaterial() method to achieve this.
typecho2017-06-30 09:59:20
1. Why does WeChat return that the official account is temporarily unable to provide services
The mechanism of WeChat is that after the user sends information to the official account, if the server does not return within 5 seconds, it will prompt that the official account is temporarily unable to provide services
The 5-second mechanism can be solved in other ways. First, empty content is returned to WeChat in order not to prompt, and then a correct
response content is returned to WeChat after the program is processed.
2. The problem of uploading three times requires you to post the code of the upload interface and the code of upload processing, and then we will talk about how to solve it
滿天的星座2017-06-30 09:59:20
I also encountered this problem when I was developing. When the user sends the word "video", the background is indeed uploading the video material (the upload code is written without problems), but the time of uploading the video is due to the network or WeChat. The background itself sometimes exceeds 5 seconds (larger videos exceed 5 seconds in the test account). At this time, the server directly returns a prompt message: This official account is temporarily unable to provide services. It will take a while for the program to print out the data and xml information of your screenshot.
So uploading video material when automatically replying
This approach is inappropriate. The video should be uploaded in advance. The user sends "video" and directly obtains the pre-uploaded video information and replies to the user