Contact

Last updated 2 months ago

Classes

Contact

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

Typedefs

ContactQueryFilter

The way to search Contact

Contact

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

Kind: global class Properties

Name

Type

Description

id

string

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

contact.say(textOrContactOrFileOrUrl) ⇒ Promise.

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

Kind: instance method of Contact

Param

Type

Description

textOrContactOrFileOrUrl

string | Contact | FileBox

send text, Contact, or file to contact. </br> You can use FileBox to send file

Example

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://chatie.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)

contact.name() ⇒ string

Get the name from a contact

Kind: instance method of Contact Example

const name = contact.name()

contact.alias(newAlias) ⇒ Promise.

GET / SET / DELETE the alias for a contact

Tests show it will failed if set alias too frequently(60 times in one minute).

Kind: instance method of Contact

Param

Type

newAlias

none | 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

Check if contact is friend

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

Kind: instance method of Contact Returns: boolean | null - True for friend of the bot False for not friend of the bot, null for unknown. Example

const isFriend = contact.friend()

contact.type() ⇒ ContactType.Unknown | ContactType.Personal | ContactType.Official

Return the type of the Contact

Tips: ContactType is enum here.</br>

Kind: instance method of Contact Example

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

contact.gender() ⇒ ContactGender.Unknown | ContactGender.Male | ContactGender.Female

Contact gender

Tips: ContactGender is enum here. </br>

Kind: instance method of Contact Example

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

contact.province() ⇒ string | null

Get the region 'province' from a contact

Kind: instance method of Contact Example

const province = contact.province()

contact.city() ⇒ string | null

Get the region 'city' from a contact

Kind: instance method of Contact Example

const city = contact.city()

contact.avatar() ⇒ Promise.

Get avatar picture file stream

Kind: instance method of Contact 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.

Force reload data for Contact, Sync data from lowlevel API again.

Kind: instance method of Contact Example

await contact.sync()

contact.self() ⇒ boolean

Check if contact is self

Kind: instance method of Contact Returns: boolean - True for contact is self, False for contact is others Example

const isSelf = contact.self()

Contact.find(query) ⇒ Promise.

Try to find a contact by filter: {name: string | RegExp} / {alias: string | RegExp}

Find contact by name or alias, if the result more than one, return the first one.

Kind: static method of Contact Returns: Promise. - If can find the contact, return Contact, or return null

Param

Type

query

ContactQueryFilter

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.>

Find contact by name or alias

If use Contact.findAll() get the contact list of the bot.

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

Kind: static method of Contact

Param

Type

[queryArg]

ContactQueryFilter

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 allof the contacts whose name is 'ruirui'
const contactList = await bot.Contact.findAll({ alias: 'lijiarui' }) // find all of the contacts whose alias is 'lijiarui'

ContactQueryFilter

The way to search Contact

Kind: global typedef Properties

Name

Type

Description

name

string

The name-string set by user-self, should be called name

alias

string

The name-string set by bot for others, should be called alias More Detail