Skip to main content

Deal with messages

Mention

Only a message in the room can mention(@) others.

import { Message } from 'wechaty'

async function onMessage(message: Message): Promise<void> {
if (await message.mentionSelf()) {
const room = message.room()
if (!room) {
throw new Error('Should never reach here: a mention message must in a room')
}

console.info(message.text())
// "@bot Hello"
console.info(await message.mentionList())
// [bot]
console.info(await message.mentionText())
// "Hello"

const talker = room.talker()
await room.say`Thanks for mention me! ${talker}`
}
}

bot.on('message', onMessage)

Self message

import { Message } from 'wechaty'

async function onMessage(message: Message): Promise<void> {
if (message.self()) {
const talker = message.talker()
const bot = message.wechaty.userSelf()
assert(talker === bot, 'Message is sent from bot')
console.info('Message is sent from bot')
}
}

bot.on('message', onMessage)