Replace check-invite with resolve-link and add support for business DM links

This commit is contained in:
Tulir Asokan 2021-11-27 11:30:41 +02:00
parent 36bb364f05
commit 4898e79780
3 changed files with 36 additions and 20 deletions

View file

@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"html"
"math"
"sort"
"strconv"
@ -135,7 +136,7 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
handler.CommandLogout(ce)
case "toggle":
handler.CommandToggle(ce)
case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "check-invite", "join", "create", "accept":
case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "resolve", "resolve-link", "join", "create", "accept":
if !ce.User.HasSession() {
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
return
@ -163,8 +164,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
handler.CommandPM(ce)
case "invite-link":
handler.CommandInviteLink(ce)
case "check-invite":
handler.CommandCheckInvite(ce)
case "resolve", "resolve-link":
handler.CommandResolveLink(ce)
case "join":
handler.CommandJoin(ce)
case "create":
@ -253,23 +254,38 @@ func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) {
}
}
const cmdCheckInviteHelp = `check-invite <invite link> - Resolve an invite link and check which group it points at.`
const inviteLinkPrefix = "https://chat.whatsapp.com/"
const cmdResolveLinkHelp = `resolve-link <group or message link> - Resolve a WhatsApp group invite or business message link.`
func (handler *CommandHandler) CommandCheckInvite(ce *CommandEvent) {
func (handler *CommandHandler) CommandResolveLink(ce *CommandEvent) {
if len(ce.Args) == 0 {
ce.Reply("**Usage:** `join <invite link>`")
return
} else if len(ce.Args[0]) <= len(inviteLinkPrefix) || ce.Args[0][:len(inviteLinkPrefix)] != inviteLinkPrefix {
ce.Reply("That doesn't look like a WhatsApp invite link")
ce.Reply("**Usage:** `resolve-link <group or message link>`")
return
}
group, err := ce.User.Client.GetGroupInfoFromLink(ce.Args[0])
if err != nil {
ce.Reply("Failed to get group info: %v", err)
return
if strings.HasPrefix(ce.Args[0], whatsmeow.InviteLinkPrefix) {
group, err := ce.User.Client.GetGroupInfoFromLink(ce.Args[0])
if err != nil {
ce.Reply("Failed to get group info: %v", err)
return
}
ce.Reply("That invite link points at %s (`%s`)", group.Name, group.JID)
} else if strings.HasPrefix(ce.Args[0], whatsmeow.BusinessMessageLinkPrefix) || strings.HasPrefix(ce.Args[0], whatsmeow.BusinessMessageLinkDirectPrefix) {
target, err := ce.User.Client.ResolveBusinessMessageLink(ce.Args[0])
if err != nil {
ce.Reply("Failed to get business info: %v", err)
return
}
message := ""
if len(target.Message) > 0 {
parts := strings.Split(target.Message, "\n")
for i, part := range parts {
parts[i] = "> " + html.EscapeString(part)
}
message = fmt.Sprintf(" The following prefilled message is attached:\n\n%s", strings.Join(parts, "\n"))
}
ce.Reply("That link points at %s (+%s).%s", target.PushName, target.JID.User, message)
} else {
ce.Reply("That doesn't look like a group invite link nor a business message link.")
}
ce.Reply("That invite link points at %s (`%s`)", group.Name, group.JID)
}
const cmdJoinHelp = `join <invite link> - Join a group chat with an invite link.`
@ -278,7 +294,7 @@ func (handler *CommandHandler) CommandJoin(ce *CommandEvent) {
if len(ce.Args) == 0 {
ce.Reply("**Usage:** `join <invite link>`")
return
} else if len(ce.Args[0]) <= len(inviteLinkPrefix) || ce.Args[0][:len(inviteLinkPrefix)] != inviteLinkPrefix {
} else if !strings.HasPrefix(ce.Args[0], whatsmeow.InviteLinkPrefix) {
ce.Reply("That doesn't look like a WhatsApp invite link")
return
}
@ -715,7 +731,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
cmdPrefix + cmdOpenHelp,
cmdPrefix + cmdPMHelp,
cmdPrefix + cmdInviteLinkHelp,
cmdPrefix + cmdCheckInviteHelp,
cmdPrefix + cmdResolveLinkHelp,
cmdPrefix + cmdJoinHelp,
cmdPrefix + cmdCreateHelp,
cmdPrefix + cmdSetPowerLevelHelp,

2
go.mod
View file

@ -8,7 +8,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.9
github.com/prometheus/client_golang v1.11.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
go.mau.fi/whatsmeow v0.0.0-20211122153317-69e01dbebefe
go.mau.fi/whatsmeow v0.0.0-20211127091917-c24e7145c07f
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

4
go.sum
View file

@ -139,8 +139,8 @@ github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8=
github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs=
go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910 h1:9FFhG0OmkuMau5UEaTgiUQ+7cSbtbOQ7hiWKdN8OI3I=
go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910/go.mod h1:AufGrvVh+00Nc07Jm4hTquh7yleZyn20tKJI2wCPAKg=
go.mau.fi/whatsmeow v0.0.0-20211122153317-69e01dbebefe h1:+IyVX4g5MK/gN1L2TSJsbwcoce6grxw8LLw28M6jzwk=
go.mau.fi/whatsmeow v0.0.0-20211122153317-69e01dbebefe/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4=
go.mau.fi/whatsmeow v0.0.0-20211127091917-c24e7145c07f h1:XsulPckHCIro+hxNz2OK5KWP811xfQ2mlbWLoxKo36w=
go.mau.fi/whatsmeow v0.0.0-20211127091917-c24e7145c07f/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=