提交 5d360514 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

fix media file bugs & examples

上级 06e14fb4
......@@ -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
*/
onLogin()
main()
})
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
......@@ -85,7 +86,7 @@ bot.start()
/**
* Main Contact Bot
*/
async function onLogin() {
async function main() {
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()
}
......@@ -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' })
// }
......@@ -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
......
......@@ -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)
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册