Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
dbbb7201
W
wechaty
项目概览
.www.
/
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,发现更多精彩内容 >>
提交
dbbb7201
编写于
5月 19, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
local ci
上级
b9ba006e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
57 addition
and
42 deletion
+57
-42
README.md
README.md
+5
-5
example/tuling123-bot.js
example/tuling123-bot.js
+12
-10
src/message.js
src/message.js
+26
-20
src/puppet-web.js
src/puppet-web.js
+14
-7
未找到文件。
README.md
浏览文件 @
dbbb7201
...
...
@@ -24,14 +24,14 @@ So a tireless bot working for me 24x7 on wechat, moniting/filtering the most imp
Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot.
## 1. Basic: 10 lines
The following 10 lines of code
will implement a bot that will reply a message automatically to you
:
The following 10 lines of code
implement a bot who reply "roger" for every message received
:
```
javascript
const
Wechaty
=
require
(
'
wechaty
'
)
const
bot
=
new
Wechaty
()
bot
.
init
()
.
then
(
bot
.
getLoginQrImgUrl
.
bind
(
bot
.
puppet
))
.
then
(
bot
.
getLoginQrImgUrl
.
bind
(
bot
))
.
then
(
url
=>
console
.
log
(
`Scan qrcode in url to login: \n
${
url
}
`
))
bot
.
on
(
'
message
'
,
m
=>
{
...
...
@@ -58,7 +58,7 @@ To Be Written.
Plan to glue with Machine Learning/Deep Learning/Neural Network/Natural Language Processing.
# Installation
& Usage
# Installation
Use NPM is recommended to install Wechaty for you:
```
bash
$
npm
install
--save
wechaty
...
...
@@ -77,7 +77,7 @@ NodeJS Version 6.0 & above is required.
Use
`git`
to checkout Wechaty source code from
[
Github.com
](
https://github.com
)
```
shell
git clone git@github.com:zixia/wechaty.git
# git clone https://github.com/zixia/wechaty.git
#
or use https:
git clone https://github.com/zixia/wechaty.git
```
## 3. Install Dependents
...
...
@@ -89,7 +89,7 @@ npm install
## 4. Run Demo Bot
```
shell
npm start
# node example/ding-dong-bot.js
#
this will run:
node example/ding-dong-bot.js
```
# Trouble Shooting
...
...
example/tuling123-bot.js
浏览文件 @
dbbb7201
/**
*
*
* Wechaty bot use a Tuling123.com brain
*
* Apply your own tuling123.com API_KEY
...
...
@@ -43,17 +43,19 @@ bot.on('message', m => {
const
msg
=
yield
m
.
ready
()
log
.
info
(
'
Bot
'
,
'
recv: %s
'
,
msg
)
if
(
!
m
.
inGroup
())
{
const
r
=
new
Wechaty
.
Message
()
.
set
(
'
to
'
,
m
.
get
(
'
from
'
))
if
(
m
.
inGroup
())
{
return
}
const
r
=
new
Wechaty
.
Message
()
.
set
(
'
to
'
,
m
.
get
(
'
from
'
))
const
content
=
m
.
get
(
'
content
'
).
toString
(
)
const
answer
=
brain
.
ask
(
content
,
{
userid
:
msg
.
get
(
'
from
'
)})
r
.
set
(
'
content
'
,
answer
)
const
content
=
m
.
get
(
'
content
'
)
const
{
code
,
text
}
=
yield
brain
.
ask
(
content
,
{
userid
:
msg
.
get
(
'
from
'
)})
r
.
set
(
'
content
'
,
text
)
yield
bot
.
send
(
r
)
log
.
info
(
'
Bot
'
,
`REPLY:
${
answer
}
`
)
}
yield
bot
.
send
(
r
)
log
.
info
(
'
Bot
'
,
`REPLY: {code:
${
code
}
, text:
${
text
}
}`
)
})
.
catch
(
e
=>
log
.
error
(
'
Bot
'
,
'
on message rejected: %s
'
,
e
))
})
...
...
src/message.js
浏览文件 @
dbbb7201
...
...
@@ -22,22 +22,25 @@ class Message {
// Transform rawObj to local m
parse
(
rawObj
)
{
return
{
id
:
rawObj
.
MsgId
,
type
:
rawObj
.
MsgType
,
from
:
Contact
.
load
(
rawObj
.
MMActualSender
)
,
to
:
Contact
.
load
(
rawObj
.
ToUserName
)
,
group
:
rawObj
.
MMIsChatRoom
?
new
Group
(
rawObj
.
FromUserName
)
:
null
// MMPeerUserName always eq FromUserName ?
,
content
:
rawObj
.
MMActualContent
// Content has @id prefix added by wx
,
status
:
rawObj
.
Status
,
digest
:
rawObj
.
MMDigest
,
date
:
new
Date
(
rawObj
.
MMDisplayTime
*
1000
)
id
:
rawObj
.
MsgId
,
type
:
rawObj
.
MsgType
,
from
:
rawObj
.
MMActualSender
,
to
:
rawObj
.
ToUserName
,
group
:
!!
(
rawObj
.
MMIsChatRoom
)
// MMPeerUserName always eq FromUserName ?
,
content
:
rawObj
.
MMActualContent
// Content has @id prefix added by wx
,
status
:
rawObj
.
Status
,
digest
:
rawObj
.
MMDigest
,
date
:
new
Date
(
rawObj
.
MMDisplayTime
*
1000
)
,
fromContact
:
Contact
.
load
(
rawObj
.
MMActualSender
)
,
toContact
:
Contact
.
load
(
rawObj
.
ToUserName
)
,
inGroup
:
rawObj
.
MMIsChatRoom
?
Group
.
load
(
rawObj
.
FromUserName
)
:
null
}
}
toString
()
{
const
name
=
html2str
(
this
.
obj
.
from
.
get
(
'
name
'
))
const
group
=
this
.
obj
.
g
roup
const
group
=
this
.
obj
.
inG
roup
let
content
=
html2str
(
this
.
obj
.
content
)
if
(
content
.
length
>
20
)
content
=
content
.
substring
(
0
,
17
)
+
'
...
'
;
let
groupStr
=
group
?
html2str
(
group
)
:
''
...
...
@@ -57,11 +60,14 @@ class Message {
ready
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
this
.
obj
.
from
.
ready
()
// Contact from
.
then
(
r
=>
this
.
obj
.
to
.
ready
())
// Contact to
.
then
(
r
=>
this
.
obj
.
group
&&
this
.
obj
.
g
roup
.
ready
())
// Group member list
this
.
obj
.
from
Contact
.
ready
()
// Contact from
.
then
(
r
=>
this
.
obj
.
to
Contact
.
ready
())
// Contact to
.
then
(
r
=>
this
.
obj
.
inGroup
&&
this
.
obj
.
inG
roup
.
ready
())
// Group member list
.
then
(
r
=>
resolve
(
this
))
// RESOLVE
.
catch
(
e
=>
reject
(
e
))
// REJECT
.
catch
(
e
=>
{
// REJECT
log
.
error
(
'
Message
'
,
'
ready() rejected:
'
+
e
)
reject
(
e
)
})
})
}
...
...
@@ -80,13 +86,13 @@ class Message {
return
this
}
dump
()
{
console
.
error
(
'
======= dump message =======
'
)
Object
.
keys
(
this
.
obj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
[
k
]}
`
))
dump
()
{
console
.
error
(
'
======= dump message =======
'
)
Object
.
keys
(
this
.
obj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
[
k
]}
`
))
}
dumpRaw
()
{
dumpRaw
()
{
console
.
error
(
'
======= dump raw message =======
'
)
Object
.
keys
(
this
.
rawObj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
rawObj
[
k
]}
`
))
Object
.
keys
(
this
.
rawObj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
rawObj
[
k
]}
`
))
}
getCount
()
{
return
Message
.
counter
}
...
...
src/puppet-web.js
浏览文件 @
dbbb7201
...
...
@@ -38,6 +38,8 @@ class PuppetWeb extends Puppet {
this
.
on
(
'
login
'
,
()
=>
this
.
logined
=
true
)
this
.
on
(
'
logout
'
,
()
=>
this
.
logined
=
false
)
this
.
userId
=
null
return
Promise
.
all
([
this
.
initServer
()
,
this
.
initBrowser
()
...
...
@@ -53,10 +55,7 @@ class PuppetWeb extends Puppet {
].
map
(
event
=>
this
.
server
.
on
(
event
,
data
=>
this
.
emit
(
event
,
data
))
)
this
.
server
.
on
(
'
message
'
,
data
=>
{
const
m
=
new
Message
(
data
)
this
.
emit
(
'
message
'
,
m
)
})
this
.
server
.
on
(
'
message
'
,
data
=>
this
.
forwardMessage
(
data
))
/**
* `unload` event is sent from js@browser to webserver via socketio
* after received `unload`, we re-inject the Wechaty js code into browser.
...
...
@@ -82,11 +81,19 @@ class PuppetWeb extends Puppet {
return
this
.
browser
.
init
()
}
forwardMessage
(
data
)
{
const
m
=
new
Message
(
data
)
const
fromId
=
m
.
get
(
'
from
'
)
if
(
this
.
userId
)
this
.
emit
(
'
message
'
,
m
)
}
send
(
message
)
{
const
toContact
=
message
.
get
(
'
to
'
)
const
content
=
message
.
get
(
'
content
'
)
const
toContact
=
message
.
get
(
'
to
'
)
const
toContactId
=
toContact
.
getId
()
const
content
=
message
.
get
(
'
content
'
)
return
this
.
proxyWechaty
(
'
send
'
,
toContact
.
getId
(),
content
)
log
.
silly
(
'
PuppetWeb
'
,
`send(
${
toContactId
}
,
${
content
}
)`
)
return
this
.
proxyWechaty
(
'
send
'
,
toContactId
,
content
)
}
logout
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录