Message

所有的微信消息会被封装成一个Message 类

Message

Examples/Ding-Dong-Bot

Kind: global class

message.from() ⇒ Contact

获取发送消息的联系人

Kind: instance method of Message Example

const bot = new Wechaty()
bot
.on('message', async m => {
  const contact = msg.from()
  const text = msg.text()
  const room = msg.room()
  if (room) {
    const topic = await room.topic()
    console.log(`Room: ${topic} Contact: ${contact.name()} Text: ${text}`)
  } else {
    console.log(`Contact: ${contact.name()} Text: ${text}`)
  }
})
.start()

message.to() ⇒ Contact | null

获取消息发送的联系人。在微信群中,Message.to() 会返回null,使用Message.room()获取微信群信息。

Kind: instance method of Message

Example

const bot = new Wechaty()
bot
.on('message', async m => {
  const contact = message.from()
  const text = message.text()
  const toContact = message.to()
  if (toContact) {
    const name = toContact.name()
    console.log(`toContact: ${name} Contact: ${contact.name()} Text: ${text}`)
  } else {
    console.log(`Contact: ${contact.name()} Text: ${text}`)
  }
})
.start()

message.room() ⇒ Room | null

获取消息所在的微信群,如果这条消息不在微信群中,会返回null

Kind: instance method of Message Example

const bot = new Wechaty()
bot
.on('message', async m => {
  const contact = msg.from()
  const text = msg.text()
  const room = msg.room()
  if (room) {
    const topic = await room.topic()
    console.log(`Room: ${topic} Contact: ${contact.name()} Text: ${text}`)
  } else {
    console.log(`Contact: ${contact.name()} Text: ${text}`)
  }
})
.start()

message.content()

Deprecated

请使用 message.text()

Kind: instance method of Message

message.text() ⇒ string

获取消息的文本内容。

Kind: instance method of Message Example

const bot = new Wechaty()
bot
.on('message', async m => {
  const contact = msg.from()
  const text = msg.text()
  const room = msg.room()
  if (room) {
    const topic = await room.topic()
    console.log(`Room: ${topic} Contact: ${contact.name()} Text: ${text}`)
  } else {
    console.log(`Contact: ${contact.name()} Text: ${text}`)
  }
})
.start()

message.toRecalled() ⇒ Promise

获取撤回消息的文本内容。

Kind: instance method of Message Example

const bot = new Wechaty()
bot
.on('message', async m => {
  if (m.type() === MessageType.Recalled) {
    const recalledMessage = await m.toRecalled()
    console.log(`Message: ${recalledMessage} has been recalled.`)
  }
})
.start()

message.say(textOrContactOrFileOrUrl, [mention]) ⇒ Promise.

回复多媒体、微信名片、文本或者链接给这条消息的发送者。

这个功能是否能实现取决于你使用的是哪一个Puppet, 详情参考:puppet兼容性列表

Kind: instance method of Message See: Examples/ding-dong-bot

Param

Type

Description

textOrContactOrFileOrUrl

string | Contact | FileBox | UrlLink

发送文本、名片或者文件

你可以使用 FileBox 来发送文件

// 1. send Image

if (/^ding$/i.test(m.text())) { const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png') await msg.say(fileBox) }

// 2. send Text

if (/^dong$/i.test(m.text())) { await msg.say('dingdingding') }

// 3. send Contact

if (/^lijiarui$/i.test(m.text())) { const contactCard = await bot.Contact.find({name: 'lijiarui'}) if (!contactCard) { console.log('not found') return } await msg.say(contactCard) }

})

// 4. send UrlLink

if (/^link$/i.test(m.text())) { const linkPayload = new UrlLnik({ description : 'Netty', thumbnailUrl: 'http://mmbiz.qpic.cn/mmbiz_jpg/48MFTQpxichmmxEoXZ1w7eno72H2MQdx1WC6JiaVdYRmwAp4MCcQbctE2IE7jWqkWOlgMPqMBXVAdR1N46xEibvoQ/640?wx_fmt=jpeg&wxtype=jpeg&wxfrom=0', title : 'Netty', url : 'http://mp.weixin.qq.com/s?__biz=MzU2MDU3MzE1Mg==&mid=2247484375&idx=1&sn=5ee91b0a8607a1766b5212a23d3c9179&chksm=fc04bc58cb73354e798403bcc03e293149bb115a0755940e334c0fbe33d7c3b0b0797120a213&scene=0&xtrack=1#rd', }) await msg.say(linkPayload) } .start()

### message.type\(\) ⇒ `MessageType`

获取消息的类型

{% hint style="info" %}

**Kind**: instance method of [`Message`](message.md#message)  
**Example**

```javascript
const bot = new Wechaty()
if (message.type() === bot.Message.Type.Text) {
  console.log('This is a text message')
}

message.self() ⇒ boolean

查看这条消息是否为机器人发送的。

Kind: instance method of Message Returns: boolean - - Return true for send from self, false for send from others. Example

if (message.self()) {
 console.log('this message is sent by myself!')
}

message.mention() ⇒ Promise.

获取在群中@的用户列表。

Kind: instance method of Message Returns: Promise.> - - Return message mentioned contactList Example

const contactList = await message.mention()
console.log(contactList)

message.mentionSelf() ⇒ Promise.

获取机器人是否在群里被@ 了

Kind: instance method of Message Returns: Promise. - - Return true for mention me. Example

if (await message.mentionSelf()) {
 console.log('this message were mentioned me! [You were mentioned] tip ([有人@我]的提示)')
}

message.forward(to) ⇒ Promise.

转发收到的消息

Kind: instance method of Message

Param

Type

Description

to

Sayable | Array.

Room 或者 Contact。指的是收消息方。

message.date()

消息发送的时间

Kind: instance method of Message

message.age() ⇒ number

消息的时差

例如: 消息在8:43:01发送的,当我们在wechaty 上收到消息的时候,时间是8:43:15,那么 age() 为 8:43:15 - 8:43:01 = 14 (seconds)

Kind: instance method of Message

message.file()

Deprecated

使用 toFileBox

Kind: instance method of Message

message.toFileBox() ⇒ Promise.

从消息中提取多媒体文件并把它 存入到FileBox 里面。

这个方法是否能实现,取决于用的是什么Puppet,具体请看:Puppet 兼容性列表

Kind: instance method of Message

message.toContact() ⇒ Promise.

提取转发的微信好友名片内容,并封装成Contact 类型。

这个方法是否能实现,取决于用的是什么Puppet,具体请看:Puppet 兼容性列表

Kind: instance method of Message

Message.find()

在缓存中找消息。

Kind: static method of Message

Message.findAll()

在缓存中找消息

Kind: static method of Message

Last updated