Comments (7)
🤖
嘿,@sgshy1995!很高兴再次见到你。希望你一切都好!
是的,Wechaty支持在回复消息中@某个人。你可以通过Message
类的say
方法来实现这个功能。这个方法接受一个mention
参数,这个参数可以是一个Contact
对象,或者是一个Contact
对象的数组。当你设置了mention
参数,你就可以在房间中@某个联系人。
以下是如何使用的示例:
const bot = new Wechaty()
bot
.on('message', async m => {
const room = m.room()
if (room) {
const members = await room.memberAll() // 获取房间中的所有成员
if (members.length > 0) {
await m.say('Hello!', members[0]) // 在文本中@第一个成员
}
}
})
.start()
在这个示例中,m.say('Hello!', members[0])
在原始消息m
来自的房间中发送一条消息,并在房间中@第一个成员。
关于Message
类的say
方法如何在Wechaty代码库中内部处理mention
参数,say
方法并不直接处理mention
参数。相反,它接受一个sayable
参数,这个参数可以是字符串,Contact,FileBox,UrlLink,MiniProgram,或者这些类型的列表。
然而,mention
功能是在Message
类的mentionList
方法中处理的。这个方法返回在消息中被提及的联系人的列表。它首先检查消息类型是否为Text,以及消息是否在一个房间中。如果不是,它返回一个空列表。
如果消息的有效载荷包含一个mentionIdList
,它将这些ID映射到联系人,并返回列表。如果没有,它处理消息的文本,提取提及列表。它将文本按AT_SEPARATOR_REGEX
分割,过滤出不包含'@'的字符串,并处理这些字符串,得到提及的名字列表。然后,它将这些名字映射到房间中的联系人,并返回联系人列表。
这是相关的代码:
async mentionList (): Promise<ContactInterface[]> {
log.verbose('Message', 'mentionList()')
const room = this.room()
if (this.type() !== PUPPET.types.Message.Text || !room) {
return []
}
if (this.payload
&& 'mentionIdList' in this.payload
&& Array.isArray(this.payload.mentionIdList)
) {
const idToContact = (id: string) => this.wechaty.Contact.find({ id })
const allContact = await Promise.all(
this.payload.mentionIdList
.map(idToContact),
)
return allContact.filter(Boolean) as ContactInterface[]
}
// process the message and get the mention list
const atList = this.text().split(AT_SEPARATOR_REGEX)
if (atList.length === 0) return []
const rawMentionList = atList
.filter(str => str.includes('@'))
.map(str => multipleAt(str))
let mentionNameList: string[] = []
mentionNameList = mentionNameList.concat.apply([], rawMentionList)
mentionNameList = mentionNameList.filter(s => !!s)
const contactListNested = await Promise.all(
mentionNameList.map(
name => room.memberAll(name),
),
)
let contactList: ContactInterface[] = []
contactList = contactList.concat.apply([], contactListNested)
return contactList
}
来源:
Sources
About Dosu
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.
🤖
Based on the information you've provided, it seems like you're trying to use the say
method to mention a user directly, which is not supported. The say
method in Wechaty is used to send messages, but it doesn't support mentioning a user directly.
If you want to mention a user in a group chat, you can use the mention
method of the Message
class. Here's an example:
const room = await bot.Room.find({topic: "Your Room Topic"});
const contact = await bot.Contact.find({name: "Contact Name"});
if (room && contact) {
await room.say(`Hello, ${contact.name()}!`, contact);
}
In this example, room.say
is used to send a message in a group chat, and the second argument is the contact to be mentioned. Please replace "Your Room Topic" and "Contact Name" with your actual room topic and contact name.
If you're not in a group chat and you want to send a direct message to a user, you can just use the say
method of the Contact
class:
const contact = await bot.Contact.find({name: "Contact Name"});
if (contact) {
await contact.say("Hello!");
}
In this example, contact.say
is used to send a direct message to a user. Please replace "Contact Name" with your actual contact name.
For more information, you can refer to the Wechaty Contact class in the Wechaty codebase.
Also, there was a similar issue in the Wechaty repository: 我在新版本中使用群消息时候,@不到人, which was last updated on November 25, 2023. You might find the discussion there helpful.
I hope this helps! If you have any other questions or need further clarification, feel free to ask.
Sources
About Dosu
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.
🤖
Based on the context provided, it seems like the issue you're experiencing with the mention method in Wechaty might be due to the puppet you're using. Certain features, including the mention functionality, are only supported by specific puppets. For instance, the puppet-padplus supports the mention functionality. If you're not using puppet-padplus, this could be the reason why the mention method is not working as expected. You might want to consider switching to puppet-padplus or another puppet that supports the mention functionality.
The say
method in Wechaty is implemented to take a Sayable
object as an argument, which can be a string, a Contact, a FileBox, or a UrlLink. It then retrieves the talker (the sender of the message) and the room (if the message is sent in a group chat). If the message is sent in a room, it calls the say
method of the room with the Sayable
object. If the message is not sent in a room, it calls the say
method of the talker with the Sayable
object. You can refer to the source code for more details.
The mentionList
method retrieves the room where the message is sent and checks if the message type is text and if it is sent in a room. If not, it returns an empty array. The rest of the implementation involves processing the message text to extract the mentioned contacts and returning them as an array of ContactInterface
objects.
I hope this helps! If you have any more questions or need further clarification, feel free to ask.
Sources
About Dosu
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.
@dosu-bot 我按照你的代码await m.say('Hello!', members[0])
,或者await m.say('Hello!', m.talker())
,都没有效果,并不能@到指定的人。
from wechaty.
@dosu-bot 按照你的代码,可以@到他,但貌似只是写了一段文字而已,微信本身并没有识别到@他。是否你的@方法实现有问题?
from wechaty.
@dosu-bot 按照你的代码,可以@到他,但貌似只是写了一段文字而已,微信本身并没有识别到@他。是否你的@方法实现有问题?
- 用 room.say 它可以支持第2...n个参数(对象)
- 用message.say则不可以
- 用 conversation.say 可以兼容以上二者
from wechaty.
@dosu-bot 按照你的代码,可以@到他,但貌似只是写了一段文字而已,微信本身并没有识别到@他。是否你的@方法实现有问题?
- 用 room.say 它可以支持第2...n个参数(对象)
- 用message.say则不可以
- 用 conversation.say 可以兼容以上二者
Caught exception: Error: query must provide at least one key. current query is empty.
uncaughtException GError: query must provide at least one key. current query is empty.
at PuppetWechat4u.contactQueryFilterFactory (file:///www/wwwroot/wxq/node_modules/wechaty-puppet/dist/esm/src/mixins/contact-mixin.js:117:23)
at PuppetWechat4u.contactSearch (file:///www/wwwroot/wxq/node_modules/wechaty-puppet/dist/esm/src/mixins/contact-mixin.js:64:41)
at async Function.findAll (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/user-modules/contact.js:129:35)
at async Function.find (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/user-modules/contact.js:84:29)
at async Promise.all (index 0)
at async PuppetWechat4u. (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/wechaty-mixins/puppet-mixin.js:273:56) {
code: 2,
details: 'Error: query must provide at least one key. current query is empty.\n' +
' at PuppetWechat4u.contactQueryFilterFactory (file:///www/wwwroot/wxq/node_modules/wechaty-puppet/dist/esm/src/mixins/contact-mixin.js:117:23)\n' +
' at PuppetWechat4u.contactSearch (file:///www/wwwroot/wxq/node_modules/wechaty-puppet/dist/esm/src/mixins/contact-mixin.js:64:41)\n' +
' at async Function.findAll (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/user-modules/contact.js:129:35)\n' +
' at async Function.find (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/user-modules/contact.js:84:29)\n' +
' at async Promise.all (index 0)\n' +
' at async PuppetWechat4u. (file:///www/wwwroot/wxq/node_modules/wechaty/dist/esm/src/wechaty-mixins/puppet-mixin.js:273:56)'
}
from wechaty.
Related Issues (20)
- 企业微信遇到的问题 HOT 4
- 重新扫码padlocal两三天了还没有人修复嘛,wechat bad request error: 你的应用版本过低,请升级至最新版本后再登录。点击“确定”后将跳转至最新版下载页面,登录不了咯, HOT 7
- 我想监听群聊中其他人进入群聊的事件,是否有提供呢 HOT 5
- 为什么使用bot.Contact.findAll()方法查找到的联系人名称都是空呢 HOT 8
- 你的应用版本过低,请升级至最新版本后再登录。点击“确定”后将跳转至 最新版下载页面,依赖都是最新版的这个怎么解决呢 HOT 2
- 你的应用版本过低,请升级至最新版本后再登录。点击“确定”后将跳转至 最新版下载页面,依赖都是最新版的这个怎么解决呢? HOT 2
- 告诉我现在个人微信可以支持发送语音的所有puppet HOT 2
- Workpro 企业微信小程序消息发送失败
- 如何识别 引用 类型的消息,目前用text获取不到 HOT 1
- 【wechaty问题交流微信群】一起讨论,吐槽,解决wechaty中遇到的坑 HOT 1
- contact.friend() HOT 1
- 什么时候可以支持消息的引用啊
- updating README
- PadLocal是不是带着小姨子跑路了 HOT 1
- 短时间多次调用room.say发送图片,发送间隔很长 HOT 5
- 你的应用版本过低,请升级击“确定”后将跳转至最新版下载页面。 HOT 1
- wechaty版本1.18+怎么获取用户唯一标识? HOT 1
- 如何强制刷新群聊列表? HOT 3
- 消息发送完成后,没有实际展示,也没有报错
- 经常中断 uncaughtException GError [AxiosError]: timeout of 60000ms exceeded
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.