Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
5d360514
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,发现更多精彩内容 >>
提交
5d360514
编写于
5月 26, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix media file bugs & examples
上级
06e14fb4
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
33 addition
and
91 deletion
+33
-91
examples/contact-bot.ts
examples/contact-bot.ts
+16
-33
examples/media-file-bot.ts
examples/media-file-bot.ts
+4
-49
package.json
package.json
+1
-1
src/message.ts
src/message.ts
+1
-1
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+7
-3
src/room.ts
src/room.ts
+4
-4
未找到文件。
examples/contact-bot.ts
浏览文件 @
5d360514
...
...
@@ -30,7 +30,8 @@ const QrcodeTerminal = require('qrcode-terminal')
import
{
Wechaty
,
log
,
}
from
'
../src/
'
Contact
,
}
from
'
../src/
'
const
welcome
=
`
=============== Powered by Wechaty ===============
...
...
@@ -62,7 +63,7 @@ bot
/**
* Main Contact Bot start from here
*/
onLog
in
()
ma
in
()
})
.
on
(
'
logout
'
,
user
=>
log
.
info
(
'
Bot
'
,
`
${
user
.
name
()}
logouted`
))
...
...
@@ -85,7 +86,7 @@ bot.start()
/**
* Main Contact Bot
*/
async
function
onLog
in
()
{
async
function
ma
in
()
{
const
contactList
=
await
bot
.
Contact
.
findAll
()
log
.
info
(
'
Bot
'
,
'
#######################
'
)
...
...
@@ -96,29 +97,18 @@ async function onLogin() {
*/
for
(
let
i
=
0
;
i
<
contactList
.
length
;
i
++
)
{
const
contact
=
contactList
[
i
]
if
(
contact
.
official
()
)
{
if
(
contact
.
type
()
===
Contact
.
Type
.
Official
)
{
log
.
info
(
'
Bot
'
,
`official
${
i
}
:
${
contact
}
`
)
}
}
// /**
// * Special contact list
// */
// for (let i = 0; i < contactList.length; i++) {
// const contact = contactList[i]
// if (contact.special()) {
// log.info('Bot', `special ${i}: ${contact.name()}`)
// }
// }
/**
* personal contact list
*/
for
(
let
i
=
0
;
i
<
contactList
.
length
;
i
++
)
{
const
contact
=
contactList
[
i
]
if
(
contact
.
personal
()
)
{
if
(
contact
.
type
()
===
Contact
.
Type
.
Personal
)
{
log
.
info
(
'
Bot
'
,
`personal
${
i
}
:
${
contact
.
name
()}
:
${
contact
.
id
}
`
)
}
}
...
...
@@ -127,22 +117,17 @@ async function onLogin() {
for
(
let
i
=
0
;
i
<
contactList
.
length
;
i
++
)
{
const
contact
=
contactList
[
i
]
if
(
!
contact
.
weixin
())
{
await
contact
.
refresh
()
}
/**
* Save avatar to file like: "1-name.jpg"
*/
const
avatarFileName
=
`
${
i
}
-
${
contact
.
name
()}
.jpg`
const
avatarReadStream
=
await
contact
.
avatar
()
const
avatarWriteStream
=
createWriteStream
(
avatarFileName
)
const
wait
=
new
Promise
(
r
=>
avatarWriteStream
.
once
(
'
close
'
,
r
))
avatarReadStream
.
pipe
(
avatarWriteStream
)
await
wait
const
file
=
await
contact
.
avatar
()
const
name
=
file
.
name
await
file
.
save
(
name
)
log
.
info
(
'
Bot
'
,
'
Contact: %s: %s with avatar file: %s
'
,
contact
.
weixin
(),
contact
.
name
(),
avatarFileName
)
log
.
info
(
'
Bot
'
,
'
Contact: "%s" with avatar file: "%s"
'
,
contact
.
name
(),
name
,
)
if
(
i
>
MAX
)
{
log
.
info
(
'
Bot
'
,
'
Contacts too many, I only show you the first %d ...
'
,
MAX
)
...
...
@@ -150,10 +135,8 @@ async function onLogin() {
}
}
//
const SLEEP = 7
//
log.info('Bot', 'I will re-dump contact weixin id & names after %d second... ', SLEEP)
//
setTimeout(main, SLEEP * 1000)
const
SLEEP
=
7
log
.
info
(
'
Bot
'
,
'
I will re-dump contact weixin id & names after %d second...
'
,
SLEEP
)
setTimeout
(
main
,
SLEEP
*
1000
)
await
bot
.
logout
()
await
bot
.
stop
()
}
examples/media-file-bot.ts
浏览文件 @
5d360514
...
...
@@ -16,13 +16,7 @@
* limitations under the License.
*
*/
// import { inspect } from 'util'
import
{
createWriteStream
,
statSync
,
// writeFileSync,
}
from
'
fs
'
/* tslint:disable:variable-name */
import
*
as
qrcodeTerminal
from
'
qrcode-terminal
'
...
...
@@ -35,7 +29,6 @@ import * as qrcodeTerminal from 'qrcode-terminal'
import
{
config
,
Message
,
// MsgType,
Wechaty
,
}
from
'
../src/
'
...
...
@@ -53,50 +46,12 @@ bot
.
on
(
'
message
'
,
msg
=>
{
console
.
log
(
`RECV:
${
msg
}
`
)
// console.log(inspect(m))
// saveRawObj(m.rawObj)
// if ( m.type() === MsgType.IMAGE
// || m.type() === MsgType.EMOTICON
// || m.type() === MsgType.VIDEO
// || m.type() === MsgType.VOICE
// || m.type() === MsgType.MICROVIDEO
// || m.type() === MsgType.APP
// || (m.type() === MsgType.TEXT && m.typeSub() === MsgType.LOCATION) // LOCATION
// ) {
if
(
msg
.
type
()
!==
Message
.
Type
.
Text
)
{
saveMediaFile
(
msg
)
const
file
=
msg
.
file
()
const
name
=
msg
.
file
().
name
file
.
save
(
name
)
}
// }
})
.
start
()
.
catch
(
e
=>
console
.
error
(
'
bot.start() error:
'
+
e
))
async
function
saveMediaFile
(
message
:
Message
)
{
const
fileBox
=
message
.
file
()
const
filename
=
fileBox
.
name
console
.
log
(
'
IMAGE local filename:
'
+
filename
)
if
(
!
filename
)
{
throw
new
Error
(
'
no filename found for media file
'
)
}
const
fileStream
=
createWriteStream
(
filename
)
console
.
log
(
'
start to readyStream()
'
)
try
{
fileBox
.
pipe
(
fileStream
)
.
on
(
'
close
'
,
()
=>
{
const
stat
=
statSync
(
filename
)
console
.
log
(
'
finish readyStream() for
'
,
filename
,
'
size:
'
,
stat
.
size
)
})
}
catch
(
e
)
{
console
.
error
(
'
stream error:
'
,
e
)
}
}
// function saveRawObj(o) {
// writeFileSync('rawObj.log', JSON.stringify(o, null, ' ') + '\n\n\n', { flag: 'a' })
// }
package.json
浏览文件 @
5d360514
...
...
@@ -97,7 +97,7 @@
"
clone-class
"
:
"
^0.6.11
"
,
"
cuid
"
:
"
^2.1.1
"
,
"
express
"
:
"
^4.16.3
"
,
"
file-box
"
:
"
^0.
2
.1
"
,
"
file-box
"
:
"
^0.
3
.1
"
,
"
hot-import
"
:
"
^0.2.1
"
,
"
mime
"
:
"
^2.2.0
"
,
"
normalize-package-data
"
:
"
^2.4.0
"
,
...
...
src/message.ts
浏览文件 @
5d360514
...
...
@@ -179,7 +179,7 @@ export class Message extends PuppetAccessory implements Sayable {
):
Message
{
log
.
verbose
(
'
Message
'
,
'
static createMobileTerminated(%s, %s)
'
,
id
,
payload
,
payload
?
payload
:
''
,
)
/**
* Must NOT use `Message` at here
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
5d360514
...
...
@@ -704,9 +704,10 @@ export class PuppetPuppeteer extends Puppet {
const
contact
=
this
.
Contact
.
load
(
contactId
)
await
contact
.
ready
()
const
fileName
=
(
contact
.
name
()
||
'
unknown
'
)
+
'
-avatar.jpg
'
return
FileBox
.
fromRemote
(
avatarUrl
,
contact
.
name
()
||
'
unknown
'
+
'
-avatar.jpg
'
,
fileName
,
headers
,
)
...
...
@@ -1305,8 +1306,11 @@ export class PuppetPuppeteer extends Puppet {
const
re
=
/
\.[
a-z0-9
]{1,7}
$/i
if
(
!
re
.
test
(
filename
))
{
const
ext
=
rawPayload
.
MMAppMsgFileExt
||
this
.
extname
(
rawPayload
)
filename
+=
'
.
'
+
ext
if
(
rawPayload
.
MMAppMsgFileExt
)
{
filename
+=
'
.
'
+
rawPayload
.
MMAppMsgFileExt
}
else
{
filename
+=
this
.
extname
(
rawPayload
)
}
}
log
.
silly
(
'
PuppetPuppeteer
'
,
'
filename()=%s, build from rawPayload
'
,
filename
)
...
...
src/room.ts
浏览文件 @
5d360514
...
...
@@ -17,7 +17,7 @@
*
* @ignore
*/
import
*
as
util
from
'
util
'
//
import * as util from 'util'
import
{
FileBox
,
...
...
@@ -262,9 +262,9 @@ export class Room extends PuppetAccessory implements Sayable {
.
map
(
id
=>
this
.
puppet
.
Contact
.
load
(
id
))
.
map
(
contact
=>
contact
.
ready
()),
)
log
.
silly
(
'
Room
'
,
'
ready() this.payload="%s"
'
,
util
.
inspect
(
payload
),
)
//
log.silly('Room', 'ready() this.payload="%s"',
//
util.inspect(payload),
//
)
this
.
payload
=
payload
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录