slack_message

A friendly DSL for building rich Slack messages


Project maintained by jmmastey Hosted on GitHub Pages — Theme by mattgraham

Updating a Previous Message

After you’ve posted a message, you may want to edit it later. Interactive bots, for instance, may want to repeatedly update a message.

Posting will always return an object representing your posted message.

message = SlackMessage.post_to('#general') do
  text "Getting ready..."
end

Then, you can use that response object to go back and rewrite the message a little or a lot.

SlackMessage.update(message) do
  text "Done!"
end

The new message contents will be built and updated via the API. To give an example, you could alert slack to a job status by updating your original message.

class SomeWorker < ApplicationWorker
  def perform
    post_started_status

    # ... perform work here

    post_finished_status
  end

  private

  def post_started_status
    @message = SlackMessage.post_as(:job_worker) do
      text "Beginning upload."
    end
  end

  def post_finished_status
    SlackMessage.update(@message) do
      text "Finished upload! @here come and get it."
      link_button "See Results", uploaded_data_url
    end
  end
end

Storing Response Objects for Later

Since updates are likely to occur long after you post the original message, you may want to persist a reference to the message until you need to update it later. As one option, you could serialize the response object for later.

# initially
message = SlackMessage.post_to('#general') do
  text "Starting..."
end
redis_connection.set(self.message_cache_key, Marshal.dump(message))


# later
message = Marshal.load(redis_connection.get(self.message_cache_key))
SlackMessage.update(message) do
  text "Finished!"
end

Updating Scheduled Messages

Sadly, there’s currently no way to edit a scheduled message. You’ll receive an error if you attempt to call update on a scheduled message.

See the API documentation for chat.update for more information on updating messages.


Next: Deleting Messages