ContactSelf
机器人自己的信息将会封装一个ContactSelf 类. 这个类继承自 Contact
ContactSelf
Kind: global class
Kind: instance method of ContactSelf
contactSelf.avatar() ⇒ Promise
获取机器人的头像
Example ( GET the avatar for bot, return {Promise<FileBox>})
// Save avatar to local file like `1-name.jpg`
bot.on('login', (user: ContactSelf) => {
console.log(`user ${user} login`)
const file = await user.avatar()
const name = file.name
await file.toFile(name, true)
console.log(`Save bot avatar: ${contact.name()} with avatar file: ${name}`)
})
contactSelf.avatar(file) ⇒ Promise<void>
Promise<void>
设置 机器人的头像
Param
Type
file
FileBox
Example (SET the avatar for a bot)
import { FileBox } from 'file-box'
bot.on('login', (user: ContactSelf) => {
console.log(`user ${user} login`)
const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png')
await user.avatar(fileBox)
console.log(`Change bot avatar successfully!`)
})
contactSelf.qrcode() ⇒ Promise<string>
Promise<string>
获取机器人的二维码。
Kind: instance method of ContactSelf
Example
import { generate } from 'qrcode-terminal'
bot.on('login', (user: ContactSelf) => {
console.log(`user ${user} login`)
const qrcode = await user.qrcode()
console.log(`Following is the bot qrcode!`)
generate(qrcode, { small: true })
})
contactSelf.name() ⇒ string
string
获取 机器人昵称。
Kind: instance method of ContactSelf
Example
bot.on('login', async user => {
console.log(`user ${user} login`)
console.log(`user name: ${user.name()}`)
})
contactSelf.name(name) ⇒ Promise<string>
Promise<string>
修改机器人昵称。
Kind: instance method of ContactSelf
Param
Description
name
机器人要修改的昵称内容
Example
bot.on('login', async user => {
console.log(`user ${user} login`)
const oldName = user.name()
try {
await user.name(`${oldName}-${new Date().getTime()}`)
} catch (e) {
console.error('change name failed', e)
}
})
contactSelf.signature(signature): Promise<void>
Promise<void>
修改机器人签名。
Kind: instance method of ContactSelf
Param
Description
signature
机器人要修改的签名内容
Example
bot.on('login', async user => {
console.log(`user ${user} login`)
try {
await user.signature(`Signature changed by wechaty on ${new Date()}`)
} catch (e) {
console.error('change signature failed', e)
}
})
Last updated
Was this helpful?