提交 9a52e5ed 编写于 作者: J Jas 提交者: GitHub

Merge pull request #4 from wechaty/master

trying to fix room.owner()
......@@ -17,6 +17,7 @@ RUN apk update && apk upgrade \
chromium-chromedriver \
chromium \
coreutils \
ffmpeg \
figlet \
udev \
vim \
......
......@@ -180,6 +180,26 @@ Know more about Projects Use Wechaty at [Wiki:PoweredByWechaty](https://github.c
Howto [contribute](https://github.com/wechaty/wechaty/blob/master/CONTRIBUTING.md)
Contributions in any form are highly encouraged and welcome! Be it new or improved presets, optimized streaming code or just some cleanup. So start forking!
## Code Contributions
If you want to add new features or change the API, please submit an issue first to make sure no one else is already working on the same thing and discuss the implementation and API details with maintainers and users by creating an issue. When everything is settled down, you can submit a pull request.
When fixing bugs, you can directly submit a pull request.
Make sure to add tests for your features and bugfixes and update the documentation (see below) before submitting your code!
## Documentation Contributions
You can directly submit pull requests for documentation changes.
## Main Contributors
* [JasLin](https://github.com/JasLin)
* [cherry-geqi](https://github.com/cherry-geqi)
* [lijiarui](https://github.com/lijiarui)
# See Also
* [RelatedProject](https://github.com/wechaty/wechaty/wiki/RelatedProject)
......
/**
*
* Wechaty - Wechat for Bot
*
* Connecting ChatBots
* https://github.com/wechaty/wechaty
*
*/
import { PassThrough } from 'stream'
// import { createWriteStream } from 'fs'
import request = require('request')
import Ffmpeg = require('fluent-ffmpeg')
import querystring = require('querystring')
/* tslint:disable:variable-name */
const QrcodeTerminal = require('qrcode-terminal')
import {
Config,
MediaMessage,
MsgType,
Wechaty,
} from '../'
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
bot
.on('scan', (url, code) => {
if (!/201|200/.test(String(code))) {
let loginUrl = url.replace(/\/qrcode\//, '/l/')
QrcodeTerminal.generate(loginUrl)
}
console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
.on('login' , user => console.log(`${user} logined`))
.on('message', async function(this, m) {
console.log(`RECV: ${m}`)
if (m.type() !== MsgType.VOICE) {
return // skip no-VOICE message
}
const mp3Stream = await (m as MediaMessage).readyStream()
const text = await voiceToText(mp3Stream)
console.log('VOICE TO TEXT: ' + text)
this.say(text) // send text to 'filehelper'
})
.init()
.catch(e => console.error('bot.init() error: ' + e))
async function voiceToText(mp3Stream: NodeJS.ReadableStream): Promise<string> {
const wavStream = mp3ToWav(mp3Stream)
// const textStream = wavToText(wavStream)
// textStream.on('data', text => {
// console.log(text)
// })
try {
const text = await wavToText(wavStream)
return text
} catch (e) {
console.log(e)
return ''
}
}
function mp3ToWav(mp3Stream: NodeJS.ReadableStream): NodeJS.ReadableStream {
const wavStream = new PassThrough()
Ffmpeg(mp3Stream)
.fromFormat('mp3')
.toFormat('wav')
.pipe(wavStream as any)
// .on('start', function(commandLine) {
// console.log('Spawned Ffmpeg with command: ' + commandLine);
// })
// .on('codecData', function(data) {
// console.log('Input is ' + data.audio + ' audio ' +
// 'with ' + data.video + ' video');
// })
// .on('progress', progress => {
// console.log('Processing: ' + progress.percent + '% done');
// })
// .on('end', function() {
// console.log('Finished processing');
// })
.on('error', function(err, stdout, stderr) {
console.log('Cannot process video: ' + err.message);
})
return wavStream
}
/**
* export BAIDU_SPEECH_API_KEY=FK58sUlteAuAIXZl5dWzAHCT
* export BAIDU_SPEECH_SECRET_KEY=feaf24adcc5b8f02b147e7f7b1953030
* curl "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=${BAIDU_SPEECH_API_KEY}&client_secret=${BAIDU_SPEECH_SECRET_KEY}"
*/
/**
* OAuth: http://developer.baidu.com/wiki/index.php?title=docs/oauth/overview
* ASR: http://yuyin.baidu.com/docs/asr/57
*/
async function wavToText(readableStream: NodeJS.ReadableStream): Promise<string> {
const params = {
'cuid': 'wechaty',
'lan': 'zh',
'token': '24.8c6a25b5dcfb41af189a97d9e0b7c076.2592000.1482571685.282335-8943256'
}
const apiUrl = 'http://vop.baidu.com/server_api?'
+ querystring.stringify(params)
const options = {
headers: {
'Content-Type': 'audio/wav; rate=8000',
},
}
return new Promise<string>((resolve, reject) => {
readableStream.pipe(request.post(apiUrl, options, (err, httpResponse, body) => {
// "err_msg":"success.","err_no":0,"result":["这是一个测试测试语音转文字,"]
if (err) {
return reject(err)
}
try {
const obj = JSON.parse(body)
if (obj.err_no !== 0) {
return reject(new Error(obj.err_msg))
}
return resolve(obj.result[0])
} catch (err) {
return reject(err)
}
}))
})
}
import {
Config
, Sayable
, log
Config,
Sayable,
log,
} from './src/config'
import { Contact } from './src/contact'
......@@ -10,8 +10,9 @@ import { FriendRequest } from './src/puppet-web/friend-request'
import { IoClient } from './src/io-client'
import {
Message
, MsgType
Message,
MediaMessage,
MsgType,
} from './src/message'
import { Puppet } from './src/puppet'
import { PuppetWeb } from './src/puppet-web/'
......@@ -22,20 +23,21 @@ import { Wechaty } from './src/wechaty'
const VERSION = require('./package.json').version
export {
Config
, Contact
, FriendRequest
, IoClient
, Message
, MsgType
, Puppet
, PuppetWeb
, Room
, Sayable
, UtilLib
, VERSION
, Wechaty
, log // for convenionce use npmlog with environment variable LEVEL
Config,
Contact,
FriendRequest,
IoClient,
Message,
MediaMessage,
MsgType,
Puppet,
PuppetWeb,
Room,
Sayable,
UtilLib,
VERSION,
Wechaty,
log, // for convenionce use npmlog with environment variable LEVEL
}
export default Wechaty
......
{
"name": "wechaty",
"version": "0.6.25",
"version": "0.6.27",
"description": "Wechat for Bot(Personal Account)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
......@@ -122,8 +122,10 @@
"cross-env": "^2.0.0",
"eslint": "^3.4.0",
"eslint-plugin-ava": "^3.1.1",
"fluent-ffmpeg": "^2.1.0",
"nyc": "^8.3.2",
"qrcode-terminal": "^0.10.0",
"request": "^2.79.0",
"shx": "^0.1.4",
"sinon": "^1.17.5",
"sloc": "^0.1.11",
......
......@@ -115,7 +115,7 @@ export class Wechaty extends EventEmitter implements Sayable {
* 1. .git not exist
* 2. git log fail
*/
log.silly('Wechaty', 'version() test %s', e.message)
log.silly('Wechaty', 'version() form development environment is not availble: %s', e.message)
}
}
......@@ -312,10 +312,12 @@ export class Wechaty extends EventEmitter implements Sayable {
}
public async say(content: string): Promise<void> {
log.verbose('Wechaty', 'say(%s)', content)
if (!this.puppet) {
throw new Error('no puppet')
}
await this.puppet.say(content)
this.puppet.say(content)
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册