Home > Net >  Whatsmeow conversation data
Whatsmeow conversation data

Time:11-28

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

CodePudding user response:

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...

  • Related