基本概述 wechaty 基础
在使用 wechaty 相关 api 之前,请大家先学习基础篇:如何使用 wechaty 框架开发微信机器人详细教程(NodeJs 版本)
设置 script 脚本执行
在 package.json 文件中,配置 "start": "node bot.js"

 在 terminal 终端进行执行
PS C:\Users\Administrator\Desktop\HackerWaking\Wechaty-Project> npm start
> wechaty-project@1.0.0 start
> node index.js
扫描登陆 scan Event
The scan event generates a QR code to integrate your bot to a puppet provider.
扫描事件生成一个QR码
import { Wechaty, ScanStatus, log } from 'wechaty'
async function onScan(qrcode, status) {
  console.info('Scan QR Code to login, status:', status, ScanStatus[status])
  console.info('https://wechaty.js.org/qrcode/' + encodeURIComponent(qrcode))
}
const bot = new Wechaty({
  name: 'bot-name',
})
bot.on('scan', onScan)
bot.start()
  .then(() => log.info('StarterBot', 'Starter Bot Started.'))
  .catch(e => log.error('StarterBot', e))
登陆:设置登陆监听 bot.on('login', bot => {})
function onLogin (bot) {
	console.info('Bot logged in:', bot)
}
bot.on('login', onLogin)
await bot.start()
退出:设置退出监听
function onLogout (user) {
	log.info('StarterBot', '%s logout', user)
}
bot.on('logout',  onLogout)
await bot.start()
监听消息 Message
function onMessage (message) {
	console.info('New message:', message)
}
bot.on('message', onMessage)
await bot.start()
接收的群消息对象 message 展示如下
WechatifiedMessageImpl {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  id: '7458368884477369075',
  payload: {
    id: '7458368884477369075',
    talkerId: '@0d338805848b00ec72e959136c0f98db9fc79cc225b2dfb91f486d6a9319c523',
    text: 'Hello',
    timestamp: 1694002096,
    type: 7,
    roomId: '@@bceca2586516062041ee67998a5ad24bd304cf7ae14b667c6c16e74dd248b544',
    mentionIdList: []
  },
  [Symbol(kCapture)]: false
}
Message 类 常见方法说明
| 方法 | 描述 | 类型 | 
|---|---|---|
| .from() | 获取发送消息的联系人 | Contact / null | 
| .to() | 获取消息发送的联系人。在微信群中,Message.to() 会返回 null,使用 Message.room() 获取微信群信息。 | Contact / null | 
| .room() | 获取消息所在的微信群,如果这条消息不在微信群中,会返回 null | Room / null | 
| .text() | 获取消息的文本内容。 | string | 
| .type() | 获取消息的类型 | MessageType | 
| .self() | 查看这条消息是否为机器人发送的。 | boolean | 
| .mention() | 获取在群中 @ 的用户列表。 | Promise | 
| .mentionSelf() | 获取机器人是否在群里被 @ 了 | Promise | 
群类型 Room
| 方法 | 描述 | 类型 | 
|---|---|---|
| .say(textOrContactOrFileOrUrl, …mentionList) | 在群内发消息,如果设置了 …mentionList 参数,机器人在群内发送消息的时候还会@这些联系人 | Promise <void> | 
| .add(contact) | 邀请好友加入群聊 | Promise <void> | 
| .del(contact) | 将好友移出群聊,这个功能仅在机器人是群主的时候会生效 | Promise <void> | 
| .quit() | 机器人主动退群 | Promise <void> | 
| .topic([newTopic]) | 设置 / 获取 群名称 | Promise <void | string> | 
| .announce([text]) | 设置 / 获取 群公告 | Promise <void | string> | 
| .qrcode() | 获取群二维码,用户可以通过扫描这个二维码加入群聊 | Promise <string> | 
| .alias(contact) | 获取这个联系人在群内的群昵称 | Promise <null | string> | 
| .has(contact) | 检查群内是否有这个群成员 | Promise <boolean> | 
| owner() | 获取群主的信息 | Contact | null | 
| .avatar() | 获取群头像的信息 | Promise <FileBox> | 
onMessage(message) {
  const room = message.room()
  if (room) {
    console.log(room)
    const talker = message.talker(); 
    talker.say("hello, i got your message")
  }
}
talker.say() 私聊发送消息
room.say("hello, good night!") 群聊发送消息
Impl 接口的实现
WechatifiedRoomImpl {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  id: '@@af95d96d3c31bc8999708594b48c5f4d79660cc48285a17706e85be3b92e7b2a',
  payload: {
    adminIdList: [],
    avatar: '/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=0&username=@@af95d96d3c31bc8999708594b48c5f4d79660cc48285a17706e85be3b92e7b2a&skey=@crypt_13d84e54_8d83375971336cd46842d5667ee67649',
    id: '@@af95d96d3c31bc8999708594b48c5f4d79660cc48285a17706e85be3b92e7b2a',
    memberIdList: [
      '@a01b4c748882b1f35b685428cded4ea6386437d974e348240d644c61c883f855',
      '@0209e75ea4788f781e241a1a2ac7936563a90d11f6c0dcc3f067035cb6d5dd91'
    ],
    topic: '雨落晴天, 一语中的'
  },
  [Symbol(kCapture)]: false
}



















