Contact
All wechat contacts(friend) will be encapsulated as a Contact.
Classes
All wechat contacts(friend) will be encapsulated as a Contact. Examples/Contact-Bot
Typedefs
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
instance
.say(textOrContactOrFileOrUrl) ⇒
Promise <void>
.name() ⇒
string
.alias(newAlias) ⇒
Promise <null | string | void>
.friend() ⇒
boolean
|null
.type() ⇒
ContactType.Unknown
|ContactType.Personal
|ContactType.Official
.gender() ⇒
ContactGender.Unknown
|ContactGender.Male
|ContactGender.Female
.province() ⇒
string
|null
.city() ⇒
string
|null
.avatar() ⇒
Promise <FileBox>
.sync() ⇒
Promise <void>
.self() ⇒
boolean
static
.find(query) ⇒
Promise <Contact | null>
.findAll([queryArg]) ⇒
Promise <Contact []>
contact.say(textOrContactOrFileOrUrlLinkOrMiniProgram) ⇒ Promise <void>
Promise <void>
Tips: This function is depending on the Puppet Implementation, see puppet-compatible-table
Kind: instance method of Contact
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://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)
// 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/chatie/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
string
Get the name from a contact
Kind: instance method of Contact
Example
const name = contact.name()
contact.alias(newAlias) ⇒ Promise <null | string | void>
Promise <null | string | void>
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
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
ContactType.Unknown
| ContactType.Personal
| ContactType.Official
Return the type of the Contact
Tips: ContactType is enum here.
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
ContactGender.Unknown
| ContactGender.Male
| ContactGender.Female
Contact gender
Tips: ContactGender is enum here.
Kind: instance method of Contact
Example
const gender = contact.gender() === bot.Contact.Gender.Male
contact.province() ⇒ string
| null
string
| null
Get the region 'province' from a contact
Kind: instance method of Contact
Example
const province = contact.province()
contact.city() ⇒ string
| null
string
| null
Get the region 'city' from a contact
Kind: instance method of Contact
Example
const city = contact.city()
contact.avatar() ⇒ Promise <FileBox>
Promise <FileBox>
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 <void>
Promise <void>
Force reload data for Contact, Sync data from lowlevel API again.
Kind: instance method of Contact
Example
await contact.sync()
contact.self() ⇒ boolean
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 <Contact | null>
Promise <Contact | null>
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
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 []>
Promise <Contact []>
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 namealias
the name-string set by bot for others, should be called alias
Kind: static method of Contact
Param
Type
queryArg
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'
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
Last updated
Was this helpful?