Add command to create WhatsApp group
This commit is contained in:
74
commands.go
74
commands.go
@@ -131,7 +131,7 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
||||
handler.CommandLogout(ce)
|
||||
case "toggle-presence":
|
||||
handler.CommandPresence(ce)
|
||||
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join":
|
||||
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join", "create":
|
||||
if !ce.User.HasSession() {
|
||||
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
||||
return
|
||||
@@ -155,6 +155,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
||||
handler.CommandInviteLink(ce)
|
||||
case "join":
|
||||
handler.CommandJoin(ce)
|
||||
case "create":
|
||||
handler.CommandCreate(ce)
|
||||
}
|
||||
default:
|
||||
ce.Reply("Unknown Command")
|
||||
@@ -249,6 +251,75 @@ func (handler *CommandHandler) CommandJoin(ce *CommandEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
const cmdCreateHelp = `create - Create a group chat.`
|
||||
|
||||
func (handler *CommandHandler) CommandCreate(ce *CommandEvent) {
|
||||
if ce.Portal != nil {
|
||||
ce.Reply("This is already a portal room")
|
||||
return
|
||||
}
|
||||
|
||||
members, err := ce.Bot.JoinedMembers(ce.RoomID)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get room members: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var roomNameEvent event.RoomNameEventContent
|
||||
err = ce.Bot.StateEvent(ce.RoomID, event.StateRoomName, "", &roomNameEvent)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get room name")
|
||||
return
|
||||
} else if len(roomNameEvent.Name) == 0 {
|
||||
ce.Reply("Please set a name for the room first")
|
||||
return
|
||||
}
|
||||
|
||||
var encryptionEvent event.EncryptionEventContent
|
||||
err = ce.Bot.StateEvent(ce.RoomID, event.StateEncryption, "", &encryptionEvent)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get room encryption status")
|
||||
return
|
||||
}
|
||||
|
||||
participants := []string{ce.User.JID}
|
||||
for userID := range members.Joined {
|
||||
jid, ok := handler.bridge.ParsePuppetMXID(userID)
|
||||
if ok && jid != ce.User.JID {
|
||||
participants = append(participants, jid)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := ce.User.Conn.CreateGroup(roomNameEvent.Name, participants)
|
||||
if err != nil {
|
||||
ce.Reply("Failed to create group: %v", err)
|
||||
return
|
||||
}
|
||||
portal := handler.bridge.GetPortalByJID(database.GroupPortalKey(resp.GroupID))
|
||||
portal.roomCreateLock.Lock()
|
||||
defer portal.roomCreateLock.Unlock()
|
||||
if len(portal.MXID) != 0 {
|
||||
portal.log.Warnln("Detected race condition in room creation")
|
||||
// TODO race condition, clean up the old room
|
||||
}
|
||||
portal.MXID = ce.RoomID
|
||||
portal.Name = roomNameEvent.Name
|
||||
portal.Encrypted = encryptionEvent.Algorithm == id.AlgorithmMegolmV1
|
||||
if !portal.Encrypted && handler.bridge.Config.Bridge.Encryption.Default {
|
||||
_, err = portal.MainIntent().SendStateEvent(portal.MXID, event.StateEncryption, "", &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1})
|
||||
if err != nil {
|
||||
portal.log.Warnln("Failed to enable e2be:", err)
|
||||
}
|
||||
portal.Encrypted = true
|
||||
}
|
||||
|
||||
portal.Update()
|
||||
portal.UpdateBridgeInfo()
|
||||
|
||||
ce.Reply("Successfully created WhatsApp group %s", portal.Key.JID)
|
||||
ce.User.addPortalToCommunity(portal)
|
||||
}
|
||||
|
||||
const cmdSetPowerLevelHelp = `set-pl [user ID] <power level> - Change the power level in a portal room. Only for bridge admins.`
|
||||
|
||||
func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) {
|
||||
@@ -540,6 +611,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
||||
cmdPrefix + cmdPMHelp,
|
||||
cmdPrefix + cmdInviteLinkHelp,
|
||||
cmdPrefix + cmdJoinHelp,
|
||||
cmdPrefix + cmdCreateHelp,
|
||||
cmdPrefix + cmdSetPowerLevelHelp,
|
||||
cmdPrefix + cmdDeletePortalHelp,
|
||||
cmdPrefix + cmdDeleteAllPortalsHelp,
|
||||
|
||||
Reference in New Issue
Block a user