跳到主要内容

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