Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
06e14fb4
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,发现更多精彩内容 >>
提交
06e14fb4
编写于
5月 26, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix media file api
上级
f6b81c5c
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
660 addition
and
584 deletion
+660
-584
src/message.ts
src/message.ts
+105
-84
src/message.type.ts
src/message.type.ts
+2
-1
src/puppet-puppeteer/event.ts
src/puppet-puppeteer/event.ts
+28
-28
src/puppet-puppeteer/firer.ts
src/puppet-puppeteer/firer.ts
+8
-8
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+517
-463
未找到文件。
src/message.ts
浏览文件 @
06e14fb4
...
...
@@ -16,8 +16,8 @@
* limitations under the License.
* @ignore
*/
import
*
as
path
from
'
path
'
import
*
as
cuid
from
'
cuid
'
//
import * as path from 'path'
//
import * as cuid from 'cuid'
import
{
FileBox
,
...
...
@@ -37,9 +37,9 @@ import Room from './room'
import
{
MessageDirection
,
MessageMOOptions
,
MessageMOOptionsText
,
MessageMOOptionsFile
,
//
MessageMOOptions,
//
MessageMOOptionsText,
//
MessageMOOptionsFile,
MessagePayload
,
MessageType
,
}
from
'
./message.type
'
...
...
@@ -91,89 +91,95 @@ export class Message extends PuppetAccessory implements Sayable {
* "mobile originated" or "mobile terminated"
* https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/
*/
public
static
createMO
(
options
:
MessageMOOptions
,
):
Message
{
log
.
verbose
(
'
Message
'
,
'
static createMobileOriginated()
'
)
/**
* Must NOT use `Message` at here
* MUST use `this` at here
*
* because the class will be `cloneClass`-ed
*/
const
msg
=
new
this
(
cuid
())
const
direction
=
MessageDirection
.
MO
const
to
=
options
.
to
const
room
=
options
.
room
const
text
=
(
options
as
MessageMOOptionsText
).
text
const
date
=
new
Date
()
const
file
=
(
options
as
MessageMOOptionsFile
).
file
const
roomId
=
room
&&
room
.
id
const
toId
=
to
&&
to
.
id
if
(
text
)
{
/**
* 1. Text
*/
msg
.
payload
=
{
type
:
MessageType
.
Text
,
direction
,
toId
,
roomId
,
text
,
date
,
}
}
else
if
(
file
)
{
/**
* 2. File
*/
let
type
:
MessageType
const
ext
=
path
.
extname
(
file
.
name
)
switch
(
ext
.
toLowerCase
())
{
case
'
.bmp
'
:
case
'
.jpg
'
:
case
'
.jpeg
'
:
case
'
.png
'
:
case
'
.gif
'
:
// type = WebMsgType.EMOTICON
type
=
MessageType
.
Image
break
// private
static createMO(
//
options: MessageMOOptions,
//
): Message {
//
log.verbose('Message', 'static createMobileOriginated()')
//
/**
//
* Must NOT use `Message` at here
//
* MUST use `this` at here
//
*
//
* because the class will be `cloneClass`-ed
//
*/
//
const msg = new this(cuid())
//
const direction = MessageDirection.MO
//
const to = options.to
//
const room = options.room
//
const text = (options as MessageMOOptionsText).text
//
const date = new Date()
//
const file = (options as MessageMOOptionsFile).file
//
const roomId = room && room.id
//
const toId = to && to.id
//
if (text) {
//
/**
//
* 1. Text
//
*/
//
msg.payload = {
//
type: MessageType.Text,
//
direction,
//
toId,
//
roomId,
//
text,
//
date,
//
}
//
} else if (file) {
//
/**
//
* 2. File
//
*/
//
let type: MessageType
//
const ext = path.extname(file.name)
//
switch (ext.toLowerCase()) {
//
case '.bmp':
//
case '.jpg':
//
case '.jpeg':
//
case '.png':
//
case '.gif': // type = WebMsgType.EMOTICON
//
type = MessageType.Image
//
break
case
'
.mp4
'
:
type
=
MessageType
.
Video
break
//
case '.mp4':
//
type = MessageType.Video
//
break
case
'
.mp3
'
:
type
=
MessageType
.
Audio
break
//
case '.mp3':
//
type = MessageType.Audio
//
break
default
:
throw
new
Error
(
'
unknown ext:
'
+
ext
)
}
//
default:
//
throw new Error('unknown ext:' + ext)
//
}
msg
.
payload
=
{
type
,
direction
,
toId
,
roomId
,
file
,
date
,
}
}
else
{
throw
new
Error
(
'
neither text nor file!?
'
)
}
//
msg.payload = {
//
type,
//
direction,
//
toId,
//
roomId,
//
file,
//
date,
//
}
//
} else {
//
throw new Error('neither text nor file!?')
//
}
return
msg
}
//
return msg
//
}
/**
* "mobile originated" or "mobile terminated"
* https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/
*/
public
static
createMT
(
id
:
string
,
id
:
string
,
payload
?
:
MessagePayload
,
):
Message
{
log
.
verbose
(
'
Message
'
,
'
static createMobileTerminated(%s)
'
,
log
.
verbose
(
'
Message
'
,
'
static createMobileTerminated(%s
, %s
)
'
,
id
,
payload
,
)
/**
* Must NOT use `Message` at here
...
...
@@ -184,15 +190,25 @@ export class Message extends PuppetAccessory implements Sayable {
const
msg
=
new
this
(
id
)
msg
.
direction
=
MessageDirection
.
MT
if
(
payload
)
{
msg
.
payload
=
payload
}
return
msg
}
/**
*
Create a Mobile Originated Message
*
@param options MO Options
*
@alias createMT
*
Create a Mobile Terminated Message
*/
public
static
create
(
options
:
MessageMOOptions
):
Message
{
return
this
.
createMO
(
options
)
public
static
create
(
id
:
string
,
payload
?
:
MessagePayload
,
):
Message
{
return
this
.
createMT
(
id
,
payload
,
)
}
/**
...
...
@@ -246,6 +262,8 @@ export class Message extends PuppetAccessory implements Sayable {
if
(
this
.
type
()
===
Message
.
Type
.
Text
)
{
msgStrList
.
push
(
`<
${
this
.
text
()}
>`
)
}
else
{
log
.
verbose
(
'
Message
'
,
'
toString() this.type()=%s
'
,
Message
.
Type
[
this
.
type
()])
if
(
!
this
.
payload
)
{
throw
new
Error
(
'
no payload
'
)
}
...
...
@@ -424,7 +442,10 @@ export class Message extends PuppetAccessory implements Sayable {
textOrFile
:
string
|
FileBox
,
mention
?
:
Contact
|
Contact
[],
):
Promise
<
void
>
{
log
.
verbose
(
'
Message
'
,
'
say(%s, %s)
'
,
textOrFile
,
mention
)
log
.
verbose
(
'
Message
'
,
'
say(%s, %s)
'
,
textOrFile
.
toString
(),
mention
,
)
// const user = this.puppet.userSelf()
const
from
=
this
.
from
()
...
...
src/message.type.ts
浏览文件 @
06e14fb4
...
...
@@ -10,7 +10,7 @@ import {
}
from
'
./room
'
export
enum
MessageDirection
{
MO
,
//
MO,
MT
,
}
...
...
@@ -18,6 +18,7 @@ export enum MessageType {
Unknown
=
0
,
Attachment
,
Audio
,
Emoticon
,
Image
,
Text
,
Video
,
...
...
src/puppet-puppeteer/event.ts
浏览文件 @
06e14fb4
...
...
@@ -190,7 +190,7 @@ async function onMessage(
this
:
PuppetPuppeteer
,
rawPayload
:
WebMessageRawPayload
,
):
Promise
<
void
>
{
le
t
msg
=
this
.
Message
.
createMT
(
rawPayload
.
MsgId
)
cons
t
msg
=
this
.
Message
.
createMT
(
rawPayload
.
MsgId
)
try
{
await
msg
.
ready
()
...
...
@@ -206,9 +206,9 @@ async function onMessage(
case
WebMessageType
.
SYS
:
if
(
msg
.
room
())
{
const
joinResult
=
await
Firer
.
checkRoomJoin
.
call
(
this
,
msg
)
const
leaveResult
=
await
Firer
.
checkRoomLeave
.
call
(
this
,
msg
)
const
topicRestul
=
await
Firer
.
checkRoomTopic
.
call
(
this
,
msg
)
const
joinResult
=
await
Firer
.
checkRoomJoin
.
call
(
this
,
msg
)
const
leaveResult
=
await
Firer
.
checkRoomLeave
.
call
(
this
,
msg
)
const
topicRestul
=
await
Firer
.
checkRoomTopic
.
call
(
this
,
msg
)
if
(
!
joinResult
&&
!
leaveResult
&&
!
topicRestul
)
{
log
.
warn
(
'
PuppetPuppeteerEvent
'
,
`checkRoomSystem message: <
${
msg
.
text
()}
> not found`
)
...
...
@@ -219,31 +219,31 @@ async function onMessage(
break
}
/**
* Check Type for special Message
* reload if needed
*/
switch
(
rawPayload
.
MsgType
)
{
case
WebMessageType
.
EMOTICON
:
case
WebMessageType
.
IMAGE
:
case
WebMessageType
.
VIDEO
:
case
WebMessageType
.
VOICE
:
case
WebMessageType
.
MICROVIDEO
:
case
WebMessageType
.
APP
:
log
.
verbose
(
'
PuppetPuppeteerEvent
'
,
'
onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message
'
)
msg
=
this
.
Message
.
createMT
(
rawPayload
.
MsgId
)
break
// /**
// * Check Type for special Message
// * reload if needed
// */
// switch (rawPayload.MsgType) {
// case WebMessageType.EMOTICON:
// case WebMessageType.IMAGE:
// case WebMessageType.VIDEO:
// case WebMessageType.VOICE:
// case WebMessageType.MICROVIDEO:
// case WebMessageType.APP:
// log.verbose('PuppetPuppeteerEvent', 'onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message')
// msg = this.Message.createMT(rawPayload.MsgId)
// break
// case WebMessageType.TEXT:
// if (rawPayload.SubMsgType === WebMessageType.LOCATION) {
// log.verbose('PuppetPuppeteerEvent', 'onMessage() (TEXT&LOCATION) message')
// msg = this.Message.createMT(rawPayload.MsgId)
// }
// break
// }
// await msg.ready()
case
WebMessageType
.
TEXT
:
if
(
rawPayload
.
SubMsgType
===
WebMessageType
.
LOCATION
)
{
log
.
verbose
(
'
PuppetPuppeteerEvent
'
,
'
onMessage() (TEXT&LOCATION) message
'
)
msg
=
this
.
Message
.
createMT
(
rawPayload
.
MsgId
)
}
break
}
await
msg
.
ready
()
this
.
emit
(
'
message
'
,
msg
)
}
catch
(
e
)
{
...
...
src/puppet-puppeteer/firer.ts
浏览文件 @
06e14fb4
...
...
@@ -204,7 +204,7 @@ function parseRoomJoin(
this
:
PuppetPuppeteer
,
content
:
string
,
):
[
string
[],
string
]
{
log
.
verbose
(
'
PuppetPuppeteerFirer
'
,
'
check
RoomJoin(%s)
'
,
content
)
log
.
verbose
(
'
PuppetPuppeteerFirer
'
,
'
parse
RoomJoin(%s)
'
,
content
)
const
reListInvite
=
regexConfig
.
roomJoinInvite
const
reListQrcode
=
regexConfig
.
roomJoinQrcode
...
...
@@ -214,7 +214,7 @@ function parseRoomJoin(
let
foundQrcode
:
string
[]
|
null
=
[]
reListQrcode
.
some
(
re
=>
!!
(
foundQrcode
=
content
.
match
(
re
)))
if
((
!
foundInvite
||
!
foundInvite
.
length
)
&&
(
!
foundQrcode
||
!
foundQrcode
.
length
))
{
throw
new
Error
(
'
check
RoomJoin() not found matched re of
'
+
content
)
throw
new
Error
(
'
parse
RoomJoin() not found matched re of
'
+
content
)
}
/**
* 管理员 invited 庆次、小桔妹 to the group chat
...
...
@@ -233,7 +233,7 @@ async function checkRoomJoin(
const
room
=
msg
.
room
()
if
(
!
room
)
{
log
.
warn
(
'
PuppetPuppeteerFirer
'
,
'
fire
RoomJoin() `room` not found
'
)
log
.
warn
(
'
PuppetPuppeteerFirer
'
,
'
check
RoomJoin() `room` not found
'
)
return
false
}
...
...
@@ -243,23 +243,23 @@ async function checkRoomJoin(
try
{
[
inviteeList
,
inviter
]
=
parseRoomJoin
.
call
(
this
,
text
)
}
catch
(
e
)
{
log
.
silly
(
'
PuppetPuppeteerFirer
'
,
'
fire
RoomJoin() "%s" is not a join message
'
,
text
)
log
.
silly
(
'
PuppetPuppeteerFirer
'
,
'
check
RoomJoin() "%s" is not a join message
'
,
text
)
return
false
// not a room join message
}
log
.
silly
(
'
PuppetPuppeteerFirer
'
,
'
fire
RoomJoin() inviteeList: %s, inviter: %s
'
,
log
.
silly
(
'
PuppetPuppeteerFirer
'
,
'
check
RoomJoin() inviteeList: %s, inviter: %s
'
,
inviteeList
.
join
(
'
,
'
),
inviter
,
)
let
inviterContact
:
Contact
|
null
=
null
let
inviteeContactList
:
Contact
[]
=
[]
let
inviterContact
:
null
|
Contact
=
null
let
inviteeContactList
:
Contact
[]
=
[]
try
{
if
(
inviter
===
'
You
'
||
inviter
===
'
你
'
||
inviter
===
'
you
'
)
{
inviterContact
=
this
.
userSelf
()
}
const
max
=
20
const
max
=
20
const
backoff
=
300
const
timeout
=
max
*
(
backoff
*
max
)
/
2
// 20 / 300 => 63,000
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
06e14fb4
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录