Skip to content

Commit

Permalink
Add ephemeral message support
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Sep 26, 2021
1 parent 4d65667 commit eca4d84
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions bot/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type conn interface {
Connect(ctx context.Context) (<-chan event, error)
Send(channel *channelID, message string) error
SendWithID(channel *channelID, message string) (string, error)
SendEphemeral(channel *channelID, userID, message string) error
SendDM(userID string, message string) error
UploadFile(channel *channelID, message string, filename string, filetype string, file io.Reader) error
Update(channel *channelID, id string, message string) error
Expand Down
4 changes: 4 additions & 0 deletions bot/conn_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (c *discordConn) SendWithID(channel *channelID, message string) (string, er
return msg.ID, nil
}

func (c *discordConn) SendEphemeral(_ *channelID, userID, message string) error {
return c.SendDM(userID, message) // Discord does not support ephemeral messages outside of slash commands
}

func (c *discordConn) SendDM(userID string, message string) error {
ch, err := c.session.UserChannelCreate(userID)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions bot/conn_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (c *memConn) SendWithID(channel *channelID, message string) (string, error)
return strconv.Itoa(c.currentID), nil
}

func (c *memConn) SendEphemeral(_ *channelID, userID, message string) error {
return c.SendDM(userID, message)
}

func (c *memConn) SendDM(userID string, message string) error {
c.mu.Lock()
defer c.mu.Unlock()
Expand Down
16 changes: 16 additions & 0 deletions bot/conn_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ func (c *slackConn) SendWithID(channel *channelID, message string) (string, erro
}
}

func (c *slackConn) SendEphemeral(channel *channelID, userID, message string) error {
options := c.postOptions(channel, slack.MsgOptionText(message, false))
for {
_, err := c.rtm.PostEphemeral(channel.Channel, userID, options...)
if err == nil {
return nil
}
if e, ok := err.(*slack.RateLimitedError); ok {
log.Printf("error: %s; sleeping before re-sending", err.Error())
time.Sleep(e.RetryAfter + additionalRateLimitDuration)
continue
}
return err
}
}

func (c *slackConn) SendDM(userID string, message string) error {
ch, _, _, err := c.rtm.OpenConversation(&slack.OpenConversationParameters{
ReturnIM: true,
Expand Down
10 changes: 5 additions & 5 deletions bot/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
sessionStartedMessage = "🚀 REPL session started, %s. Type `!help` to see a list of available commands, or `!exit` to forcefully " +
"exit the REPL."
splitModeThreadMessage = "Use this thread to enter your commands. Your output will appear in the main channel."
onlyMeModeMessage = "Only you as the session owner can send commands. Use the `!allow` command to let other users control the session."
onlyMeModeMessage = "*Only you as the session owner* can send commands. Use the `!allow` command to let other users control the session."
everyoneModeMessage = "*Everyone in this channel* can send commands. Use the `!deny` command specifically revoke access from users."
sessionExitedMessage = "👋 REPL exited. See you later!"
sessionExitedWithRecordingMessage = "👋 REPL exited. You can find a recording of the session in the file below."
Expand Down Expand Up @@ -257,9 +257,6 @@ func (s *session) Run() error {
if err != nil {
return err
}
if err := s.maybeSendStartShareMessage(); err != nil {
return err
}
command := s.createCommand()
if err := s.tmux.Start(env, command...); err != nil {
log.Printf("[%s] Failed to start tmux: %s", s.conf.id, err.Error())
Expand All @@ -268,6 +265,9 @@ func (s *session) Run() error {
if err := s.conn.Send(s.conf.control, s.sessionStartedMessage()); err != nil {
return err
}
if err := s.maybeSendStartShareMessage(); err != nil {
return err
}
s.g.Go(s.userInputLoop)
s.g.Go(s.commandOutputLoop)
s.g.Go(s.activityMonitor)
Expand Down Expand Up @@ -701,7 +701,7 @@ func (s *session) maybeSendStartShareMessage() error {
return err
}
message := fmt.Sprintf(shareStartCommandMessage, port, s.conf.share.user, host)
if err := s.conn.SendDM(s.conf.user, message); err != nil {
if err := s.conn.SendEphemeral(s.conf.control, s.conf.user, message); err != nil {
return err
}
return nil
Expand Down

0 comments on commit eca4d84

Please sign in to comment.