Wechaty
NewsBlogDevelopersDocs
中文版
中文版
  • 介绍
  • 快速开始
  • API 文档
    • Wechaty
    • Message
    • Contact
    • ContactSelf
    • Room
    • RoomInvitation
    • Friendship
  • 示例代码
  • Puppet
  • 常见问题
  • 最佳实践
  • 参与贡献
  • 了解更多
    • 视频教程
    • 进阶
    • 测试
    • 目录结构
    • ReleaseLog
    • ChangeLog
    • Awesome Wechaty
    • Contributors
    • 关于token的所有问题
Powered by GitBook
On this page
  • Message
  • message.from() ⇒ Contact
  • message.to() ⇒ Contact | null
  • message.room() ⇒ Room | null
  • message.content()
  • message.text() ⇒ string
  • message.toRecalled() ⇒ Promise
  • message.say(textOrContactOrFileOrUrl, [mention]) ⇒ Promise.
  • message.self() ⇒ boolean
  • message.mention() ⇒ Promise.
  • message.mentionSelf() ⇒ Promise.
  • message.forward(to) ⇒ Promise.
  • message.date()
  • message.age() ⇒ number
  • message.file()
  • message.toFileBox() ⇒ Promise.
  • message.toContact() ⇒ Promise.
  • Message.find()
  • Message.findAll()

Was this helpful?

  1. API 文档

Message

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

PreviousWechatyNextContact

Last updated 5 years ago

Was this helpful?

Message

Kind: global class

    • instance

      • ⇒ Contact | null

      • ⇒ Contact | null

      • ⇒ Room | null

      • ~~[.content()](message.md#message-content)~~

      • . ⇒ string

      • ⇒ Promise.

      • ⇒ MessageType

      • ⇒ boolean

      • ⇒ Promise.>

      • ⇒ Promise.

      • ⇒ Promise.

      • ⇒ number

      • ~~[.file()](message.md#message-file)~~

      • ⇒ Promise.

      • ⇒ Promise.

    • static

message.from() ⇒ Contact

获取发送消息的联系人

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()获取微信群信息。

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

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() ⇒ string

获取消息的文本内容。

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

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

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.

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

Param

Type

Description

textOrContactOrFileOrUrl

string | Contact | FileBox | UrlLink

发送文本、名片或者文件

// 1. send Image

// 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

### 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

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

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

message.mention() ⇒ Promise.

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

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

message.mentionSelf() ⇒ Promise.

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

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

message.forward(to) ⇒ Promise.

转发收到的消息

Param

Type

Description

to

Sayable | Array.

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

message.date()

消息发送的时间

message.age() ⇒ number

消息的时差

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

message.file()

Deprecated

message.toFileBox() ⇒ Promise.

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

message.toContact() ⇒ Promise.

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

Message.find()

在缓存中找消息。

Message.findAll()

在缓存中找消息

Kind: instance method of Example

Kind: instance method of

Kind: instance method of Example

请使用

Kind: instance method of

Kind: instance method of Example

Kind: instance method of Example

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

Kind: instance method of See:

你可以使用 来发送文件

if (/^ding$/i.test(m.text())) { const fileBox = FileBox.fromUrl('') await msg.say(fileBox) }

if (/^link$/i.test(m.text())) { const linkPayload = new UrlLnik({ description : 'Netty', thumbnailUrl: '', title : 'Netty', url : '', }) await msg.say(linkPayload) } .start()

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

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

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

Kind: instance method of

Kind: instance method of

Kind: instance method of

使用

Kind: instance method of

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

Kind: instance method of

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

Kind: instance method of

Kind: static method of

Kind: static method of

https://chatie.io/wechaty/images/bot-qr-code.png
http://mmbiz.qpic.cn/mmbiz_jpg/48MFTQpxichmmxEoXZ1w7eno72H2MQdx1WC6JiaVdYRmwAp4MCcQbctE2IE7jWqkWOlgMPqMBXVAdR1N46xEibvoQ/640?wx_fmt=jpeg&wxtype=jpeg&wxfrom=0
http://mp.weixin.qq.com/s?__biz=MzU2MDU3MzE1Mg==&mid=2247484375&idx=1&sn=5ee91b0a8607a1766b5212a23d3c9179&chksm=fc04bc58cb73354e798403bcc03e293149bb115a0755940e334c0fbe33d7c3b0b0797120a213&scene=0&xtrack=1#rd
Examples/Ding-Dong-Bot
Message
.from()
.to()
.room()
text()
.say(textOrContactOrFile, [mention])
.type()
.self()
.mention()
.mentionSelf()
.forward(to)
.date()
.age()
.toFileBox()
.toContact()
.find()
.findAll()
Message
Message
Message
message.text()
Message
Message
Message
Examples/ding-dong-bot
Message
Message
Message
Message
Message
Message
Message
toFileBox
Message
Message
Message
Message
Message
FileBox
puppet兼容性列表
Puppet 兼容性列表
Puppet 兼容性列表