Skip to main content

Managing contacts

List all contacts#

import { Contact } from 'wechaty'
async function onReady () {
const contactList = await bot.Contact.findAll()
console.info('Total number of contacts:', contactList.length)
for (const contact of contactList) {
console.info('Id:', contact.id)
console.info('Name:', contact.name())
const type = contact.type()
console.info('Type:', Contact.Type[type])
}
}
bot.on('ready', onReady)

Search in contacts#

Contact.find and Contact.findAll supports search by id, name, alias, weixin.

async function onReady () {
// find by id:
const filehelper = await bot.Contact.find('filehelper')
console.info('filehelper:', filehelper)
// find by name:
const nameContainsJList = await bot.Contact.findAll({ name: /j/i })
console.info('Total number of contacts:', nameContainsJList.length)
for (const contact of nameContainsJList) {
console.info('contact:', contact)
}
}
bot.on('ready', onReady)
Last updated on by Souvik Biswas