Coder Social home page Coder Social logo

Comments (2)

dosubot avatar dosubot commented on June 2, 2024 2

🤖

你好 @wushd,我是Dosu,一个帮助你解决bug、回答问题和成为贡献者的机器人。在等待人工维护者的同时,我在这里提供帮助。很高兴为你服务!

在Wechaty中,你可以使用Room类的say方法来实现发送群消息并@多个人的功能。你可以通过Contact类的find方法来通过群昵称搜索对应的人员。以下是一个示例代码片段:

import { Wechaty, Contact, Room } from 'wechaty'

const bot = new Wechaty()

bot.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.github.io/qrcode/${encodeURIComponent(qrcode)}`))
bot.on('login', user => console.log(`User ${user} logged in`))

bot.on('ready', async () => {
  // Search for the room
  const room = await bot.Room.find({ topic: "Your Room Topic" }) // change "Your Room Topic" to your room topic

  if (room) {
    // Search for the contacts
    const contact1 = await bot.Contact.find({ name: "Contact1" }) // change "Contact1" to your contact's name
    const contact2 = await bot.Contact.find({ name: "Contact2" }) // change "Contact2" to your contact's name

    if (contact1 && contact2) {
      // Send a message to the room and @ the contacts
      await room.say("Hello!", contact1, contact2)
    }
  }
})

bot.start()

在这段代码中,bot.Room.find用于查找房间,bot.Contact.find用于查找联系人,room.say用于向房间发送消息并@联系人。你可以将"Your Room Topic","Contact1"和"Contact2"替换为你实际的房间主题和联系人名称。

关于Room类的say方法的工作原理,它可以接受不同类型的参数:

  1. 要发送为消息的文本字符串。
  2. 表示要发送的联系人卡片的ContactInterface对象。
  3. 表示要发送的媒体文件的FileBox对象。
  4. 表示要发送的链接的URL字符串。
  5. 表示要发送的小程序的MiniProgram对象。

say方法还可以接受一个可选参数mention,它可以是一个ContactInterface对象或一个ContactInterface对象的数组。如果提供了此参数,发送的消息将提及指定的联系人。

这是方法签名:

say (sayable:  Sayable)                                 : Promise<void | MessageInterface>
say (text:     string, ...mentionList: ContactInterface[])       : Promise<void | MessageInterface>
say (textList: TemplateStringsArray, ...varList: any[]) : Promise<void | MessageInterface>

这是如何使用它的示例:

const room = await bot.Room.find({topic: 'wechaty'})

// Send text inside Room
await room.say('Hello world!')

// Send media file inside Room
import { FileBox }  from 'wechaty'
const fileBox = FileBox.fromUrl('https://wechaty.github.io/wechaty/images/bot-qr-code.png')
await room.say(fileBox)

// Send Contact Card in a room
const contactCard = await bot.Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of the room member
await room.say(contactCard)

// Send text inside room and mention @mention contact
const contact = await bot.Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of the room member
await room.say('Hello world!', contact)

请注意,say方法的返回值是一个Promise,它解析为一个MessageInterface对象或void,具体取决于puppet实现。

参考资料:

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

from wechaty.

zhengxs2018 avatar zhengxs2018 commented on June 2, 2024

room.say() 的第一个参数是 sayable,后续所有的都是联系人列表。

image
// 准备一个提及的列表
const mentionList = []

// 找到联系人就添加到 mentionList 列表

// 提及功能,微信只支持文字消息
room.say('你的回复', ...mentionList)

from wechaty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.