Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
8066f272
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,发现更多精彩内容 >>
提交
8066f272
编写于
5月 29, 2018
作者:
ruiruibupt
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code clean
上级
d05b1f0a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
129 addition
and
90 deletion
+129
-90
src/puppet-padchat/bridge.ts
src/puppet-padchat/bridge.ts
+12
-2
src/puppet-padchat/event.ts
src/puppet-padchat/event.ts
+3
-3
src/puppet-padchat/padchat-schemas.ts
src/puppet-padchat/padchat-schemas.ts
+1
-1
src/puppet-padchat/puppet-padchat.ts
src/puppet-padchat/puppet-padchat.ts
+113
-84
未找到文件。
src/puppet-padchat/bridge.ts
浏览文件 @
8066f272
...
...
@@ -4,6 +4,9 @@ import { EventEmitter } from 'events'
import
*
as
cuid
from
'
cuid
'
import
*
as
WebSocket
from
'
ws
'
import
*
as
fs
from
'
fs
'
import
{
PadchatContactRawPayload
,
}
from
'
./padchat-schemas
'
export
const
resolverDict
:
{
[
idx
:
string
]:
Function
,
...
...
@@ -338,8 +341,15 @@ export class Bridge extends EventEmitter {
* Get contact by contact id
* @param {any} id user_name
*/
public
WXGetContact
(
id
:
string
):
void
{
this
.
sendToWebSocket
(
'
WXGetContact
'
,
[
id
])
public
async
WXGetContact
(
id
:
string
):
Promise
<
PadchatContactRawPayload
>
{
const
result
=
await
this
.
sendToWebSocket
(
'
WXGetContact
'
,
[
id
])
if
(
!
result
)
{
throw
Error
(
'
PuppetPadchatBridge, WXGetContact, cannot get result from websocket server!
'
)
}
if
(
result
.
user_name
)
{
log
.
warn
(
'
PuppetPadchatBridge
'
,
'
WXGetContact cannot get user_name
'
)
}
return
result
}
/**
...
...
src/puppet-padchat/event.ts
浏览文件 @
8066f272
...
...
@@ -17,7 +17,7 @@ async function onMessage(
this
:
PuppetPadchat
,
rawPayload
:
PadchatMessageRawPayload
,
):
Promise
<
void
>
{
let
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
)
let
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
,
await
this
.
messageRawPayloadParser
(
rawPayload
)
)
try
{
await
msg
.
ready
()
...
...
@@ -60,12 +60,12 @@ async function onMessage(
case
PadchatMessageType
.
MICROVIDEO
:
case
PadchatMessageType
.
APP
:
log
.
verbose
(
'
PuppetPadchatEvent
'
,
'
onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message
'
)
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
)
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
,
await
this
.
messageRawPayloadParser
(
rawPayload
)
)
break
case
PadchatMessageType
.
TEXT
:
log
.
verbose
(
'
PuppetPadchatEvent
'
,
'
onMessage() (TEXT&LOCATION) message
'
)
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
)
msg
=
this
.
Message
.
create
(
rawPayload
.
msg_id
,
await
this
.
messageRawPayloadParser
(
rawPayload
)
)
}
await
msg
.
ready
()
...
...
src/puppet-padchat/padchat-schemas.ts
浏览文件 @
8066f272
...
...
@@ -31,7 +31,7 @@ export interface PadchatContactRawPayload {
nick_name
:
string
,
// "梦君君", Contact:用户昵称, Room: 群昵称
provincia
:
string
,
// "Beijing",
remark
:
string
,
// "女儿",
sex
:
number
,
// 2 Female, 1 Male, 0 Not Known
sex
:
0
|
1
|
2
,
// 2 Female, 1 Male, 0 Not Known
signature
:
string
,
// "且行且珍惜",
small_head
:
string
,
// "http://wx.qlogo.cn/mmhead/ver_1/xfCMmibHH74xGLoyeDFJadrZXX3eOEznPefiaCa3iczxZGMwPtDuSbRQKx3Xdm18un303mf0NFia3USY2nO2VEYILw/132",
// source: number, // 14, // 0, 14, don't know why
...
...
src/puppet-padchat/puppet-padchat.ts
浏览文件 @
8066f272
...
...
@@ -26,7 +26,7 @@ import {
import
{
// Message,
MessagePayload
,
MessagePayload
,
MessageType
,
}
from
'
../message
'
import
{
...
...
@@ -94,7 +94,7 @@ import * as WebSocket from 'ws'
const
TOKEN
=
'
padchattest
'
export
class
PuppetPadchat
extends
Puppet
{
public
bridge
:
Bridge
|
undefined
public
bridge
:
Bridge
public
botWs
:
WebSocket
constructor
(
...
...
@@ -104,6 +104,13 @@ export class PuppetPadchat extends Puppet {
this
.
botWs
=
new
WebSocket
(
ADDRESS
,
{
perMessageDeflate
:
true
})
this
.
bridge
=
new
Bridge
({
userId
:
TOKEN
,
botWs
:
this
.
botWs
,
autoData
:
{},
// profile: profile, // should be profile in the future
})
this
.
botWs
.
on
(
'
message
'
,
data
=>
this
.
wsOnMessage
(
data
))
}
...
...
@@ -226,13 +233,7 @@ export class PuppetPadchat extends Puppet {
const
autoData
:
AutoDataType
=
await
this
.
options
.
profile
.
get
(
'
autoData
'
)
log
.
silly
(
'
PuppetPadchat
'
,
'
initBridge, get autoData: %s
'
,
JSON
.
stringify
(
autoData
))
this
.
bridge
=
new
Bridge
({
userId
:
TOKEN
,
botWs
:
this
.
botWs
,
autoData
:
autoData
,
// profile: profile, // should be profile in the future
})
this
.
bridge
.
autoData
=
autoData
// this.bridge.on('ding' , Event.onDing.bind(this))
// this.bridge.on('error' , e => this.emit('error', e))
...
...
@@ -266,44 +267,24 @@ export class PuppetPadchat extends Puppet {
// "userId": "test"
// }
if
(
!
rawWebSocketData
.
data
)
{
log
.
warn
(
'
PuppetPadchat
'
,
'
WebSocket Server Error:
get empty message data form Tencent server
'
)
log
.
silly
(
'
PuppetPadchat
'
,
'
WebSocket Server
get empty message data form Tencent server
'
)
return
}
// JSON.parse(decodeURIComponent(rawWebSocketData.data):
// [
// {
// "content": "XXXX",
// "continue": 1,
// "data": "XXX",
// "description": "李佳芮+:+[语音]",
// "from_user": "qq512436430",
// "msg_id": "8502371723610127059",
// "msg_source": "",
// "msg_type": 5,
// "status": 1,
// "sub_type": 34,
// "timestamp": 1526984922,
// "to_user": "wxid_zj2cahpwzgie12",
// "uin": 324216852
// }
// ]
const
rawData
=
JSON
.
parse
(
decodeURIComponent
(
rawWebSocketData
.
data
))[
0
]
log
.
silly
(
'
PuppetPadchat
'
,
'
WebSocket Server rawData result: %s
'
,
JSON
.
stringify
(
rawData
))
let
msg
if
(
!
rawData
[
'
msg_id
'
])
{
msg
=
this
.
Message
.
create
(
'
emptyId
'
)
log
.
warn
(
'
PuppetPadchat
'
,
'
WebSocket Server Error: get empty message msg_id form Tencent server
'
)
}
const
msgRawPayloadList
:
PadchatMessageRawPayload
[]
=
JSON
.
parse
(
decodeURIComponent
(
rawWebSocketData
.
data
))
msgRawPayloadList
.
forEach
(
async
(
msgRawPayload
)
=>
{
log
.
silly
(
'
PuppetPadchat
'
,
'
WebSocket Server rawData result: %s
'
,
JSON
.
stringify
(
msgRawPayload
))
if
(
!
msgRawPayload
[
'
msg_id
'
])
{
log
.
warn
(
'
PuppetPadchat
'
,
'
WebSocket Server Error: get empty message msg_id form Tencent server
'
)
return
}
msg
=
this
.
Message
.
create
(
rawData
[
'
msg_id
'
])
;
(
msg
as
any
).
payload
=
this
.
messageRawPayloadParser
(
rawData
as
PadchatMessageRawPayload
)
await
msg
.
ready
()
const
msg
=
this
.
Message
.
create
(
msgRawPayload
[
'
msg_id
'
],
await
this
.
messageRawPayloadParser
(
msgRawPayload
))
await
msg
.
ready
()
this
.
emit
(
'
message
'
,
msg
)
this
.
emit
(
'
message
'
,
msg
)
})
// Data Return From WebSocket Client
}
else
{
...
...
@@ -449,29 +430,39 @@ export class PuppetPadchat extends Puppet {
public
async
contactRawPayload
(
id
:
string
):
Promise
<
PadchatContactRawPayload
>
{
log
.
verbose
(
'
PuppetPadchat
'
,
'
contactRawPayload(%s)
'
,
id
)
const
rawPayload
:
PadchatContactRawPayload
=
{
big_head
:
'
padchat name
'
,
city
:
''
,
country
:
''
,
nick_name
:
''
,
provincia
:
''
,
remark
:
''
,
sex
:
1
,
signature
:
''
,
small_head
:
''
,
stranger
:
''
,
user_name
:
''
,
}
const
rawPayload
=
await
this
.
bridge
.
WXGetContact
(
id
)
return
rawPayload
}
public
async
contactRawPayloadParser
(
rawPayload
:
PadchatContactRawPayload
):
Promise
<
ContactPayload
>
{
log
.
verbose
(
'
PuppetPadchat
'
,
'
contactRawPayloadParser(%s)
'
,
rawPayload
)
const
gender
=
{
0
:
Gender
.
Unknown
,
1
:
Gender
.
Male
,
2
:
Gender
.
Female
,
}
if
(
/@chatroom$/
.
test
(
rawPayload
.
user_name
))
{
throw
Error
(
'
Room Object instead of Contact!
'
)
}
let
contactType
=
this
.
Contact
.
Type
.
Unknown
if
(
/^gh_/
.
test
(
rawPayload
.
user_name
))
{
contactType
=
this
.
Contact
.
Type
.
Official
}
else
{
contactType
=
this
.
Contact
.
Type
.
Personal
}
const
payload
:
ContactPayload
=
{
gender
:
Gender
.
Unknown
,
type
:
ContactType
.
Unknown
,
gender
:
gender
[
rawPayload
.
sex
],
type
:
contactType
,
alias
:
rawPayload
.
remark
,
avatar
:
rawPayload
.
big_head
,
city
:
rawPayload
.
city
,
name
:
rawPayload
.
nick_name
,
province
:
rawPayload
.
provincia
,
signature
:
(
rawPayload
.
signature
).
replace
(
'
+
'
,
''
),
// Stay+Foolish
}
return
payload
}
...
...
@@ -482,38 +473,71 @@ export class PuppetPadchat extends Puppet {
*
*/
public
async
messageRawPayload
(
id
:
string
):
Promise
<
PadchatMessageRawPayload
>
{
log
.
verbose
(
'
PuppetPadchat
'
,
'
messageRawPayload(%s)
'
,
id
)
const
rawPayload
:
PadchatMessageRawPayload
=
{
content
:
''
,
data
:
''
,
continue
:
1
,
description
:
''
,
from_user
:
''
,
msg_id
:
''
,
msg_source
:
''
,
msg_type
:
5
,
status
:
1
,
sub_type
:
PadchatMessageType
.
TEXT
,
timestamp
:
11111111
,
to_user
:
''
,
uin
:
111111
,
// from : 'from_id',
// text : 'padchat message text',
// to : 'to_id',
}
return
rawPayload
throw
Error
(
'
should not call messageRawPayload
'
)
// log.verbose('PuppetPadchat', 'messageRawPayload(%s)', id)
// const rawPayload: PadchatMessageRawPayload = {
// content: '',
// data: '',
// continue: 1,
// description: '',
// from_user: '',
// msg_id: '',
// msg_source: '',
// msg_type: 5,
// status: 1,
// sub_type: PadchatMessageType.TEXT,
// timestamp: 11111111,
// to_user: '',
// uin: 111111,
// // from : 'from_id',
// // text : 'padchat message text',
// // to : 'to_id',
// }
// return rawPayload
}
public
async
messageRawPayloadParser
(
rawPayload
:
PadchatMessageRawPayload
):
Promise
<
MessagePayload
>
{
log
.
verbose
(
'
PuppetPadchat
'
,
'
messagePayload(%s)
'
,
rawPayload
)
let
type
:
MessageType
=
this
.
Message
.
Type
.
Unknown
switch
(
rawPayload
.
sub_type
)
{
case
PadchatMessageType
.
TEXT
:
type
=
this
.
Message
.
Type
.
Text
break
case
PadchatMessageType
.
IMAGE
:
type
=
this
.
Message
.
Type
.
Image
break
case
PadchatMessageType
.
VOICE
:
type
=
this
.
Message
.
Type
.
Audio
break
case
PadchatMessageType
.
EMOTICON
:
type
=
this
.
Message
.
Type
.
Emoticon
break
case
PadchatMessageType
.
APP
:
type
=
this
.
Message
.
Type
.
Attachment
break
case
PadchatMessageType
.
VIDEO
:
type
=
this
.
Message
.
Type
.
Video
break
default
:
type
=
this
.
Message
.
Type
.
Text
}
const
payload
:
MessagePayload
=
{
date
:
new
Date
(),
fromId
:
'
xxx
'
,
text
:
'
padchat message text
'
,
toId
:
this
.
userSelf
().
id
,
type
:
t
his
.
Message
.
Type
.
Text
,
fromId
:
rawPayload
.
from_user
,
text
:
rawPayload
.
content
,
toId
:
rawPayload
.
to_user
,
type
:
t
ype
,
}
if
(
/@chatroom$/
.
test
(
rawPayload
.
from_user
))
{
payload
.
roomId
=
rawPayload
.
from_user
payload
.
fromId
=
rawPayload
.
content
.
split
(
'
:
\n
'
)[
0
]
payload
.
text
=
rawPayload
.
content
.
split
(
'
:
\n
'
)[
1
]
}
log
.
verbose
(
'
PuppetPadchat
'
,
'
messagePayload(%s)
'
,
JSON
.
stringify
(
payload
))
return
payload
}
...
...
@@ -522,6 +546,11 @@ export class PuppetPadchat extends Puppet {
text
:
string
,
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchat
'
,
'
messageSend(%s, %s)
'
,
receiver
,
text
)
const
id
=
receiver
.
contactId
||
receiver
.
roomId
if
(
!
id
)
{
throw
Error
(
'
No id
'
)
}
await
this
.
bridge
.
WXSendMsg
(
id
,
text
)
}
public
async
messageSendFile
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录