Skip to content

Latest commit

 

History

History
356 lines (250 loc) · 10.1 KB

contact.md

File metadata and controls

356 lines (250 loc) · 10.1 KB

Contact

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

Examples/Contact-Bot

Kind: global class

contact.say(textOrContactOrFile)

Return the type of: Promise.<void>

Kind: instance method of Contact

Param Type Description
textOrContactOrFile string | Contact | FileBox send text, Contact, or file to contact.
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.fromLocal('/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()

Return the type of: string

Get the name from a contact

Kind: instance method of Contact
Example

const name = contact.name()

contact.alias(newAlias)

Return the type of: string | null | Promise.<boolean>

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 {(string | null)})

const alias = 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.stranger()

Deprecated

Should use friend instead

Kind: instance method of Contact

contact.friend()

Return the type of: boolean | null

Check if contact is friend

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.official()

Deprecated

Check if it's a offical account, should use type instead

Kind: instance method of Contact

contact.personal()

Deprecated

Check if it's a personal account, should use type instead

Kind: instance method of Contact

contact.type()

Return the type of: 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()

Return the type of: 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()

Return the type of: string | null

Get the region 'province' from a contact

Kind: instance method of Contact
Example

const province = contact.province()

contact.city()

Return the type of: string | null

Get the region 'city' from a contact

Kind: instance method of Contact
Example

const city = contact.city()

contact.avatar()

Return the type of: 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.refresh()

Deprecated

Force reload(re-ready()) data for Contact, use sync instead

Kind: instance method of Contact

contact.sync()

Return the type of: Promise.<this>

Force reload(re-ready()) data for Contact,

Kind: instance method of Contact
Example

await contact.sync()

contact.self()

Return the type of: 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.load(id)

Return the type of: Contact

Get Contact by id

Kind: static method of Contact

Param Type
id string

Example

const bot = new Wechaty()
await bot.start()
const contact = bot.Contact.load('contactId')

Contact.find(query)

Return the type of: 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.<(Contact|null)> - 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])

Return the type of: Promise.<Array.<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 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'