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

#34 add example for contact-bot

上级 3435e8cc
/**
*
* 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)
}
...@@ -53,6 +53,7 @@ export class Contact implements Sayable { ...@@ -53,6 +53,7 @@ export class Contact implements Sayable {
private static pool = new Map<string, Contact>() private static pool = new Map<string, Contact>()
private obj: ContactObj | null private obj: ContactObj | null
private dirtyObj: ContactObj | null
private rawObj: ContactRawObj private rawObj: ContactRawObj
constructor(public readonly id: string) { constructor(public readonly id: string) {
...@@ -89,6 +90,7 @@ export class Contact implements Sayable { ...@@ -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 name() { return UtilLib.plainText(this.obj && this.obj.name || '') }
public remark() { return this.obj && this.obj.remark } public remark() { return this.obj && this.obj.remark }
public stranger() { return this.obj && this.obj.stranger } public stranger() { return this.obj && this.obj.stranger }
...@@ -100,6 +102,14 @@ export class Contact implements Sayable { ...@@ -100,6 +102,14 @@ export class Contact implements Sayable {
return !!(this.obj && this.obj.id) return !!(this.obj && this.obj.id)
} }
public async refresh(): Promise<this> {
if (this.isReady()) {
this.dirtyObj = this.obj
}
this.obj = null
return this.ready()
}
public async ready(contactGetter?: (id: string) => Promise<ContactRawObj>): Promise<this> { public async ready(contactGetter?: (id: string) => Promise<ContactRawObj>): Promise<this> {
log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')') log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')')
if (!this.id) { if (!this.id) {
...@@ -157,7 +167,10 @@ export class Contact implements Sayable { ...@@ -157,7 +167,10 @@ export class Contact implements Sayable {
return selfId === userId return selfId === userId
} }
public static findAll(query: ContactQueryFilter): Promise<Contact[]> { public static findAll(query?: ContactQueryFilter): Promise<Contact[]> {
if (!query) {
query = { name: /.*/ }
}
log.verbose('Cotnact', 'findAll({ name: %s })', query.name) log.verbose('Cotnact', 'findAll({ name: %s })', query.name)
const name = query.name const name = query.name
...@@ -192,7 +205,7 @@ export class Contact implements Sayable { ...@@ -192,7 +205,7 @@ export class Contact implements Sayable {
log.verbose('Contact', 'find(%s)', query.name) log.verbose('Contact', 'find(%s)', query.name)
const contactList = await Contact.findAll(query) const contactList = await Contact.findAll(query)
if (!contactList || contactList.length < 1) { if (!contactList || !contactList.length) {
throw new Error('find not found any contact') throw new Error('find not found any contact')
} }
return contactList[0] return contactList[0]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册