Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
e6b9cc70
W
wechaty
项目概览
.Veneno.
/
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,发现更多精彩内容 >>
提交
e6b9cc70
编写于
2月 06, 2017
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make tuling123-bot more easy & straight forward #236
上级
1af8b9b2
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
15 addition
and
99 deletion
+15
-99
example/tuling123-bot.ts
example/tuling123-bot.ts
+15
-99
未找到文件。
example/tuling123-bot.ts
浏览文件 @
e6b9cc70
...
...
@@ -15,12 +15,10 @@
const
QrcodeTerminal
=
require
(
'
qrcode-terminal
'
)
const
Tuling123
=
require
(
'
tuling123-client
'
)
import
{
EventEmitter
}
from
'
events
'
import
{
Config
,
Wechaty
,
log
Config
,
Wechaty
,
log
,
}
from
'
../
'
// log.level = 'verbose'
...
...
@@ -33,7 +31,7 @@ import {
*
*/
const
TULING123_API_KEY
=
'
18f25157e0446df58ade098479f74b21
'
const
brain
=
new
Tuling123
(
TULING123_API_KEY
)
const
tuling
=
new
Tuling123
(
TULING123_API_KEY
)
const
bot
=
Wechaty
.
instance
({
profile
:
Config
.
DEFAULT_PROFILE
})
...
...
@@ -58,21 +56,19 @@ bot
console
.
log
(
`
${
url
}
\n[
${
code
}
] Scan QR Code in above url to login: `
)
})
.
on
(
'
message
'
,
async
msg
=>
{
if
(
msg
.
self
())
return
try
{
// const msg = await m.load()
const
room
=
msg
.
room
()
// Skip message from self, or inside a room
if
(
msg
.
self
()
||
msg
.
room
())
return
if
(
room
&&
/Wechaty/i
.
test
(
room
.
topic
()))
{
log
.
info
(
'
Bot
'
,
'
talk: %s
'
,
msg
)
talk
(
msg
)
}
else
{
log
.
info
(
'
Bot
'
,
'
recv: %s
'
,
msg
)
}
}
catch
(
e
)
{
log
.
error
(
'
Bot
'
,
'
on message rejected: %s
'
,
e
)
}
tuling
.
ask
(
msg
.
content
(),
{
userid
:
msg
.
from
()})
.
then
(({
text
})
=>
{
log
.
info
(
'
Tuling123
'
,
'
Talker reply:"%s" for "%s"
'
,
text
,
msg
.
content
())
msg
.
say
(
text
)
})
.
catch
(
err
=>
{
log
.
error
(
'
Bot
'
,
'
on message rejected: %s
'
,
err
)
})
})
bot
.
init
()
...
...
@@ -81,83 +77,3 @@ bot.init()
bot
.
quit
()
process
.
exit
(
-
1
)
})
class
Talker
extends
EventEmitter
{
private
obj
:
{
text
:
any
time
:
any
}
private
timer
:
NodeJS
.
Timer
|
null
constructor
(
private
thinker
)
{
super
()
log
.
verbose
(
'
Talker()
'
)
this
.
obj
=
{
text
:
[]
,
time
:
[]
}
}
public
save
(
text
)
{
log
.
verbose
(
'
Talker
'
,
'
save(%s)
'
,
text
)
this
.
obj
.
text
.
push
(
text
)
this
.
obj
.
time
.
push
(
Date
.
now
())
}
public
load
()
{
const
text
=
this
.
obj
.
text
.
join
(
'
,
'
)
log
.
verbose
(
'
Talker
'
,
'
load(%s)
'
,
text
)
this
.
obj
.
text
=
[]
this
.
obj
.
time
=
[]
return
text
}
public
updateTimer
(
delayTime
?:
number
)
{
delayTime
=
delayTime
||
this
.
delayTime
()
log
.
verbose
(
'
Talker
'
,
'
updateTimer(%s)
'
,
delayTime
)
if
(
this
.
timer
)
{
clearTimeout
(
this
.
timer
)
}
this
.
timer
=
setTimeout
(
_
=>
this
.
say
(),
delayTime
)
}
public
hear
(
text
)
{
log
.
verbose
(
'
Talker
'
,
`hear(
${
text
}
)`
)
this
.
save
(
text
)
this
.
updateTimer
()
}
public
say
()
{
log
.
verbose
(
'
Talker
'
,
'
say()
'
)
const
text
=
this
.
load
()
this
.
thinker
(
text
)
.
then
(
reply
=>
this
.
emit
(
'
say
'
,
reply
))
this
.
timer
=
null
}
public
delayTime
()
{
const
minDelayTime
=
5000
const
maxDelayTime
=
15000
const
delayTime
=
Math
.
floor
(
Math
.
random
()
*
(
maxDelayTime
-
minDelayTime
))
+
minDelayTime
return
delayTime
}
}
/* tslint:disable:variable-name */
let
talkerList
:
Talker
[]
=
[]
function
talk
(
m
)
{
const
fromId
=
m
.
from
().
id
const
roomId
=
m
.
room
().
id
const
content
=
m
.
content
()
const
talkerName
=
fromId
+
roomId
if
(
!
talkerList
[
talkerName
])
{
talkerList
[
talkerName
]
=
new
Talker
(
function
(
text
)
{
return
brain
.
ask
(
text
,
{
userid
:
talkerName
})
.
then
(
r
=>
{
log
.
info
(
'
Tuling123
'
,
'
Talker reply:"%s" for "%s"
'
,
r
.
text
,
text
)
return
r
.
text
})
})
talkerList
[
talkerName
].
on
(
'
say
'
,
reply
=>
m
.
say
(
reply
))
}
talkerList
[
talkerName
].
hear
(
content
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录