Merge branch 'introduction-on-join' into 'main'
Send introduction text and help after the bot joins a room See merge request etke.cc/postmoogle!16
This commit is contained in:
17
bot/bot.go
17
bot/bot.go
@@ -19,14 +19,15 @@ import (
|
|||||||
|
|
||||||
// Bot represents matrix bot
|
// Bot represents matrix bot
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
noowner bool
|
noowner bool
|
||||||
federation bool
|
federation bool
|
||||||
prefix string
|
prefix string
|
||||||
domain string
|
domain string
|
||||||
rooms map[string]id.RoomID
|
rooms map[string]id.RoomID
|
||||||
roomsmu *sync.Mutex
|
roomsmu *sync.Mutex
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
lp *linkpearl.Linkpearl
|
lp *linkpearl.Linkpearl
|
||||||
|
handledJoinEvents sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new matrix bot
|
// New creates a new matrix bot
|
||||||
|
|||||||
@@ -109,6 +109,26 @@ func (b *Bot) parseCommand(message string) []string {
|
|||||||
return strings.Split(message, " ")
|
return strings.Split(message, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
||||||
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("sendIntroduction"))
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
var msg strings.Builder
|
||||||
|
msg.WriteString("Hello!\n\n")
|
||||||
|
msg.WriteString("This is Postmoogle - a bot that bridges Email to Matrix.\n\n")
|
||||||
|
msg.WriteString(fmt.Sprintf(
|
||||||
|
"To get started, assign an email address to this room by sending a `%s %s SOME_INBOX` command.\n",
|
||||||
|
b.prefix,
|
||||||
|
optionMailbox,
|
||||||
|
))
|
||||||
|
msg.WriteString(fmt.Sprintf(
|
||||||
|
"You will then be able to send emails to `SOME_INBOX@%s` and have them appear in this room.",
|
||||||
|
b.domain,
|
||||||
|
))
|
||||||
|
|
||||||
|
b.Notice(ctx, roomID, msg.String())
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
||||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("sendHelp"))
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("sendHelp"))
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool {
|
|||||||
func (s settings) Get(key string) string {
|
func (s settings) Get(key string) string {
|
||||||
value := s[strings.ToLower(strings.TrimSpace(key))]
|
value := s[strings.ToLower(strings.TrimSpace(key))]
|
||||||
|
|
||||||
sanitizer, exists := sanitizers[key]
|
sanitizer, ok := sanitizers[key]
|
||||||
if exists {
|
if ok {
|
||||||
return sanitizer(value)
|
return sanitizer(value)
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
|
|||||||
33
bot/sync.go
33
bot/sync.go
@@ -9,6 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) initSync() {
|
func (b *Bot) initSync() {
|
||||||
|
b.lp.OnEventType(
|
||||||
|
event.StateMember,
|
||||||
|
func(_ mautrix.EventSource, evt *event.Event) {
|
||||||
|
go b.onMembership(evt)
|
||||||
|
},
|
||||||
|
)
|
||||||
b.lp.OnEventType(
|
b.lp.OnEventType(
|
||||||
event.EventMessage,
|
event.EventMessage,
|
||||||
func(_ mautrix.EventSource, evt *event.Event) {
|
func(_ mautrix.EventSource, evt *event.Event) {
|
||||||
@@ -21,6 +27,17 @@ func (b *Bot) initSync() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) onMembership(evt *event.Event) {
|
||||||
|
hub := sentry.CurrentHub().Clone()
|
||||||
|
|
||||||
|
if evt.Content.AsMember().Membership == event.MembershipJoin && evt.Sender == b.lp.GetClient().UserID {
|
||||||
|
b.onBotJoin(evt, hub)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Potentially handle other membership events in the future
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) onMessage(evt *event.Event) {
|
func (b *Bot) onMessage(evt *event.Event) {
|
||||||
// ignore own messages
|
// ignore own messages
|
||||||
if evt.Sender == b.lp.GetClient().UserID {
|
if evt.Sender == b.lp.GetClient().UserID {
|
||||||
@@ -70,3 +87,19 @@ func (b *Bot) onEncryptedMessage(evt *event.Event) {
|
|||||||
|
|
||||||
b.handle(span.Context(), decrypted)
|
b.handle(span.Context(), decrypted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// onBotJoin handles the "bot joined the room" event
|
||||||
|
func (b *Bot) onBotJoin(evt *event.Event, hub *sentry.Hub) {
|
||||||
|
// Workaround for membership=join events which are delivered to us twice,
|
||||||
|
// as described in this bug report: https://github.com/matrix-org/synapse/issues/9768
|
||||||
|
_, ok := b.handledJoinEvents.LoadOrStore(evt.ID, true)
|
||||||
|
if ok {
|
||||||
|
b.log.Info("Suppressing already handled event %s", evt.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := sentry.SetHubOnContext(context.Background(), hub)
|
||||||
|
|
||||||
|
b.sendIntroduction(ctx, evt.RoomID)
|
||||||
|
b.sendHelp(ctx, evt.RoomID)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user