Wechaty
NewsBlogDevelopersDocs
中文版
中文版
  • 介绍
  • 快速开始
  • API 文档
    • Wechaty
    • Message
    • Contact
    • ContactSelf
    • Room
    • RoomInvitation
    • Friendship
  • 示例代码
  • Puppet
  • 常见问题
  • 最佳实践
  • 参与贡献
  • 了解更多
    • 视频教程
    • 进阶
    • 测试
    • 目录结构
    • ReleaseLog
    • ChangeLog
    • Awesome Wechaty
    • Contributors
    • 关于token的所有问题
Powered by GitBook
On this page
  • Friendship
  • friendship.accept() ⇒ Promise <void>
  • friendship.hello() ⇒ string
  • friendship.contact() ⇒ Contact
  • friendship.type() ⇒ FriendshipType
  • Friendship.send()
  • Friendship.add(contact, hello) ⇒ Promise <void>

Was this helpful?

  1. API 文档

Friendship

发送,接受好友请求的Class

PreviousRoomInvitationNext示例代码

Last updated 5 years ago

Was this helpful?

Friendship

发送,接受好友请求的Class,有以下三种:

  1. 发送好友请求

  2. 在 Friend event 中收到好友请求

  3. 在 Friend event 中确认好友请求

Kind: global class

    • instance

      • ⇒ Promise <void>

      • ⇒ string

      • ⇒ Contact

      • ⇒ FriendshipType

    • static

      • ⇒ Promise <void>

friendship.accept() ⇒ Promise <void>

通过好友请求

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

获取对方发送好友请求的验证信息。

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

获取发送好友请求的联系人。

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 在这里是enum:

  • FriendshipType.Unknown

  • FriendshipType.Confirm

  • FriendshipType.Receive

  • FriendshipType.Verify

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(contact, hello) ⇒ Promise <void>

发送好友请求给contact 发送的内容是 hello.

最佳实践:每分钟加1次,如果发送的过于频繁,你可能会被封号,每天能添加的好友数量是有上限的。

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!')
}

Kind: instance method of Example

Kind: instance method of Example (If request content is `ding`, then accept the friendship)

Kind: instance method of Example

Kind: instance method of Example (如果用户发送好友请求的内容是 `ding`, 机器人会自动通过好友请求)

请使用 替换

Kind: static method of

Kind: static method of

Examples/Friend-Bot
Friendship
.accept()
.hello()
.contact()
.type()
.send()
.add(contact, hello)
Friendship
Friendship
Friendship
Friendship
Friendship#add
Friendship
Friendship