Coder Social home page Coder Social logo

Comments (7)

dosubot avatar dosubot commented on June 10, 2024 2

🤖

嘿,@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.

dosubot avatar dosubot commented on June 10, 2024 2

🤖

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.

dosubot avatar dosubot commented on June 10, 2024 2

🤖

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.

sgshy1995 avatar sgshy1995 commented on June 10, 2024

@dosu-bot 我按照你的代码await m.say('Hello!', members[0]),或者await m.say('Hello!', m.talker()),都没有效果,并不能@到指定的人。

from wechaty.

sgshy1995 avatar sgshy1995 commented on June 10, 2024

@dosu-bot 按照你的代码,可以@到他,但貌似只是写了一段文字而已,微信本身并没有识别到@他。是否你的@方法实现有问题?

from wechaty.

MarkShawn2020 avatar MarkShawn2020 commented on June 10, 2024

@dosu-bot 按照你的代码,可以@到他,但貌似只是写了一段文字而已,微信本身并没有识别到@他。是否你的@方法实现有问题?

  • 用 room.say 它可以支持第2...n个参数(对象)
  • 用message.say则不可以
  • 用 conversation.say 可以兼容以上二者

from wechaty.

aqpmzngldh avatar aqpmzngldh commented on June 10, 2024

@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)

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.