Add support for custom management room messages (#355)

Co-authored-by: Will Hunt <willh@matrix.org>
This commit is contained in:
Justin Carlson 2021-10-28 06:58:20 -04:00 committed by GitHub
parent 13cb1e5c5b
commit 0063c2a804
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 1 deletions

View file

@ -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"`
@ -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

View file

@ -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.

View file

@ -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()
@ -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) {