How to Use Invite Links Programmatically with wacli to Join WhatsApp Groups
Use wacli groups join --code <CODE> from the command line, or call JoinGroupWithLink(ctx, code) from the Go API in internal/wa/groups.go to programmatically join WhatsApp groups using invite links.
The wacli project by steipete provides a command-line interface and Go library for automating WhatsApp operations. When you need to use invite links programmatically with wacli to join groups, the tool exposes direct methods that wrap the underlying WhatsMeow library. This guide covers both the CLI commands and the Go API implementation based on the actual source code.
Core Methods for Invite Link Operations
The primary functionality resides in internal/wa/groups.go, which exposes two core operations for managing invite links programmatically.
GetGroupInviteLink
This method retrieves the current invite link for a group you administer. It can optionally force a reset to generate a new link and invalidate the previous one.
// internal/wa/groups.go lines 65-73
func (c *Client) GetGroupInviteLink(ctx context.Context, group types.JID, reset bool) (string, error) {
// Implementation forwards to WhatsMeow
}
JoinGroupWithLink
This method consumes an invite code—the string segment after the final / in a WhatsApp group invite URL—and joins the authenticated client to that group.
// internal/wa/groups.go lines 75-82
func (c *Client) JoinGroupWithLink(ctx context.Context, code string) (types.JID, error) {
// Implementation forwards to WhatsMeow
}
Using the CLI to Join Groups with Invite Codes
For command-line automation, wacli provides the groups join sub-command implemented in cmd/wacli/groups_invite_join.go.
Command Structure
wacli groups join --code <INVITE_CODE>
The invite code is the final segment of the URL. For example, if the invite link is https://chat.whatsapp.com/ABcDefGhIjK, the code is ABcDefGhIjK.
CLI Execution Flow
The CLI command performs the following operations:
- Parses the
--codeflag. - Creates an
appinstance vianewApp(), which initializes the high-level façade over the WhatsMeow client. - Ensures authentication via
EnsureAuthed(). - Establishes connection via
Connect(). - Executes
JoinGroupWithLinkat line 143 ofcmd/wacli/groups_invite_join.go:
// cmd/wacli/groups_invite_join.go line 143
jid, err := a.WA().JoinGroupWithLink(ctx, code)
- Optionally fetches and persists group metadata if
GetGroupInfosucceeds.
Programmatic Implementation in Go
While the CLI provides convenience, you can invoke the same methods directly from Go code to use invite links programmatically with wacli within your own applications.
Joining a Group Programmatically
package main
import (
"context"
"log"
"github.com/steipete/wacli/internal/wa"
)
func main() {
// Initialize client with store path
client, err := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
if err != nil {
log.Fatal(err)
}
// Connect (assume already authenticated)
ctx := context.Background()
if err := client.Connect(ctx, wa.ConnectOptions{AllowQR: false}); err != nil {
log.Fatal(err)
}
// Invite code from URL
inviteCode := "ABcDefGhIjK"
// Join group
jid, err := client.JoinGroupWithLink(ctx, inviteCode)
if err != nil {
log.Fatalf("Failed to join: %v", err)
}
log.Printf("Successfully joined group: %s", jid.String())
}
Generating Invite Links
If you administer a group, generate or reset invite links using GetGroupInviteLink:
package main
import (
"context"
"log"
"github.com/steipete/wacli/internal/wa"
"go.mau.fi/whatsmeow/types"
)
func main() {
client, _ := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
ctx := context.Background()
client.Connect(ctx, wa.ConnectOptions{AllowQR: false})
// Parse group JID
groupJID, _ := types.ParseJID("[email protected]")
// Generate new link (reset=true invalidates old links)
link, err := client.GetGroupInviteLink(ctx, groupJID, true)
if err != nil {
log.Fatal(err)
}
log.Printf("New invite link: %s", link)
}
Complete Workflow Example
This example extracts the invite code from a URL, joins the group, and handles the result:
package main
import (
"context"
"log"
"strings"
"github.com/steipete/wacli/internal/wa"
"go.mau.fi/whatsmeow/types"
)
func extractCode(link string) string {
parts := strings.Split(strings.TrimSpace(link), "/")
return parts[len(parts)-1]
}
func main() {
waClient, _ := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
ctx := context.Background()
waClient.Connect(ctx, wa.ConnectOptions{AllowQR: false})
// Example: obtain a fresh link first
groupJID, _ := types.ParseJID("[email protected]")
freshLink, _ := waClient.GetGroupInviteLink(ctx, groupJID, true)
code := extractCode(freshLink)
joinedJID, err := waClient.JoinGroupWithLink(ctx, code)
if err != nil {
log.Fatalf("Join failed: %v", err)
}
log.Printf("Joined via freshly-generated link: %s", joinedJID.String())
}
Key Source Files and Architecture
Understanding the codebase structure helps when extending or debugging invite link functionality in wacli:
| File | Role |
|---|---|
internal/wa/groups.go |
Contains GetGroupInviteLink and JoinGroupWithLink methods that wrap the WhatsMeow library. |
cmd/wacli/groups_invite_join.go |
Implements the wacli groups join CLI command; parses the --code flag and orchestrates the join operation. |
internal/app/app.go |
Provides the App.WA() accessor used by CLI commands to reach the underlying WA client. |
internal/wa/client.go |
Handles client lifecycle (authentication, connection) required before any group operation can succeed. |
cmd/wacli/groups.go |
Registers the join sub-command alongside other group management commands. |
The architecture follows a clear separation: the internal/wa package provides the low-level WhatsApp client abstraction, internal/app offers a higher-level application context, and cmd/wacli provides the user-facing CLI interface.
Summary
- CLI approach: Run
wacli groups join --code <CODE>where<CODE>is the final segment of the invite URL. - Go API approach: Call
JoinGroupWithLink(ctx, code)frominternal/wa/groups.goafter initializing and connecting the client. - Link generation: Administrators can obtain or reset invite links via
GetGroupInviteLink(ctx, groupJID, reset)in the same package. - Authentication required: Both methods require an authenticated and connected client, handled automatically by the CLI or manually via
client.Connect()in Go code. - Source locations: Core logic lives in
internal/wa/groups.go, while the CLI wrapper is incmd/wacli/groups_invite_join.go.
Frequently Asked Questions
How do I extract the invite code from a WhatsApp group link?
The invite code is the final segment of the URL after the last forward slash. For example, in https://chat.whatsapp.com/ABcDefGhIjK, the code is ABcDefGhIjK. You can extract this programmatically using standard string splitting operations in any language, or use the strings.Split function in Go as shown in the workflow examples above.
Can I generate a new invite link for a group I administer using wacli?
Yes. Use the GetGroupInviteLink method from internal/wa/groups.go with the reset parameter set to true. This invalidates the previous link and returns a fresh one. The method requires you to pass the group's JID (e.g., [email protected]) and a boolean indicating whether to reset the existing link.
What happens if the invite link is invalid or expired when using JoinGroupWithLink?
The JoinGroupWithLink method returns an error if the code is invalid, expired, or if the group has reached its member limit. Your code should always check the error return value and handle failure cases appropriately. The CLI implementation in cmd/wacli/groups_invite_join.go demonstrates this by logging the error and exiting with a non-zero status code when the join operation fails.
Is authentication required before calling JoinGroupWithLink or GetGroupInviteLink?
Yes. Both methods require an authenticated and connected WhatsApp client. The CLI handles this automatically through EnsureAuthed() and Connect() calls before executing the join operation. When using the Go API directly, you must call client.Connect() with valid credentials stored in the SQLite database at your specified StorePath before attempting any group operations.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →