Skip to main content

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.add(contact, hello)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
hellostringThe friend request content

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