跳到主要内容

Manage contacts

Use the guide to help you integrate additional functions to an existing project which is present at Github/Contact-Bot or check that your existing local system will run on Wechaty. If, you wish to learn on how to build the bot on your own, please visit one of our Building the bot section.

The steps outlined here mainly focus on working with Javascript, but user are free to switch between any languages. All wechaty contacts are encapsulated as a Contact.

Prerequisites

  • Your system must have Node.js installed (version >= 16).
  • Your system must have Wechaty (version >= 0.40).
  • You need to be familiar with the basics of Wechaty platform. If not, follow our tutorials section.
  • You need to have at least a minimal application ready to work, follow one of our Example/Contact-Bot.

If you don't know where to start from

See Running our first ding-dong bot.

The functions below require a basic script to help run the bot. Import the codes from Github/Contact-Bot for the basic script, and integrate the codes below.

All contacts - define how to list all contact

This section help you list down all your contacts from the Instant messaging platform you choose to intergrate this bot by its id, name & type.

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 - define how to search within contacts

This guide help you find your contact from the list of contacts from the Instant messaging platform you choose to intergrate this bot.

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)

References