Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Slack Lied to Me

DZone's Guide to

Slack Lied to Me

When things don't go according to plan, the results can be devastating. See one man's quest as he attempts to program a helpful bot for his officemates.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

I built my first Slack bot today.

It has one job: to report a specific type of error, and to do so while impersonating the office dog. It might get more jobs in the future, but you have to start somewhere.

Adding a Slack bot to your Rails app happens in three steps:

  1. Set up a new custom integration.
  2. Add slack-ruby-client to your setup.
  3. Fiddle around until you reach the desired flexibility and feature set.

When you’re done with the first step, Slack promises that your bot’s messages will have a friendly avatar and username.


Instead, they don’t. Following the basic instructions, you get a grey bot image and default username.


And so it begins. Outdated documentation.

Basic API spec says that you should post messages with an as_user: true flag, which makes the bot use its default settings from Step 1.


#Setup stuff, goes in a global file somewhere. config/initializers/slack.rb for instance

<p>Slack.configure do |config|</p>

<p>config.token = ENV[&#8216;SLACK_API_TOKEN’]</p>

<p>end</p>

#Instantiate client

<p>client = Slack::Web::Client.new</p>

#Send message to #general channel

<p>client.chat_postMessage(channel: &#8216;#general’, text: ‘Hello World’, as_user: true)</p>


That code throws an error -> user not in channel. So you dig through the documentation and find channels_join. But that throws an error, too -> user is a bot.

Bots can’t join channels.

If you remove as_user: true, then you can post, but you post without an avatar and without a username. Ugh.

The easiest workaround is to explicitly add a username and an avatar to your post call. It makes half of Step 1 redundant, but your bot messages look great, and your team gets excited.


If only they knew @quentin will only ever post errors.

Another way to get around the bots-can't-join-channels problem is to mention @quentin, then invite him to the channel. I’m sure that will never get difficult or annoying to manage. No chance at all.

It’s probably best to stick to the workaround.

Giving a voice to the office dog is fun and all, but the useful stuff begins when you look into attachments. Slack lets you add rich attachments to your messages. Anything from semantically color-coded styling to buttons for common actions.

I’m keeping it simple for now: report error, add contextual logs. In theory, this will help us quickly decide which errors are worth looking into and which aren’t.

Adding attachments looks like this for me:

client.chat_postMessage(
channel: '#bot-test',
text: "this is *bold*",
attachments: [{
    color: 'warning',
    fallback: title,
    title: title,
    title_link: session_url,
    text: "*executed_at*\t*created_at*\t*user*\t*event*",
    ts: Time.now.to_f
}])


It creates a message like this:


See those asterisks? Those are not supposed to be there. Slack’s message formatting rules say that strings wrapped in asterisks should render bold anywhere they appear. Messages, attachments, everywhere, bold.

Instead, it works for the message itself but not for the attachment. Possible reasons range from a Slack bug, to a library bug, to something strange happening in how I call the library. I haven’t figured it out, and I’m probably going to decide it’s not worth my time.

But the office dog speaks now.

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
d3.js ,database management ,charting ,slack bots

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}