Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
2cd28d87
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,发现更多精彩内容 >>
提交
2cd28d87
编写于
4月 30, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
message with attachment file caused by Message.ext() BREAKING CHANGE (fix #1175)
上级
2250ce12
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
190 addition
and
98 deletion
+190
-98
examples/ding-dong-bot.ts
examples/ding-dong-bot.ts
+16
-3
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+17
-16
src/puppet-puppeteer/puppeteer-message.ts
src/puppet-puppeteer/puppeteer-message.ts
+157
-79
未找到文件。
examples/ding-dong-bot.ts
浏览文件 @
2cd28d87
...
...
@@ -85,17 +85,30 @@ bot
)
if
(
/^
(
ding|ping|bing|code
)
$/i
.
test
(
m
.
text
())
&&
!
m
.
self
())
{
m
.
say
(
'
dong
'
)
/**
* 1. reply 'dong'
*/
log
.
info
(
'
Bot
'
,
'
REPLY: dong
'
)
m
.
say
(
'
dong
'
)
const
joinWechaty
=
`Join Wechaty Developers' Community\n\n`
+
`Wechaty is used in many ChatBot projects by hundreds of developers.\n\n`
+
`If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n`
+
`you can join our Wechaty Developers' Home at once`
await
m
.
say
(
joinWechaty
)
await
m
.
say
(
new
bot
.
Message
(
BOT_QR_CODE_IMAGE_FILE
))
/**
* 2. reply qrcode image
*/
const
imageMessage
=
new
bot
.
Message
(
BOT_QR_CODE_IMAGE_FILE
)
log
.
info
(
'
Bot
'
,
'
REPLY: %s
'
,
imageMessage
)
await
m
.
say
(
imageMessage
)
/**
* 3. reply 'scan now!'
*/
await
m
.
say
(
'
Scan now, because other Wechaty developers want to talk with you too!
\n\n
(secret code: wechaty)
'
)
log
.
info
(
'
Bot
'
,
'
REPLY: Image
'
)
}
}
catch
(
e
)
{
log
.
error
(
'
Bot
'
,
'
on(message) exception: %s
'
,
e
)
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
2cd28d87
...
...
@@ -336,14 +336,14 @@ export class PuppetPuppeteer extends Puppet {
let
mediatype
:
MediaType
switch
(
ext
)
{
case
'
bmp
'
:
case
'
jpeg
'
:
case
'
jpg
'
:
case
'
png
'
:
case
'
gif
'
:
case
'
.
bmp
'
:
case
'
.
jpeg
'
:
case
'
.
jpg
'
:
case
'
.
png
'
:
case
'
.
gif
'
:
mediatype
=
MediaType
.
IMAGE
break
case
'
mp4
'
:
case
'
.
mp4
'
:
mediatype
=
MediaType
.
VIDEO
break
default
:
...
...
@@ -507,7 +507,7 @@ export class PuppetPuppeteer extends Puppet {
},
},
}
let
mediaId
let
mediaId
:
string
try
{
mediaId
=
<
string
>
await
new
Promise
((
resolve
,
reject
)
=>
{
try
{
...
...
@@ -537,7 +537,7 @@ export class PuppetPuppeteer extends Puppet {
log
.
error
(
'
PuppetPuppeteer
'
,
'
uploadMedia(): upload fail
'
)
throw
new
Error
(
'
PuppetPuppeteer.uploadMedia(): upload fail
'
)
}
return
Object
.
assign
(
mediaData
,
{
MediaId
:
mediaId
as
string
})
return
Object
.
assign
(
mediaData
,
{
MediaId
:
mediaId
})
}
public
async
sendMedia
(
message
:
PuppeteerMessage
):
Promise
<
boolean
>
{
...
...
@@ -556,8 +556,9 @@ export class PuppetPuppeteer extends Puppet {
}
let
mediaData
:
MediaData
const
rawObj
=
message
.
rawObj
as
MsgRawObj
if
(
!
rawObj
.
MediaId
)
{
const
rawObj
=
message
.
rawObj
||
{}
as
MsgRawObj
if
(
!
rawObj
||
!
rawObj
.
MediaId
)
{
try
{
mediaData
=
await
this
.
uploadMedia
(
message
,
destinationId
)
message
.
rawObj
=
Object
.
assign
(
rawObj
,
mediaData
)
...
...
@@ -1090,14 +1091,14 @@ export class PuppetPuppeteer extends Puppet {
public
extToType
(
ext
:
string
):
MsgType
{
switch
(
ext
)
{
case
'
bmp
'
:
case
'
jpeg
'
:
case
'
jpg
'
:
case
'
png
'
:
case
'
.
bmp
'
:
case
'
.
jpeg
'
:
case
'
.
jpg
'
:
case
'
.
png
'
:
return
MsgType
.
IMAGE
case
'
gif
'
:
case
'
.
gif
'
:
return
MsgType
.
EMOTICON
case
'
mp4
'
:
case
'
.
mp4
'
:
return
MsgType
.
VIDEO
default
:
return
MsgType
.
APP
...
...
src/puppet-puppeteer/puppeteer-message.ts
浏览文件 @
2cd28d87
...
...
@@ -80,10 +80,10 @@ export class PuppeteerMessage extends Message {
super
()
log
.
silly
(
'
PuppeteerMessage
'
,
'
constructor()
'
)
this
.
obj
=
{}
as
MsgObj
this
.
obj
=
{}
as
MsgObj
// this.rawObj = {} as MsgRawObj
if
(
!
fileOrObj
)
{
this
.
rawObj
=
<
MsgRawObj
>
{}
return
}
...
...
@@ -137,39 +137,39 @@ export class PuppeteerMessage extends Message {
* @private
*/
public
toString
()
{
return
`PuppeteerMessage<
${
Misc
.
plainText
(
this
.
obj
.
content
)}
>`
return
`PuppeteerMessage<
${
Misc
.
plainText
(
this
.
obj
&&
this
.
obj
.
content
)}
>`
}
/**
* @private
*/
public
toStringDigest
()
{
const
text
=
Misc
.
digestEmoji
(
this
.
obj
.
digest
)
return
'
{
'
+
this
.
typeEx
()
+
'
}
'
+
text
}
/**
* @private
*/
public
getSenderString
()
{
const
from
=
PuppeteerContact
.
load
(
this
.
obj
.
from
)
from
.
puppet
=
this
.
puppet
const
fromName
=
from
.
name
()
const
roomTopic
=
this
.
obj
.
room
?
(
'
:
'
+
PuppeteerRoom
.
load
(
this
.
obj
.
room
).
topic
())
:
''
return
`<
${
fromName
}${
roomTopic
}
>`
}
/**
* @private
*/
public
getContentString
()
{
let
content
=
Misc
.
plainText
(
this
.
obj
.
content
)
if
(
content
.
length
>
20
)
{
content
=
content
.
substring
(
0
,
17
)
+
'
...
'
}
return
'
{
'
+
this
.
type
()
+
'
}
'
+
content
}
/
/ /
**
//
* @private
//
*/
//
public toStringDigest() {
//
const text = Misc.digestEmoji(this.obj.digest)
//
return '{' + this.typeEx() + '}' + text
//
}
/
/ /
**
//
* @private
//
*/
//
public getSenderString() {
//
const from = PuppeteerContact.load(this.obj.from)
//
from.puppet = this.puppet
//
const fromName = from.name()
//
const roomTopic = this.obj.room
//
? (':' + PuppeteerRoom.load(this.obj.room).topic())
//
: ''
//
return `<${fromName}${roomTopic}>`
//
}
/
/ /
**
//
* @private
//
*/
//
public getContentString() {
//
let content = Misc.plainText(this.obj.content)
//
if (content.length > 20) { content = content.substring(0, 17) + '...' }
//
return '{' + this.type() + '}' + content
//
}
/**
* @private
...
...
@@ -347,7 +347,43 @@ export class PuppeteerMessage extends Message {
*/
public
type
():
MsgType
{
log
.
silly
(
'
PuppeteerMessage
'
,
'
type() = %s
'
,
MsgType
[
this
.
obj
.
type
])
return
this
.
obj
.
type
||
MsgType
.
TEXT
/**
* 1. A message created with rawObj
*/
if
(
this
.
obj
.
type
)
{
return
this
.
obj
.
type
}
/**
* 2. A message created with TEXT
*/
const
ext
=
this
.
extFromFile
()
if
(
!
ext
)
{
return
MsgType
.
TEXT
}
/**
* 3. A message created with local file
*/
switch
(
ext
.
toLowerCase
())
{
case
'
.bmp
'
:
case
'
.jpg
'
:
case
'
.jpeg
'
:
case
'
.png
'
:
return
MsgType
.
IMAGE
case
'
.gif
'
:
return
MsgType
.
EMOTICON
case
'
.mp4
'
:
return
MsgType
.
VIDEO
case
'
.mp3
'
:
return
MsgType
.
VOICE
}
throw
new
Error
(
'
unknown type:
'
+
ext
)
}
/**
...
...
@@ -378,12 +414,12 @@ export class PuppeteerMessage extends Message {
return
this
.
rawObj
.
AppMsgType
}
/**
* Get the typeEx from the message.
*
* @returns {MsgType}
*/
public
typeEx
()
{
return
MsgType
[
this
.
obj
.
type
]
}
/
/ /
**
//
* Get the typeEx from the message.
//
*
//
* @returns {MsgType}
//
*/
//
public typeEx() { return MsgType[this.obj.type] }
/**
* Check if a message is sent by self.
...
...
@@ -608,38 +644,38 @@ export class PuppeteerMessage extends Message {
return
this
}
/**
* @private
*/
public
get
(
prop
:
string
):
string
{
log
.
warn
(
'
PuppeteerMessage
'
,
'
DEPRECATED get() at %s
'
,
new
Error
(
'
stack
'
).
stack
)
if
(
!
prop
||
!
(
prop
in
this
.
obj
))
{
const
s
=
'
[
'
+
Object
.
keys
(
this
.
obj
).
join
(
'
,
'
)
+
'
]
'
throw
new
Error
(
`Message.get(
${
prop
}
) must be in:
${
s
}
`
)
}
return
this
.
obj
[
prop
]
}
/**
* @private
*/
public
set
(
prop
:
string
,
value
:
string
):
this
{
log
.
warn
(
'
PuppeteerMessage
'
,
'
DEPRECATED set() at %s
'
,
new
Error
(
'
stack
'
).
stack
)
if
(
typeof
value
!==
'
string
'
)
{
throw
new
Error
(
'
value must be string, we got:
'
+
typeof
value
)
}
this
.
obj
[
prop
]
=
value
return
this
}
/
/ /
**
//
* @private
//
*/
//
public get(prop: string): string {
//
log.warn('PuppeteerMessage', 'DEPRECATED get() at %s', new Error('stack').stack)
//
if (!prop || !(prop in this.obj)) {
//
const s = '[' + Object.keys(this.obj).join(',') + ']'
//
throw new Error(`Message.get(${prop}) must be in: ${s}`)
//
}
//
return this.obj[prop]
//
}
/
/ /
**
//
* @private
//
*/
//
public set(prop: string, value: string): this {
//
log.warn('PuppeteerMessage', 'DEPRECATED set() at %s', new Error('stack').stack)
//
if (typeof value !== 'string') {
//
throw new Error('value must be string, we got: ' + typeof value)
//
}
//
this.obj[prop] = value
//
return this
//
}
/**
* @private
*/
public
dump
()
{
console
.
error
(
'
======= dump message =======
'
)
Object
.
keys
(
this
.
obj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
[
k
]}
`
))
Object
.
keys
(
this
.
obj
!
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
!
[
k
]}
`
))
}
/**
...
...
@@ -648,7 +684,7 @@ export class PuppeteerMessage extends Message {
public
dumpRaw
()
{
console
.
error
(
'
======= dump raw message =======
'
)
if
(
!
this
.
rawObj
)
{
throw
new
Error
(
'
no this.
o
bj
'
)
throw
new
Error
(
'
no this.
rawO
bj
'
)
}
Object
.
keys
(
this
.
rawObj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
rawObj
&&
this
.
rawObj
[
k
]}
`
))
}
...
...
@@ -729,13 +765,16 @@ export class PuppeteerMessage extends Message {
* }
* })
*/
public
filename
():
string
{
public
filename
():
string
|
null
{
log
.
verbose
(
'
PuppeteerMessage
'
,
'
filename()
'
)
if
(
this
.
parsedPath
)
{
// https://nodejs.org/api/path.html#path_path_parse_path
const
filename
=
path
.
join
(
this
.
parsedPath
!
.
dir
||
''
,
this
.
parsedPath
!
.
base
||
''
,
)
log
.
silly
(
'
PuppeteerMessage
'
,
'
filename()=%s, build from parsedPath
'
,
filename
)
return
filename
}
...
...
@@ -747,10 +786,12 @@ export class PuppeteerMessage extends Message {
const
ext
=
this
.
rawObj
.
MMAppMsgFileExt
||
this
.
ext
()
filename
+=
'
.
'
+
ext
}
log
.
silly
(
'
PuppeteerMessage
'
,
'
filename()=%s, build from rawObj
'
,
filename
)
return
filename
}
throw
new
Error
(
'
no rawObj
'
)
return
null
}
...
...
@@ -766,38 +807,75 @@ export class PuppeteerMessage extends Message {
* })
*/
public
ext
():
string
{
if
(
this
.
parsedPath
&&
this
.
parsedPath
.
ext
)
const
fileExt
=
this
.
extFromFile
()
if
(
fileExt
)
{
return
fileExt
}
const
typeExt
=
this
.
extFromType
()
if
(
typeExt
)
{
return
typeExt
}
throw
new
Error
(
'
unknown ext()
'
)
}
private
extFromFile
():
string
|
null
{
if
(
this
.
parsedPath
&&
this
.
parsedPath
.
ext
)
{
return
this
.
parsedPath
.
ext
}
return
null
}
private
extFromType
():
string
{
let
ext
:
string
const
type
=
this
.
type
()
switch
(
t
his
.
type
()
)
{
switch
(
t
ype
)
{
case
MsgType
.
EMOTICON
:
return
'
.gif
'
ext
=
'
.gif
'
break
case
MsgType
.
IMAGE
:
return
'
.jpg
'
ext
=
'
.jpg
'
break
case
MsgType
.
VIDEO
:
case
MsgType
.
MICROVIDEO
:
return
'
.mp4
'
ext
=
'
.mp4
'
break
case
MsgType
.
VOICE
:
return
'
.mp3
'
ext
=
'
.mp3
'
break
case
MsgType
.
APP
:
switch
(
this
.
typeApp
())
{
case
AppMsgType
.
URL
:
return
'
.url
'
// XXX
ext
=
'
.url
'
// XXX
break
default
:
ext
=
'
.
'
+
this
.
type
()
break
}
break
case
MsgType
.
TEXT
:
if
(
this
.
typeSub
()
===
MsgType
.
LOCATION
)
{
return
'
.jpg
'
ext
=
'
.jpg
'
}
ext
=
'
.
'
+
this
.
type
()
break
default
:
log
.
silly
(
'
PuppeteerMessage
'
,
`ext() got unknown type:
${
this
.
type
()}
`
)
ext
=
'
.
'
+
this
.
type
()
}
log
.
silly
(
'
PuppeteerMessage
'
,
`ext() got unknown type:
${
this
.
type
()}
`
)
return
String
(
'
.
'
+
this
.
type
())
return
ext
}
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录