Skip to content
Swizec Teller - a geek with a hatswizec.com

Slack lied to me

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 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.

bqiWYiL

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

3yaqj8b

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[‘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: ‘#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.

xWO1Rwg

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

Another way to get around the bots-cant-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:

3FZOz5A

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. ?

Did you enjoy this article?

Published on August 25th, 2016 in Back End, Ruby, Ruby (programming language), slack, Technical, user documentation

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️