Friendship
发送,接受好友请求的Class
Friendship
发送,接受好友请求的Class,有以下三种:
- 发送好友请求 
- 在 Friend event 中收到好友请求 
- 在 Friend event 中确认好友请求 
Kind: global class
- instance - .accept() ⇒ - Promise <void>
- .hello() ⇒ - string
- .contact() ⇒ - Contact
- .type() ⇒ - FriendshipType
 
- static - .add(contact, hello) ⇒ - Promise <void>
 
 
friendship.accept() ⇒ Promise <void>
Promise <void>通过好友请求
Kind: instance method of Friendship
Example
const bot = new Wechaty()
bot.on('friendship', async friendship => {
  try {
    console.log(`received friend event.`)
    switch (friendship.type()) {
    // 1. New Friend Request
    case Friendship.Type.Receive:
      await friendship.accept()
      break
    // 2. Friend Ship Confirmed
    case Friendship.Type.Confirm:
      console.log(`friend ship confirmed`)
      break
    }
  } catch (e) {
    console.error(e)
  }
}
.start()friendship.hello() ⇒ string
string获取对方发送好友请求的验证信息。
Kind: instance method of Friendship
Example (If request content is `ding`, then accept the friendship)
const bot = new Wechaty()
bot.on('friendship', async friendship => {
  try {
    console.log(`received friend event from ${friendship.contact().name()}`)
    if (friendship.type() === Friendship.Type.Receive && friendship.hello() === 'ding') {
      await friendship.accept()
    }
  } catch (e) {
    console.error(e)
  }
}
.start()friendship.contact() ⇒ Contact
Contact获取发送好友请求的联系人。
Kind: instance method of Friendship
Example
const bot = new Wechaty()
bot.on('friendship', async friendship => {
  const contact = friendship.contact()
  const name = contact.name()
  console.log(`received friend event from ${name}`)
}
.start()friendship.type() ⇒ FriendshipType
FriendshipType返回好友请求的类型。
FriendshipType 在这里是enum:
- FriendshipType.Unknown 
- FriendshipType.Confirm 
- FriendshipType.Receive 
- FriendshipType.Verify 
Kind: instance method of Friendship
Example (如果用户发送好友请求的内容是 `ding`, 机器人会自动通过好友请求)
const bot = new Wechaty()
bot.on('friendship', async friendship => {
  try {
    if (friendship.type() === Friendship.Type.Receive && friendship.hello() === 'ding') {
      await friendship.accept()
    }
  } catch (e) {
    console.error(e)
  }
}
.start()Friendship.send()
Deprecated
请使用 Friendship#add 替换
Kind: static method of Friendship
Friendship.add(contact, hello) ⇒ Promise <void>
Promise <void>发送好友请求给contact 发送的内容是 hello.
最佳实践:每分钟加1次,如果发送的过于频繁,你可能会被封号,每天能添加的好友数量是有上限的。
Kind: static method of Friendship
Param
Type
Description
contact
Contact
需要发送好友请求的联系人
hello
string
发送好友请求的内容
Example
const memberList = await room.memberList()
for (let i = 0; i < memberList.length; i++) {
  await bot.Friendship.add(member, 'Nice to meet you! I am wechaty bot!')
}Last updated
Was this helpful?
