为了账号安全,请及时绑定邮箱和手机立即绑定

Whatsmeow 对话数据

Whatsmeow 对话数据

Go
婷婷同学_ 2023-03-21 10:34:07
我正在尝试使用whatsmeow为 WhatsApp构建一个tui 客户端。翻了半天文档,还是没找到获取个别联系人对话数据的方法。任何帮助表示赞赏。我找到了ParseWebMessage但我不确定如何使用它。chatJID, err := types.ParseJID(conv.GetId())for _, historyMsg := range conv.GetMessages() {    evt, err := cli.ParseWebMessage(chatJID, historyMsg.GetMessage())    yourNormalEventHandler(evt)}事实上,我什至不确定这是否是我要找的
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

好吧,您基本上链接到了文档中包含您要查找的信息的部分。ParseWebMessage调用的返回类型events.Message记录在此处。它包含一个Info类型字段MessageInfo(再次记录在此处)。反过来,这种MessageInfo类型嵌入了MessageSource类型see docs here看起来像这样:

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

}

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


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)

}

您也可以通过简单地执行以下操作来跳过您发送的消息:


if evt.Info.IsFromMe {

    continue

}

和字段都是类型,记录在这里evt.Info.Chat。这种 ID 类型基本上有 2 种变体:用户和服务器 JID 以及 AD-JID(用户、代理和设备)。您可以通过检查标志来区分两者。evt.Info.SenderJIDJID.AD

我根本没有使用过这个模块,我只是简单地浏览了文档,但据我了解,这个模块允许您编写一个处理程序,它将接收您收到的所有内容的类型events.Message。通过检查evt.Info.IsGroup,您可以了解我们发送的消息是在群聊中,还是在您的个人对话中。根据evt.Info.Senderevt.Info.Chat,您可以计算出消息的发送者。作为evt.Info.SenderJID 反过来允许您调用方法GetUserInfo传入 JID,这会为您UserInfo返回一个对象,如此处记录的那样,显示名称、图片、状态等...

所以我猜你正在寻找这些方面的东西:

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

    }

}

请注意,该ContatInfo类型没有立即出现在文档中,但我在 repo 中偶然发现了它。


无论哪种方式,我都不太确定你想做什么,以及你是如何/为什么被困住的。查找此信息所需要做的就是检查ParseWebMessage您提到的方法的返回类型,检查几种类型,并滚动浏览一些列出/记录的方法以大致了解如何获取所有数据可能需要...


查看完整回答
反对 回复 2023-03-21
  • 1 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信