Skip to main content

Contact Class

All wechat contacts(friend) will be encapsulated as a Contact.

Classesโ€‹

All wechat contacts(friend) will be encapsulated as a Contact. Examples/Contact-Bot

Contactโ€‹

All wechat contacts(friend) will be encapsulated as a Contact.

Propertiesโ€‹

NameTypeDescription
idstringGet Contact id. This function is depending on the Puppet Implementation, see puppet-compatible-table

Global Class Contactโ€‹

Instance Methodsโ€‹

Instance MethodsReturn type
say(text Or Contact Or File Or Url)Promise
name()String
alias(newAlias)Promise
friend()Boolean or null
type()ContactType.Unknown or ContactType.Personal or ContactType.Official
gender()ContactGender.Unknown or ContactGender.Male or ContactGender.Female
province()String or null
city()String or null
avatar()Promise
sync()Promise
self()Boolean

Static Methodsโ€‹

Static MethodsReturn Type
find(query)Promise <Contact \| null>
findAll(Query Arguements)Promise <Contact []>

contact.say(textOrContactOrFileOrUrlLinkOrMiniProgram) โ‡’ Promise <void>โ€‹

Tips: This function is depending on the Puppet Implementation, see puppet-compatible-table

ParamTypeDescription
textOrContactOrFileOrUrlLinkOrMiniProgramstring | Contact | FileBox | UrlLink | MiniProgramsend text, Contact, file or UrlLink to contact. You can use FileBox to send file

Exampleโ€‹

import { FileBox }  from 'file-box'
import {
Wechaty,
UrlLink,
MiniProgram,
} from 'wechaty'

const bot = new Wechaty()
await bot.start()
const contact = await bot.Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of your contact name in wechat

// 1. send text to contact

await contact.say('welcome to wechaty!')

// 2. send media file to contact

import { FileBox } from 'file-box'
const fileBox1 = FileBox.fromUrl('https://wechaty.github.io/wechaty/images/bot-qr-code.png')
const fileBox2 = FileBox.fromFile('/tmp/text.txt')
await contact.say(fileBox1)
await contact.say(fileBox2)

// 3. send contact card to contact

const contactCard = bot.Contact.load('contactId')
await contact.say(contactCard)

// 4. send url link to contact

const urlLink = new UrlLink({
description : 'WeChat Bot SDK for Individual Account, Powered by TypeScript, Docker, and Love',
thumbnailUrl: 'https://avatars0.githubusercontent.com/u/25162437?s=200&v=4',
title : 'Welcome to Wechaty',
url : 'https://github.com/wechaty/wechaty',
})
await contact.say(urlLink)

// 5. send MiniProgram (only supported by `wechaty-puppet-macpro`)

const miniProgram = new MiniProgram ({
appid : 'gh_0aa444a25adc',
title : 'ๆˆ‘ๆญฃๅœจไฝฟ็”จAuthing่ฎค่ฏ่บซไปฝ๏ผŒไฝ ไนŸๆฅ่ฏ•่ฏ•ๅง',
pagePath : 'routes/explore.html',
description : '่บซไปฝ็ฎกๅฎถ',
thumbUrl : '30590201000452305002010002041092541302033d0af802040b30feb602045df0c2c5042b777875706c6f61645f31373533353339353230344063686174726f6f6d3131355f313537363035393538390204010400030201000400',
thumbKey : '42f8609e62817ae45cf7d8fefb532e83',
});

await contact.say(miniProgram);

contact.name() โ‡’ stringโ€‹

The method gets the name from a contact.Check the below illustation for implementation.

Exampleโ€‹

const name = contact.name()

contact.alias(newAlias) โ‡’ Promise <null | string | void>โ€‹

The method gets or sets or deletes the alias for a contact.Tests show it will failed if set alias too frequently(60 times in one minute).

ParamType
newAliasnone | string | null

Example ( GET the alias for a contact, return {(Promise<string | null>)})โ€‹

const alias = await contact.alias()
if (alias === null) {
console.log('You have not yet set any alias for contact ' + contact.name())
} else {
console.log('You have already set an alias for contact ' + contact.name() + ':' + alias)
}

Example (SET the alias for a contact)โ€‹

try {
await contact.alias('lijiarui')
console.log(`change ${contact.name()}'s alias successfully!`)
} catch (e) {
console.log(`failed to change ${contact.name()} alias!`)
}

Example (DELETE the alias for a contact)โ€‹

try {
const oldAlias = await contact.alias(null)
console.log(`delete ${contact.name()}'s alias successfully!`)
console.log(`old alias is ${oldAlias}`)
} catch (e) {
console.log(`failed to delete ${contact.name()}'s alias!`)
}

Contact.friend() โ‡’ boolean | nullโ€‹

The method checks if contact is friend.It returns true for friend of the bot and false for not friend of the bot, null for unknown.

Tips: This function is depending on the Puppet Implementation, see puppet-compatible-table

Exampleโ€‹

const isFriend = contact.friend()

Contact.type() โ‡’ ContactType.Unknown | ContactType.Personal | ContactType.Officialโ€‹

This method returns the type of the Contact.Check the example below for implementation.

Tips: ContactType is enum here.

Exampleโ€‹

const bot = new Wechaty()
await bot.start()
const isOfficial = contact.type() === bot.Contact.Type.Official

contact.gender() โ‡’ ContactGender.Unknown | ContactGender.Male | ContactGender.Femaleโ€‹

The method gets the Contact gender.Check the below example for implementation.

Tips: ContactGender is enum here.

Exampleโ€‹

const gender = contact.gender() === bot.Contact.Gender.Male

Contact.province() โ‡’ string | nullโ€‹

This method gets the region 'province' from a contact.Check the below example for implementation.

Exampleโ€‹

const province = contact.province()

Contact.city() โ‡’ string | nullโ€‹

This method gets the region 'city' from a contact.Check the below example for implementation.

Exampleโ€‹

const city = contact.city()

Contact.avatar() โ‡’ Promise <FileBox>โ€‹

This method gets avatar picture from file stream.Check the below example for implementation.

Exampleโ€‹

// Save avatar to local file like `1-name.jpg`

const file = await contact.avatar()
const name = file.name
await file.toFile(name, true)
console.log(`Contact: ${contact.name()} with avatar file: ${name}`)

Contact.sync() โ‡’ Promise <void>โ€‹

The method force reload of data for Contact, Sync data from lowlevel API again.Check the below example for implementation.

Exampleโ€‹

await contact.sync()

Contact.self() โ‡’ booleanโ€‹

The method checks if contact is self.It returns boolean - True for contact is self, False for contact is others.Check the below example for implementation.

Exampleโ€‹

const isSelf = contact.self()

Static Methodsโ€‹

Contact.find(query) โ‡’ Promise <Contact | null>โ€‹

The method finds the contact by name or alias, if the result more than one, return the first one.Try to find a contact by filter: {name: string | RegExp} / {alias: string | RegExp}.The returns Promise. - If it can find the contact,or return null.

ParamType
queryContactQueryFilter

Exampleโ€‹

const bot = new Wechaty()
await bot.start()
const contactFindByName = await bot.Contact.find({ name:"ruirui"} )
const contactFindByAlias = await bot.Contact.find({ alias:"lijiarui"} )

Contact.findAll([queryArg]) โ‡’ Promise <Contact []>โ€‹

This method finds contact by name or alias.If you use Contact.findAll() get the contact list of the bot. Include the contacts from bot's rooms.

Definitionโ€‹

  • name the name-string set by user-self, should be called name
  • alias the name-string set by bot for others, should be called alias
ParamType
queryArgContactQueryFilter

Exampleโ€‹

const bot = new Wechaty()
await bot.start()
const contactList = await bot.Contact.findAll() // get the contact list of the bot
const contactList = await bot.Contact.findAll({ name: 'ruirui' }) // find all of the contacts whose name is 'ruirui'
const contactList = await bot.Contact.findAll({ alias: 'lijiarui' }) // find all of the contacts whose alias is 'lijiarui'

Typedefsโ€‹

ContactQueryFilterโ€‹

It is a global typedef used to search contacts.

Propertiesโ€‹

NameTypeDescription
namestringThe name-string set by user-self, should be called name
aliasstringThe name-string set by bot for others, should be called alias More Detail