Skip to content

Commit

Permalink
Take care not to double up newlines when adding triple-backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
parsley42 committed Aug 17, 2023
1 parent 35558d6 commit f45865d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v2.11.1 - Improved Slack "raw" Message Splitting
On occasion the AI plugin could generate very long Slack messages containing one or more code blocks. The old message splitting algorithm just blindly split up the message and sent chunks, breaking on newlines when possible. The new algorithm now keeps track of code blocks, and closes and re-opens them between messages.

# v2.11.0 - Hidden Commands and Support for Slack Slash Commands
When Gopherbot's Slack connector migrated from RTM to socketmode events, I added bare minimum support for "slash commands" - if you set up your robot with e.g. "/clu" for a slash command, you could type "/clu ping" and it would dutifully respond "@parsley PONG" in the channel - but nobody would have seen the original ping command, since slash commands are hidden. Version 2.11.0 adds a more useful functionality for slash commands called "hidden commands", meaning the user can message the robot and receive a reply that is hidden from other users. The interfaces are defined in a generic way so that future connectors could also support this functionality in a connector-specific way, but for now I will only document how it's implemented in Slack.

Expand Down
22 changes: 19 additions & 3 deletions connectors/slack/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ func normalizeBackticks(input string) string {
func optAddBlockDelimeters(inside_block bool, chunk string) (bool, string) {
delimeters := strings.Count(chunk, "```")
if inside_block {
chunk = "```\n" + chunk
if strings.HasPrefix(chunk, "\n") {
chunk = "```" + chunk
} else {
chunk = "```\n" + chunk
}
}
if delimeters%2 == 1 {
inside_block = !inside_block
}
if inside_block {
chunk = chunk + "\n```"
if strings.HasSuffix(chunk, "\n") {
chunk = chunk + "```"
} else {
chunk = chunk + "\n```"
}
}
return inside_block, chunk
}
Expand Down Expand Up @@ -148,7 +156,15 @@ func (s *slackConnector) slackifyMessage(prefix, msg string, f robot.MessageForm
msg = prefix + msg
}
if f == robot.Fixed {
msg = "```\n" + msg + "\n```"
f_prefix := "```\n"
if strings.HasPrefix(msg, "\n") {
f_prefix = "```"
}
f_suffix := "\n```"
if strings.HasSuffix(msg, "\n") {
f_suffix = "```"
}
msg = f_prefix + msg + f_suffix
}

msgLen := len(msg)
Expand Down

0 comments on commit f45865d

Please sign in to comment.