Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom management room messages #355

Merged
merged 9 commits into from
Oct 28, 2021
11 changes: 11 additions & 0 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ type BridgeConfig struct {

CommandPrefix string `yaml:"command_prefix"`

ManagementRoomText struct {
Welcome string `yaml:"welcome"`
WelcomeConnected string `yaml:"welcome_connected"`
WelcomeUnconnected string `yaml:"welcome_unconnected"`
AdditionalHelp string `yaml:"additional_help"`
} `yaml:"management_room_text"`

Encryption struct {
Allow bool `yaml:"allow"`
Default bool `yaml:"default"`
Expand Down Expand Up @@ -136,6 +143,10 @@ func (bc *BridgeConfig) setDefaults() {
bc.PrivateChatPortalMeta = false
bc.BridgeNotices = true
bc.EnableStatusBroadcast = true

bc.ManagementRoomText.Welcome = "Hello, I'm a WhatsApp bridge bot."
bc.ManagementRoomText.WelcomeConnected = "Use `help` for help."
bc.ManagementRoomText.WelcomeUnconnected = "Use `help` for help or `login` to log in."
}

type umBridgeConfig BridgeConfig
Expand Down
12 changes: 12 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ bridge:
# The prefix for commands. Only required in non-management rooms.
command_prefix: "!wa"

# Messages sent upon joining a management room.
# Markdown is supported. The defaults are listed below.
management_room_text:
# Sent when joining a room.
welcome: "Hello, I'm a WhatsApp bridge bot."
# Sent when joining a management room and the user is already logged in.
welcome_connected: "Use `help` for help."
# Sent when joining a management room and the user is not logged in.
welcome_unconnected: "Use `help` for help or `login` to log in."
# Optional extra text sent when joining a management room.
# additional_help: "This would be some additional text in case you need it."

# End-to-bridge encryption support options.
#
# See https://docs.mau.fi/bridges/general/end-to-bridge-encryption.html for more info.
Expand Down
24 changes: 23 additions & 1 deletion matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func (mx *MatrixHandler) joinAndCheckMembers(evt *event.Event, intent *appservic
return members
}

func (mx *MatrixHandler) sendNoticeWithMarkdown(roomID id.RoomID, message string) (*mautrix.RespSendEvent, error) {
intent := mx.as.BotIntent()
content := format.RenderMarkdown(message, true, false)
content.MsgType = event.MsgNotice
return intent.SendMessageEvent(roomID, event.EventMessage, content)
}

func (mx *MatrixHandler) HandleBotInvite(evt *event.Event) {
intent := mx.as.BotIntent()

Expand Down Expand Up @@ -134,11 +141,26 @@ func (mx *MatrixHandler) HandleBotInvite(evt *event.Event) {
return
}

_, _ = mx.sendNoticeWithMarkdown(evt.RoomID, mx.bridge.Config.Bridge.ManagementRoomText.Welcome)

if !hasPuppets && (len(user.ManagementRoom) == 0 || evt.Content.AsMember().IsDirect) {
user.SetManagementRoom(evt.RoomID)
_, _ = intent.SendNotice(user.ManagementRoom, "This room has been registered as your bridge management/status room. Send `help` to get a list of commands.")
_, _ = intent.SendNotice(user.ManagementRoom, "This room has been registered as your bridge management/status room.")
mx.log.Debugln(evt.RoomID, "registered as a management room with", evt.Sender)
}

if evt.RoomID == user.ManagementRoom {
if user.HasSession() {
_, _ = mx.sendNoticeWithMarkdown(evt.RoomID, mx.bridge.Config.Bridge.ManagementRoomText.WelcomeConnected)
} else {
_, _ = mx.sendNoticeWithMarkdown(evt.RoomID, mx.bridge.Config.Bridge.ManagementRoomText.WelcomeUnconnected)
}

additionalHelp := mx.bridge.Config.Bridge.ManagementRoomText.AdditionalHelp
if len(additionalHelp) > 0 {
_, _ = mx.sendNoticeWithMarkdown(evt.RoomID, additionalHelp)
}
}
}

func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, puppet *Puppet, key database.PortalKey) {
Expand Down