tulir / whatsmeow

Go library for the WhatsApp web multidevice API
https://go.mau.fi/whatsmeow
Mozilla Public License 2.0
2.29k stars 430 forks source link

Error 400 - Bad Request When Trying to Create a Community #704

Open jrCleber opened 9 hours ago

jrCleber commented 9 hours ago

I'm encountering a 400 Bad Request error when attempting to create a community. The request includes a CreateKey, a list of participants, and the necessary parameters for creating a community (setting IsParent to true). However, the WhatsApp server responds with a "bad-request" error.

func (cli *Client) CreateGroup(req ReqCreateGroup) (*types.GroupInfo, error) {
    participantNodes := make([]waBinary.Node, len(req.Participants), len(req.Participants)+1)
    for i, participant := range req.Participants {
        participantNodes[i] = waBinary.Node{
            Tag:   "participant",
            Attrs: waBinary.Attrs{"jid": participant},
        }
    }
    if req.CreateKey == "" {
        req.CreateKey = cli.GenerateMessageID()
    }
    if req.IsParent {
        if req.DefaultMembershipApprovalMode == "" {
            req.DefaultMembershipApprovalMode = "request_required"
        }
        participantNodes = append(participantNodes, waBinary.Node{
            Tag: "parent",
            Attrs: waBinary.Attrs{
                "default_membership_approval_mode": req.DefaultMembershipApprovalMode,
            },
        })
    } else if !req.LinkedParentJID.IsEmpty() {
        participantNodes = append(participantNodes, waBinary.Node{
            Tag:   "linked_parent",
            Attrs: waBinary.Attrs{"jid": req.LinkedParentJID},
        })
    }
    fmt.Printf("Req: %+v\n", req)
    // WhatsApp web doesn't seem to include the static prefix for these
    key := strings.TrimPrefix(req.CreateKey, "3EB0")
    resp, err := cli.sendGroupIQ(context.TODO(), iqSet, types.GroupServerJID, waBinary.Node{
        Tag: "create",
        Attrs: waBinary.Attrs{
            "subject": req.Name,
            "key":     key,
        },
        Content: participantNodes,
    })
    fmt.Println("Error: ", err)
    if err != nil {
        return nil, err
    }
    groupNode, ok := resp.GetOptionalChildByTag("group")
    if !ok {
        return nil, &ElementMissingError{Tag: "group", In: "response to create group query"}
    }
    return cli.parseGroupNode(&groupNode)
}
Req: {
   Name:Community Test 
   Participants:[123@s.whatsapp.net] 
   CreateKey:3EB03406994909F676E2B9 
   GroupParent:{IsParent:true DefaultMembershipApprovalMode:request_required} 
   GroupLinkedParent:{LinkedParentJID:}
}
Error:  info query returned status 400: bad-request

I tried to create with a business and regular account. The error was the same.