Whatsmeow对话数据

huangapple go评论89阅读模式
英文:

Whatsmeow conversation data

问题

我正在尝试使用whatsmeow构建一个WhatsApp的tui客户端

经过半天的搜索和阅读文档,我仍然找不到获取单个联系人对话数据的方法。希望能得到帮助。

我找到了ParseWebMessage,但我不太确定如何使用它。

chatJID, err := types.ParseJID(conv.GetId())
for _, historyMsg := range conv.GetMessages() {
	evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
	yourNormalEventHandler(evt)
}

事实上,我甚至不确定这是否是我要找的东西。

英文:

I'm trying to build a tui client for WhatsApp using whatsmeow.

After half a day of searching and reading through the docs, I still can't find a way to get the conversation data of individual contacts. Any help is appreciated.

I found ParseWebMessage but I'm not really sure how to use this.

chatJID, err := types.ParseJID(conv.GetId())
for _, historyMsg := range conv.GetMessages() {
	evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
	yourNormalEventHandler(evt)
}

Matter of fact I'm not even sure if this is what I'm looking for

答案1

得分: 1

好的,以下是翻译好的内容:

嗯,你基本上链接到了包含你要查找信息的文档部分。ParseWebMessage 调用的返回类型是 events.Message,在这里有文档记录(链接:https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20221126173344-e660988acdbc/types/events#Message)。它包含一个 MessageInfo 类型的 Info 字段(同样,在这里有文档记录:https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20221126173344-e660988acdbc/types#MessageInfo)。而 MessageInfo 类型嵌入了 MessageSource 类型(参见文档:https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20221126173344-e660988acdbc/types#MessageSource),它的结构如下:

type MessageSource struct {
    Chat     JID  // 消息所在的聊天
    Sender   JID  // 发送消息的用户
    IsFromMe bool // 消息是否由当前用户发送而不是其他人
    IsGroup  bool // 聊天是否为群聊或广播列表

    // 当向广播列表消息发送已读回执时,Chat 是广播列表,Sender 是你,所以该字段包含已读回执的接收者。
    BroadcastListOwner JID
}

因此,要获取发送给定消息的联系人,根据你的代码 evt, err := cli.ParseWebMessage(),你需要检查:

evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
if err != nil {
    // 处理错误
}
fmt.Printf("Sender ID: %s\nSent in Chat: %s\n", evt.Info.Sender, evt.Info.Chat)
if evt.Info.IsGroup {
    fmt.Printf("%s 是一个群聊\n", evt.Info.Chat)
}

你还可以通过以下方式跳过你发送的消息:

if evt.Info.IsFromMe {
    continue
}

evt.Info.Chatevt.Info.Sender 字段都是 JID 类型,文档链接在这里(https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20221126173344-e660988acdbc/types#JID)。实际上,有两种类型的 ID:用户和服务器 JID,以及 AD-JID(用户、代理和设备)。你可以通过检查 JID.AD 标志来区分这两种类型。

我没有完全使用过这个模块,只是简要浏览了文档,但我理解的是,该模块允许你编写一个处理程序,该处理程序将接收到一个 events.Message 类型的消息。通过检查 evt.Info.IsGroup,你可以确定消息是在群聊中发送的,还是在你的一对一对话中发送的。根据 evt.Info.Senderevt.Info.Chat,你可以确定是谁发送了消息。evt.Info.Sender 是一个 JID,你可以调用 GetUserInfo 方法,传入 JID,它会返回一个 UserInfo 对象(在这里有文档记录:https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20221126173344-e660988acdbc/types#UserInfo),显示姓名、图片、状态等等。

所以我猜你想要的是这样的代码:

// 一些来自给定人员直接发送给你的所有消息的映射
contacts := cli.GetAllContacts() // 返回 map[JID]ContactInfo
personMsg := map[string][]*events.Message
evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
if err != nil {
    // 处理
}
if !evt.Info.IsFromMe && !evt.Info.IsGroup { // 不是群聊,也不是我发送的
    info, _ := cli.GetUserInfo([]types.JID{evt.Info.Sender})
    if contact, ok := contacts[info[evt.Info.Sender]]; ok {
        msgs, ok := personMsg[contact.PushName]
        if !ok {
            msgs := []*events.Message{}
        }
        personMsg[contact.PushName] = append(msgs, evt)
    }
}

注意,ContatInfo 类型在文档中没有立即显示,但我在仓库中找到了它。

无论如何,我不太确定你想要做什么,以及你为什么陷入困境。只需要查看你提到的 ParseWebMessage 方法的返回类型,检查一些类型,并浏览一些列出/记录的方法,就可以大致了解如何获取所有可能需要的数据...

英文:

Well, you basically linked to the part of the docs that contains the information you're looking for. The return type of the ParseWebMessage call is events.Message, documented here. It contains an Info field of type MessageInfo (again, documented here). In turn, this MessageInfo type embeds the MessageSource type see docs here which looks like this:

type MessageSource struct {
	Chat     JID  // The chat where the message was sent.
	Sender   JID  // The user who sent the message.
	IsFromMe bool // Whether the message was sent by the current user instead of someone else.
	IsGroup  bool // Whether the chat is a group chat or broadcast list.

	// When sending a read receipt to a broadcast list message, the Chat is the broadcast list
	// and Sender is you, so this field contains the recipient of the read receipt.
	BroadcastListOwner JID
}

So to get the contact who sent a given message, given your code evt, err := cli.ParseWebMessage(), you need to check:

evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
if err != nil {
    // handle error, of course
}
fmt.Printf("Sender ID: %s\nSent in Chat: %s\n", evt.Info.Sender, evt.Info.Chat)
if evt.Info.IsGroup {
    fmt.Printf("%s is a group chat\n", evt.Info.Chat)
}

You can also skip messages you sent by simply doing this:

if evt.Info.IsFromMe {
    continue
}

The evt.Info.Chat and evt.Info.Sender fields are all of type JID, documented here. There essentially are 2 variations of this ID type: user and server JID's and AD-JIDs (user, agent, and device). You can distinguish between the two by checking the JID.AD flag.

I haven't used this module at all, I only scanned through the docs briefly, but as I understand it, this module allows you to write a handler which will receive an events.Message type for everything you receive. By checking the evt.Info.IsGroup, you can work out whether the message we sent in a group chat, or in your person-to-person conversation thing. Based on evt.Info.Sender and evt.Info.Chat, you can work out who sent the message. The evt.Info.Sender being a JID in turn allows you to call the GetUserInfo method, passing in the JID, which gets you a UserInfo object in return as documented here, showing the name, picture, status, etc...

So I guess you're looking for something along these lines:

// some map of all messages from a given person, sent directly to you
contacts := cli.GetAllContacts() // returns map[JID]ContactInfo
personMsg := map[string][]*events.Message
evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())
if err != nil {
    // handle
}
if !evt.Info.IsFromMe && !evt.Info.IsGroup {// not a group, not sent by me
    info, _ := cli.GetUserInfo([]types.JID{evt.Info.Sender})
    if contact, ok := contacts[info[evt.Info.Sender]; ok {
        msgs, ok := personMsg[contact.PushName]
        if !ok {
            msgs := []*events.Message{}
        }
        personMsg[contact.PushName] = append(msgs, evt)
    }
}

Note the ContatInfo type didn't show up in the docs right away, but I stumbled across it in the repo.

Either way, I'm not quite sure what you're trying to do, and how/why you're stuck. All it took to find this information was to check the return type of the ParseWebMessage method you mentioned, check a couple of types, and scroll through some of the listed/documented methods to get a rough idea of how you can get all the data you could possibly need...

huangapple
  • 本文由 发表于 2022年11月28日 00:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/74591851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定