跳到主要内容

Friendship

Wechaty bot allows you to make friends via its global class called Friendship.This section is completely about the Frienship class.

Examples or Friend-Bot

Global class Friendship

The Friendship class allows you to do the following functionalities.

  1. send friend requests
  2. receive friend requests (in friend event)
  3. confirmation of friendship( in friend event)

Instance Method

Instance MethodsReturn Type
accept()Promise (void)
hello()string
contact()contact
type()Frienshiptype

Static Method

Static MethodsReturn Type
add()Promise (void)

friendship.accept()Promise <void>

The method accepts friend request.Check the example below for implementation

Example

const bot = new Wechaty()
bot.on('friendship', async friendship => {
try {
console.log(`received friend event.`)
switch (friendship.type()) {

// 1. New Friend Request

case bot.Friendship.Type.Receive:
await friendship.accept()
break

// 2. Friend Ship Confirmed

case bot.Friendship.Type.Confirm:
console.log(`friend ship confirmed`)
break
}
} catch (e) {
console.error(e)
}
})
.start()

friendship.hello()string

The method verifies message.Check the example below for implementation.

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() === bot.Friendship.Type.Receive && friendship.hello() === 'ding') {
await friendship.accept()
}
} catch (e) {
console.error(e)
}
}
.start()

friendship.contact()Contact

The method gets the contact from friendship.Below is an example for implementation.

Example

const bot = new Wechaty()
bot.on('friendship', friendship => {
const contact = friendship.contact()
const name = contact.name()
console.log(`received friend event from ${name}`)
}
.start()

friendship.type()FriendshipType

The method returns the friendship type.Check the below example for implementation.

Tips: FriendshipType is enum here. </br>

  • FriendshipType.Unknown
  • FriendshipType.Confirm
  • FriendshipType.Receive
  • FriendshipType.Verify

Example (If request content is `ding`, then accept the friendship)

const bot = new Wechaty()
bot.on('friendship', async friendship => {
try {
if (friendship.type() === bot.Friendship.Type.Receive && friendship.hello() === 'ding') {
await friendship.accept()
}
} catch (e) {
console.error(e)
}
}
.start()

Friendship.search(phone)Promise <Contact>

The method search contact by phone number and get a contact. The best practice is to send friend request once per minute. Remember not to do this too frequently, or your account may be blocked.

ParamTypeDescription
phonenumbersearch phone number

Example

const phone = '131xxx1234'
const searchContact = await bot.Friendship.search({
phone,
})

Friendship.add(contact, options)Promise <void>

The method sends a Friend Request to a contact with message hello.The best practice is to send friend request once per minute. Remember not to do this too frequently, or your account may be blocked.

ParamTypeDescription
contactContactSend friend request to contact
optionsFriendshipAddOptionsThe friend request option

Example (add searched contact)

await bot.Friendship.add(searchContact, { hello: 'Nice to meet you! I am wechaty bot!' })

Example (add room member)

const memberList = await room.memberList()
for (let i = 0; i < memberList.length; i++) {
await bot.Friendship.add(member, {
room: message.room(),
hello: 'Nice to meet you! I am wechaty bot!',
})
}

Example (add contact card)

if (message.type() === bot.Message.Type.Contact) {
const contact = await message.toContact()
const options = {
contact: message.talker(),
hello: 'Nice to meet you! I am wechaty bot!',
}
if (message.room()) {
options.room = message.room()
}
await bot.Friendship.add(contact, options)
}