Skip to content

Commit

Permalink
#32 语音识别
Browse files Browse the repository at this point in the history
  • Loading branch information
stonexer committed Apr 13, 2016
1 parent 17ad632 commit 0624b22
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
69 changes: 65 additions & 4 deletions example/lib/wxbot.js
Expand Up @@ -2,6 +2,7 @@
const Wechat = require('../../index')
const debug = require('debug')('wxbot')
const fs = require('fs')
const ffmpeg = require('ffmpeg')

class WxBot extends Wechat {

Expand All @@ -12,7 +13,9 @@ class WxBot extends Wechat {

this.replyUsers = new Set()
this.on('text-message', msg => this._botReply(msg))

this.on('voice-message', msg => this._botReply(msg))
this.on('image-message', msg => this._botReply(msg))

this.superviseUsers = new Set()
this.openTimes = 0
this.on('init-message', () => this._botSupervise())
Expand Down Expand Up @@ -84,12 +87,70 @@ class WxBot extends Wechat {
return '现在思路很乱,最好联系下我哥 T_T...'
})
}

_getWavBuf(mp3Buf) {
return new Promise((resolve, reject) => {
fs.writeFile('1.mp3', mp3Buf, (err,file) => {
if(err) {
reject(err)
}
let process = new ffmpeg('1.mp3')
process.then( (video) => {
video.save('1.wav', () => {
fs.readFile('1.wav', (err, buf) => {
if(err) {
reject(err)
}
fs.unlink('1.wav')
fs.unlink('1.mp3')
resolve(buf)
})
})
}).catch(err => {
reject(err)
})
})
})
}

_speechRecognize(buf) {
let params = {
'cuid': 'wechat4u',
'lan': 'zh',
'token': '24.9a233a67c12f73276a026357b6079dc0.2592000.1462584892.282335-6332804'
}
return this.request({
method: 'POST',
url: 'http://vop.baidu.com/server_api',
headers: {
'Content-Type': 'audio/wav; rate=8000',
},
params: params,
data: buf
}).then(res => {
const data = res.data
debug('语音识别结果:', data.result[0])
return data.result[0]
})
}

_botReply(msg) {
if (this.replyUsers.has(msg['FromUserName'])) {
this._tuning(msg['Content']).then((reply) => {
this.sendMsg(reply, msg['FromUserName'])
debug(reply)
new Promise((resolve, reject) => {
if(msg['Content'].type === 'audio/mp3') {
this._getWavBuf(msg['Content'].data)
.then(buf => this._speechRecognize(buf))
.then(content => this._tuning(content))
.then(replyContent => resolve(replyContent))
} else if(msg['Content'].type === 'image/jpeg') {
resolve('好美的照片啊!')
} else {
this._tuning(msg['Content'])
.then(replyContent => resolve(replyContent))
}
}).then(replyContent => {
this.sendMsg(replyContent, msg['FromUserName'])
debug('回复消息:', replyContent)
})
}
}
Expand Down
13 changes: 10 additions & 3 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"vue-strap": "^1.0.7",
"vue-style-loader": "^1.0.0",
"webpack": "^1.12.2",
"ffmpeg": "0.0.4",
"cross-env": "^1.0.7"
},
"repository": {
Expand All @@ -68,13 +69,19 @@
"rules": {
"camelcase": 2,
"curly": 2,
"brace-style": [2, "1tbs"],
"quotes": [2, "single"],
"brace-style": [
2,
"1tbs"
],
"quotes": [
2,
"single"
],
"space-infix-ops": 2
},
"env": {
"browser": true,
"node": true
}
}
}
}

0 comments on commit 0624b22

Please sign in to comment.