Over a million developers have joined DZone.

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

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

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>


#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:

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.

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

d3.js,database management,charting,slack bots

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}