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
.
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
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)
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)
from typing import List
from wechaty import Wechaty, Contact
class MyBot(Wechaty):
async def on_ready(self, _):
contacts: List[Contact] = await self.Contact.find_all()
for contact in contacts:
print(f'id<{contact.contact_id}>, name<{contact.name}>, type<{contact.type()}>')
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
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.
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
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)
import { Contact } from 'wechaty'
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)
from typing import List, Optional
from wechaty import Wechaty, Contact
from wechaty_puppet.schemas.contact import ContactQueryFilter
class MyBot(Wechaty):
async def on_ready(self, _):
# find by id
filehelper: Optional[Contact] = await self.Contact.find('filehelper')
if filehelper:
print(f'filehelper<{filehelper}>')
# find by name
contacts: List[Contact] = await self.Contact.find_all(ContactQueryFilter(name='your-friend-name'))
print(f'total number of contacts: {len(contacts)}')
for contact in contacts:
print(contact)
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!