Comments (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
方法的工作原理,它可以接受不同类型的参数:
- 要发送为消息的文本字符串。
- 表示要发送的联系人卡片的
ContactInterface
对象。 - 表示要发送的媒体文件的
FileBox
对象。 - 表示要发送的链接的URL字符串。
- 表示要发送的小程序的
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.
room.say()
的第一个参数是 sayable,后续所有的都是联系人列表。
// 准备一个提及的列表
const mentionList = []
// 找到联系人就添加到 mentionList 列表
// 提及功能,微信只支持文字消息
room.say('你的回复', ...mentionList)
from wechaty.
Related Issues (20)
- 如何监听Friendship 根据手机号查询是空的微信号 HOT 3
- 微信如何回复图片或文件消息 HOT 4
- FileBox不存在 HOT 1
- 是否支持企业微信? HOT 3
- 是否可以在回复的消息中@某个人 HOT 5
- 从getting-started项目启动报Maximum number of redirects exceeded HOT 4
- Wechaty is imported as a type in typescript. HOT 5
- wechaty不能监听到登录后(包括登录)的消息了 HOT 1
- 扫码登录之后直接微信闪退 HOT 1
- wechaty是不更新了吗 HOT 2
- 这两天机器人一直登录几分钟,立马掉线,这是什么问题 HOT 3
- UNAVAILABLE: Network closed for unknown reason HOT 1
- 如果自己想实现企业微信的流程,我需要如何去做 HOT 1
- 名片问题 HOT 3
- 自己部署wechaty HOT 3
- wechaty-puppet-service 获取群列表时,报错:GError: 13 INTERNAL: invalid roomId: 56***********4 HOT 1
- 请问如何发送群消息并@全体成员 HOT 3
- 询问通过Room.findAll和Contact.findAll所查询出来的id是否是唯一的,会发改变吗,用的w4u的协议 HOT 1
- When using the anti recall function, obtain the recall message as undefined HOT 12
- 扫码登录后能用小程序吗? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wechaty.