diff --git a/examples/contact-bot.ts b/examples/contact-bot.ts index 460fcc9ec724c7df2bd9d22a81b58661be3244be..4ff6a3e36e7ec21d51afe30cded1c95d0c4d4fbb 100644 --- a/examples/contact-bot.ts +++ b/examples/contact-bot.ts @@ -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() } diff --git a/examples/media-file-bot.ts b/examples/media-file-bot.ts index 0f650daf0081e07df29de84a4776a458e342458d..73550c38e409d2251883f89393fe83f05d1197f0 100644 --- a/examples/media-file-bot.ts +++ b/examples/media-file-bot.ts @@ -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' }) -// } diff --git a/package.json b/package.json index 8fcc4005d403cec6a0c2cd4e450ab7a8a831791f..033205026b83b16addb470a95a2dab8edd228f64 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/message.ts b/src/message.ts index 0fdb1cf01b9087f1d2ff6726c89b5a1b105e7fe4..cf6cefd5b3fa32a995bb02f3d71cc7843ab8314d 100644 --- a/src/message.ts +++ b/src/message.ts @@ -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 diff --git a/src/puppet-puppeteer/puppet-puppeteer.ts b/src/puppet-puppeteer/puppet-puppeteer.ts index 7741c52111a2d910293acf9e2d5964c7172d4398..afc5d3a1197b1d988bc12398723680c42511b071 100644 --- a/src/puppet-puppeteer/puppet-puppeteer.ts +++ b/src/puppet-puppeteer/puppet-puppeteer.ts @@ -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) diff --git a/src/room.ts b/src/room.ts index d7360491b4d8610c53402a70d2a2ba8c5c7b0057..f2c643566f25aeb183234b801f28b66cd082de63 100644 --- a/src/room.ts +++ b/src/room.ts @@ -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 }