Wechaty
NewsBlogDevelopersDocs
English Version
English Version
  • Introduction
  • Quick Start
  • API Reference
    • Wechaty
    • Contact
    • ContactSelf
    • Room
    • RoomInvitation
    • Message
    • Friendship
  • Examples
  • Puppet
  • FAQ
  • Best Practice
  • Welcome to contribute
  • More
    • Video Tutorial
    • Advanced
    • Code Structure
    • Release Log
    • Change Log
    • Awesome Wechaty
    • Contributors
    • We love feedback
    • Better tools for authors
  • Others
    • Concepts
    • Tech
Powered by GitBook
On this page
  • Message
  • message.from() ⇒ Contact | null
  • message.to() ⇒ Contact | null
  • message.room() ⇒ Room | null
  • message.content()
  • message.text() ⇒ string
  • message.toRecalled() ⇒ Promise <Message | null>
  • message.say(textOrContactOrFileOrUrlLinkOrMiniProgram) ⇒ Promise <void>
  • message.type() ⇒ MessageType
  • message.self() ⇒ boolean
  • message.mention() ⇒ Promise <Contact []>
  • message.mentionSelf() ⇒ Promise <boolean>
  • message.forward(to) ⇒ Promise <void>
  • message.date() ⇒ Date
  • message.age() ⇒ number
  • message.file()
  • message.toFileBox() ⇒ Promise <FileBox>
  • message.toContact() ⇒ Promise <Contact>
  • message.toUrlLink() ⇒ Promise <UrlLink>
  • Message.find() ⇒ Promise <Message | null>
  • Message.findAll() ⇒ Promise <Message []>

Was this helpful?

  1. API Reference

Message

All wechat messages will be encapsulated as a Message.

PreviousRoomInvitationNextFriendship

Last updated 5 years ago

Was this helpful?

Message

All wechat messages will be encapsulated as a Message.

Kind: global class

    • instance

      • ⇒ Contact | null

      • ⇒ Contact | null

      • ⇒ Room | null

      • ⇒ string

      • ⇒ Promise <void>

      • ⇒ MessageType

      • ⇒ boolean

      • ⇒ Promise <Contact []>

      • ⇒ Promise <boolean>

      • ⇒ Promise <void>

      • ⇒ Date

      • ⇒ number

      • ⇒ Promise <FileBox>

      • ⇒ Promise <Contact>

      • ⇒ Promise <UrlLink>

    • static

      • ⇒ Promise <Message>

      • ⇒ Promise <Message []>

message.from() ⇒ Contact | null

Get the sender from a message.

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

Get the destination of the message Message.to() will return null if a message is in a room, use Message.room() to get the 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

Get the room from the message. If the message is not in a room, then will return null

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

Get the text content of the message

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 <Message | null>

Get the text content of the recalled message

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

message.say(textOrContactOrFileOrUrlLinkOrMiniProgram) ⇒ Promise <void>

Reply a Text, Contact Card, Media File or Link message to the sender.

Param

Type

Description

textOrContactOrFileOrUrlLinkOrMiniProgram

string | Contact | FileBox | UrlLink | MiniProgram

Example

import { FileBox }  from 'file-box'
import {
  Wechaty,
  UrlLink,
  MiniProgram,
}  from 'wechaty'

const bot = new Wechaty()
bot
.on('message', async m => {

// 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 urlLink = new UrlLink({
      description: 'Wechaty is a Bot SDK for Wechat Individual Account which can help you create a bot in 6 lines of javascript, with cross-platform support including Linux, Windows, Darwin(OSX/Mac) and Docker.',
      thumbnailUrl: 'https://camo.githubusercontent.com/f310a2097d4aa79d6db2962fa42bb3bb2f6d43df/68747470733a2f2f6368617469652e696f2f776563686174792f696d616765732f776563686174792d6c6f676f2d656e2e706e67',
      title: 'Wechaty',
      url: 'https://github.com/chatie/wechaty',
    });

    await msg.say(urlLink);
  }

// 5. send MiniProgram (only supported by `wechaty-puppet-macpro`)

  if (/^mini-program$/i.test(m.text())) {
    const miniProgram = new MiniProgram ({
      appid              : 'gh_0aa444a25adc',
      title              : '我正在使用Authing认证身份,你也来试试吧',
      pagePath           : 'routes/explore.html',
      description        : '身份管家',
      thumbUrl           : '30590201000452305002010002041092541302033d0af802040b30feb602045df0c2c5042b777875706c6f61645f31373533353339353230344063686174726f6f6d3131355f313537363035393538390204010400030201000400',
      thumbKey           : '42f8609e62817ae45cf7d8fefb532e83',
    });

    await msg.say(miniProgram);
  }
})
.start()

message.type() ⇒ MessageType

Get the type from the message.

Tips: MessageType is Enum here. </br>

  • MessageType.Unknown

  • MessageType.Attachment

  • MessageType.Audio

  • MessageType.Contact

  • MessageType.Emoticon

  • MessageType.Image

  • MessageType.Text

  • MessageType.Video

  • MessageType.Url

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

message.self() ⇒ boolean

Check if a message is sent by self.

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

message.mention() ⇒ Promise <Contact []>

Get message mentioned contactList.

Message event table as follows

Web

Mac PC Client

iOS Mobile

android Mobile

[You were mentioned] tip ([有人@我]的提示)

✘

√

√

√

Identify magic code (8197) by copy & paste in mobile

✘

√

√

✘

Identify magic code (8197) by programming

✘

✘

✘

✘

Identify two contacts with the same roomAlias by [You were mentioned] tip

✘

✘

√

√

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

message.mentionSelf() ⇒ Promise <boolean>

Check if a message is mention self.

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

message.forward(to) ⇒ Promise <void>

Forward the received message.

Param

Type

Description

to

Sayable | Array

Room or Contact The recipient of the message, the room, or the contact

Example

const bot = new Wechaty()
bot
.on('message', async m => {
  const room = await bot.Room.find({topic: 'wechaty'})
  if (room) {
    await m.forward(room)
    console.log('forward this message to wechaty room!')
  }
})
.start()

message.date() ⇒ Date

Message sent date

message.age() ⇒ number

Returns the message age in seconds.

For example, the message is sent at time 8:43:01, and when we received it in Wechaty, the time is 8:43:15, then the age() will return 8:43:15 - 8:43:01 = 14 (seconds)

message.file()

Deprecated

message.toFileBox() ⇒ Promise <FileBox>

Extract the Media File from the Message, and put it into the FileBox.

message.toContact() ⇒ Promise <Contact>

Get Share Card of the Message Extract the Contact Card from the Message, and encapsulate it into Contact class

message.toUrlLink() ⇒ Promise <UrlLink>

Get Url Link of the Message Extract the Url Link from the Message, and encapsulate it into UrlLink class

Message.find() ⇒ Promise <Message | null>

Find message in cache

Message.findAll() ⇒ Promise <Message []>

Find messages in cache

Kind: instance method of Example

Kind: instance method of

Kind: instance method of

use instead

Kind: instance method of

Kind: instance method of Example

Kind: instance method of Example

Tips: This function is depending on the Puppet Implementation, see

Kind: instance method of See:

send text, Contact, UrlLink, MiniProgram or file to bot. You can use to send file

Kind: instance method of Example

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

Kind: instance method of Returns: Promise <Contact []> - - Return message mentioned contactList Example

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

Kind: instance method of

Kind: instance method of

Kind: instance method of

use instead

Kind: instance method of

Tips: This function is depending on the Puppet Implementation, see

Kind: instance method of

Tips: This function is depending on the Puppet Implementation, see

Kind: instance method of

Tips: This function is depending on the Puppet Implementation, see

Kind: instance method of

Kind: static method of

Kind: static method of

puppet-compatible-table
puppet-compatible-table
puppet-compatible-table
puppet-compatible-table
Examples/Ding-Dong-Bot
Message
.from()
.to()
.room()
.content()
.text()
.say(textOrContactOrFile)
.type()
.self()
.mention()
.mentionSelf()
.forward(to)
.date()
.age()
.file()
.toFileBox()
.toContact()
.toUrlLink()
.find()
.findAll()
Message
Message
Message
text
Message
Message
Message
Examples/ding-dong-bot
Message
Message
Message
Message
Message
Message
Message
Message
toFileBox
Message
Message
Message
Message
Message
Message
FileBox