diff --git a/example/contact-bot.ts b/example/contact-bot.ts new file mode 100644 index 0000000000000000000000000000000000000000..752071e92f320d8f77a19e4c30a35b6784e0803b --- /dev/null +++ b/example/contact-bot.ts @@ -0,0 +1,86 @@ +/** + * + * Wechaty - Wechat for Bot + * + * Connecting ChatBots + * https://github.com/wechaty/wechaty + * + */ +import { + Config + , Contact + , Wechaty + , log +} from '../' + +const welcome = ` +=============== Powered by Wechaty =============== +-------- https://github.com/wechaty/wechaty -------- + +Hello, + +I'm a Wechaty Botie with the following super powers: + +1. List all your contacts with weixn id & name + +__________________________________________________ + +Hope you like it, and you are very welcome to +upgrade me for more super powers! + +Please wait... I'm trying to login in... + +` + +console.log(welcome) +const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE }) + +bot +.on('login' , function(this, user) { + log.info('Bot', `${user.name()} logined`) + this.say('wechaty contact-bot just logined') + main() +}) +.on('logout' , user => log.info('Bot', `${user.name()} logouted`)) +.on('error' , e => log.info('Bot', 'error: %s', e)) +.on('scan', (url, code) => { + if (!/201|200/.test(String(code))) { + let loginUrl = url.replace(/\/qrcode\//, '/l/') + require('qrcode-terminal').generate(loginUrl) + } + console.log(`${url}\n[${code}] Scan QR Code in above url to login: `) +}) + +bot.init() +.catch(e => { + log.error('Bot', 'init() fail: %s', e) + bot.quit() + process.exit(-1) +}) + +async function main() { + const contactList = await Contact.findAll() + + log.info('Bot', '#######################') + log.info('Bot', 'Contact number: %d\n', contactList.length) + + const MAX = 17 + for (let i = 0; i < contactList.length; i++ ) { + const contact = contactList[i] + await contact.ready() + + if (!contact.weixin()) { + await contact.refresh() + } + + log.info('Bot', 'Contact: %s: %s', contact.weixin(), contact.name()) + + if (i > MAX) { + log.info('Bot', 'Contact too many, I only show you the first %d ... ', MAX) + break + } + } + + log.info('Bot', 'I will re-dump contact names after 7 second... ') + setTimeout(main, 7000) +} diff --git a/src/contact.ts b/src/contact.ts index 53923744c5a686b029e6ed906ea0dd63d1711f6c..a65dbbcfde67d9b4b3b0847a7c0ca9b11c0acb36 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -53,6 +53,7 @@ export class Contact implements Sayable { private static pool = new Map() private obj: ContactObj | null + private dirtyObj: ContactObj | null private rawObj: ContactRawObj constructor(public readonly id: string) { @@ -89,6 +90,7 @@ export class Contact implements Sayable { } } + public weixin() { return this.obj && this.obj.weixin || '' } public name() { return UtilLib.plainText(this.obj && this.obj.name || '') } public remark() { return this.obj && this.obj.remark } public stranger() { return this.obj && this.obj.stranger } @@ -100,6 +102,14 @@ export class Contact implements Sayable { return !!(this.obj && this.obj.id) } + public async refresh(): Promise { + if (this.isReady()) { + this.dirtyObj = this.obj + } + this.obj = null + return this.ready() + } + public async ready(contactGetter?: (id: string) => Promise): Promise { log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')') if (!this.id) { @@ -157,7 +167,10 @@ export class Contact implements Sayable { return selfId === userId } - public static findAll(query: ContactQueryFilter): Promise { + public static findAll(query?: ContactQueryFilter): Promise { + if (!query) { + query = { name: /.*/ } + } log.verbose('Cotnact', 'findAll({ name: %s })', query.name) const name = query.name @@ -192,7 +205,7 @@ export class Contact implements Sayable { log.verbose('Contact', 'find(%s)', query.name) const contactList = await Contact.findAll(query) - if (!contactList || contactList.length < 1) { + if (!contactList || !contactList.length) { throw new Error('find not found any contact') } return contactList[0]