Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
4dd64295
W
wechaty
项目概览
weixin_46011646
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4dd64295
编写于
11月 26, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code clean
上级
f932dea8
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
30 addition
and
83 deletion
+30
-83
example/voice-to-text-bot.ts
example/voice-to-text-bot.ts
+12
-2
package.json
package.json
+1
-1
src/io-client.ts
src/io-client.ts
+17
-44
src/io.ts
src/io.ts
+0
-36
未找到文件。
example/voice-to-text-bot.ts
浏览文件 @
4dd64295
...
...
@@ -100,14 +100,24 @@ function mp3ToWav(mp3Stream: NodeJS.ReadableStream): NodeJS.ReadableStream {
}
/**
* Baidu:
* 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
*/
/**
* OAuth: http://developer.baidu.com/wiki/index.php?title=docs/oauth/overview
* ASR: http://yuyin.baidu.com/docs/asr/57
* YunZhiSheng:
* http://dev.hivoice.cn/download_file/USC_DevelGuide_WebAPI_audioTranscription.pdf
*/
/**
* Google:
* http://blog.csdn.net/dlangu0393/article/details/7214728
* http://elric2011.github.io/a/using_speech_recognize_service.html
*/
async
function
wavToText
(
readableStream
:
NodeJS
.
ReadableStream
):
Promise
<
string
>
{
const
params
=
{
...
...
package.json
浏览文件 @
4dd64295
...
...
@@ -24,7 +24,7 @@
"lint"
:
"npm run eslint && npm run tslint"
,
"nycava"
:
"WECHATY_HEAD=chrome nyc ava --serial --fail-fast --verbose --timeout=3m
\"
dist/{src,test}/**/*.spec.js
\"
"
,
"eslint"
:
"eslint
\"
{bin,example,src,test}/**/*.js
\"
--ignore-pattern=
\"
test/fixture/**
\"
"
,
"tslint"
:
"tslint
\"
{bin,example,src,test}/**/*.ts
\"
--exclude=
\"
test/fixture/**
\"
&& tsc --noEmit"
,
"tslint"
:
"tslint
\"
{bin,example,src,test}/**/*.ts
\"
--exclude=
\"
test/fixture/**
\"
--exclude="
dist/
"
&& tsc --noEmit"
,
"shlint"
:
"bash -n bin/*.sh"
,
"pretest"
:
"npm run clean && npm run lint && npm run dist"
,
"sloc"
:
"sloc bin example src test index.ts --details --format cli-table --keys total,source,comment && sloc bin example src test index.ts"
,
...
...
src/io-client.ts
浏览文件 @
4dd64295
...
...
@@ -30,8 +30,8 @@ export class IoClient {
private
state
=
new
StateMonitor
<
'
online
'
,
'
offline
'
>
(
'
IoClient
'
,
'
offline
'
)
constructor
(
private
token
:
string
=
Config
.
token
||
Config
.
DEFAULT_TOKEN
,
private
log
:
any
=
new
Brolog
()
private
token
:
string
=
Config
.
token
||
Config
.
DEFAULT_TOKEN
,
private
log
:
any
=
new
Brolog
(),
)
{
if
(
!
log
)
{
const
e
=
new
Error
(
'
constructor() log(npmlog/brolog) must be set
'
)
...
...
@@ -59,27 +59,22 @@ export class IoClient {
public
async
init
():
Promise
<
void
>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
init()
'
)
// if (/connecting|disconnecting/.test(this.currentState())) {
if
(
this
.
state
.
inprocess
())
{
const
e
=
new
Error
(
'
state.inprocess(), skip init
'
)
this
.
log
.
warn
(
'
IoClient
'
,
'
init() with %s
'
,
e
.
message
)
throw
e
}
// this.targetState('connected')
// this.currentState('connecting')
this
.
state
.
target
(
'
online
'
)
this
.
state
.
current
(
'
online
'
,
false
)
try
{
await
this
.
initIo
()
await
this
.
initWechaty
()
// this.currentState('connected')
this
.
state
.
current
(
'
online
'
)
return
}
catch
(
e
)
{
this
.
log
.
error
(
'
IoClient
'
,
'
init() exception: %s
'
,
e
.
message
)
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
throw
e
}
...
...
@@ -88,7 +83,6 @@ export class IoClient {
private
async
initWechaty
():
Promise
<
void
>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
initWechaty()
'
)
// if (this.targetState() !== 'connected') {
if
(
this
.
state
.
target
()
!==
'
online
'
)
{
const
e
=
new
Error
(
'
state.target() is not `online`, skipped
'
)
this
.
log
.
warn
(
'
IoClient
'
,
'
initWechaty() %s
'
,
e
.
message
)
...
...
@@ -111,41 +105,34 @@ export class IoClient {
.
catch
(
e
=>
this
.
log
.
error
(
'
IoClient
'
,
'
message.ready() %s
'
,
e
))
})
try
{
await
wechaty
.
init
()
.
then
(
_
=>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
wechaty.init() done
'
)
return
wechaty
})
.
catch
(
e
=>
{
}
catch
(
e
)
{
this
.
log
.
error
(
'
IoClient
'
,
'
init() init fail: %s
'
,
e
)
wechaty
.
quit
()
throw
e
})
}
return
}
private
async
initIo
():
Promise
<
void
>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
initIo() with token %s
'
,
this
.
token
)
// if (this.targetState() !== 'connected') {
if
(
this
.
state
.
target
()
!==
'
online
'
)
{
const
e
=
new
Error
(
'
initIo() targetState is not `connected`, skipped
'
)
this
.
log
.
warn
(
'
IoClient
'
,
e
.
message
)
throw
e
}
// const io = this.io = new Io({
// wechaty: this.wechaty
// , token: this.token
// })
const
io
=
this
.
io
await
io
.
init
()
.
catch
(
e
=>
{
try
{
await
this
.
io
.
init
()
}
catch
(
e
)
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
initIo() init fail: %s
'
,
e
.
message
)
throw
e
})
}
return
}
...
...
@@ -193,27 +180,20 @@ export class IoClient {
return
this
.
init
()
}
// if (/connecting|disconnecting/.test(this.currentState())) {
if
(
this
.
state
.
inprocess
())
{
this
.
log
.
warn
(
'
IoClient
'
,
'
start() with a pending state, not the time
'
)
return
Promise
.
reject
(
'
pending
'
)
}
// this.targetState('connected')
// this.currentState('connecting')
this
.
state
.
target
(
'
online
'
)
this
.
state
.
current
(
'
online
'
,
false
)
try
{
await
this
.
initIo
()
// .then(() => {
// // this.io = io
// // this.currentState('connected')
this
.
state
.
current
(
'
online
'
)
return
}
catch
(
e
)
{
this
.
log
.
error
(
'
IoClient
'
,
'
start() exception: %s
'
,
e
.
message
)
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
throw
e
}
...
...
@@ -221,8 +201,6 @@ export class IoClient {
public
async
stop
():
Promise
<
void
>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
stop()
'
)
// this.targetState('disconnected')
// this.currentState('disconnecting')
this
.
state
.
target
(
'
offline
'
)
this
.
state
.
current
(
'
offline
'
,
false
)
...
...
@@ -260,14 +238,11 @@ export class IoClient {
public
async
quit
():
Promise
<
void
>
{
this
.
log
.
verbose
(
'
IoClient
'
,
'
quit()
'
)
// if (this.currentState() === 'disconnecting') {
if
(
this
.
state
.
current
()
===
'
offline
'
&&
this
.
state
.
inprocess
())
{
this
.
log
.
warn
(
'
IoClient
'
,
'
quit() with currentState() = `disconnecting`, skipped
'
)
return
Promise
.
reject
(
'
quit() with currentState = `disconnecting`
'
)
}
// this.targetState('disconnected')
// this.currentState('disconnecting')
this
.
state
.
target
(
'
offline
'
)
this
.
state
.
current
(
'
offline
'
,
false
)
...
...
@@ -282,7 +257,6 @@ export class IoClient {
// this.io = null
}
else
{
this
.
log
.
warn
(
'
IoClient
'
,
'
quit() no this.io
'
)
}
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
return
...
...
@@ -291,7 +265,6 @@ export class IoClient {
this
.
log
.
error
(
'
IoClient
'
,
'
exception: %s
'
,
e
.
message
)
// XXX fail safe?
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
throw
e
...
...
src/io.ts
浏览文件 @
4dd64295
...
...
@@ -50,8 +50,6 @@ export class Io {
private
eventBuffer
:
IoEvent
[]
=
[]
private
ws
:
WebSocket
// private _currentState: string
// private _targetState: string
private
state
=
new
StateMonitor
<
'
online
'
,
'
offline
'
>
(
'
Io
'
,
'
offline
'
)
private
reconnectTimer
:
NodeJS
.
Timer
|
null
...
...
@@ -76,32 +74,8 @@ export class Io {
,
setting
.
protocol
,
this
.
uuid
)
// this.purpose('offline')
// this.targetState('disconnected')
// this.currentState('disconnected')
// this.state.target('offline')
// this.state.current('offline')
}
// // targetState : 'connected' | 'disconnected'
// private targetState(newState?) {
// if (newState) {
// log.verbose('Io', 'targetState(%s)', newState)
// this._targetState = newState
// }
// return this._targetState
// }
// // currentState : 'connecting' | 'connected' | 'disconnecting' | 'disconnected'
// private currentState(newState?) {
// if (newState) {
// log.verbose('Io', 'currentState(%s)', newState)
// this._currentState = newState
// }
// return this._currentState
// }
public
toString
()
{
return
'
Class Io(
'
+
this
.
setting
.
token
+
'
)
'
}
private
connected
()
{
return
this
.
ws
&&
this
.
ws
.
readyState
===
WebSocket
.
OPEN
}
...
...
@@ -109,8 +83,6 @@ export class Io {
public
async
init
():
Promise
<
void
>
{
log
.
verbose
(
'
Io
'
,
'
init()
'
)
// this.targetState('connected')
// this.currentState('connecting')
this
.
state
.
target
(
'
online
'
)
this
.
state
.
current
(
'
online
'
,
false
)
...
...
@@ -118,13 +90,11 @@ export class Io {
await
this
.
initEventHook
()
await
this
.
initWebSocket
()
// this.currentState('connected')
this
.
state
.
current
(
'
online
'
)
return
}
catch
(
e
)
{
log
.
warn
(
'
Io
'
,
'
init() exception: %s
'
,
e
.
message
)
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
throw
e
}
...
...
@@ -132,7 +102,6 @@ export class Io {
private
initWebSocket
()
{
log
.
verbose
(
'
Io
'
,
'
initWebSocket()
'
)
// this.currentState('connecting')
this
.
state
.
current
(
'
online
'
,
false
)
// const auth = 'Basic ' + new Buffer(this.setting.token + ':X').toString('base64')
...
...
@@ -404,13 +373,10 @@ export class Io {
private
close
()
{
log
.
verbose
(
'
Io
'
,
'
close()
'
)
// this.targetState('disconnected')
// this.currentState('disconnecting')
this
.
state
.
target
(
'
offline
'
)
this
.
state
.
current
(
'
offline
'
,
false
)
this
.
ws
.
close
()
// this.currentState('disconnected')
this
.
state
.
current
(
'
offline
'
)
// TODO: remove listener for this.setting.wechaty.on(message )
...
...
@@ -418,8 +384,6 @@ export class Io {
}
public
quit
()
{
// this.targetState('disconnected')
// this.currentState('disconnecting')
this
.
state
.
target
(
'
offline
'
)
this
.
state
.
current
(
'
offline
'
,
false
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录