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.
Join the DZone community and get the full member experience.Join For Free
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:
- set up a new custom integration .
- add slack-ruby-client to your setup.
- 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
flag, which makes the bot use its default settings from step 1.
that code throws an error
-> user not in channel
. so you dig through the documentation and find
. but that throws an error, too
-> user is a bot
bots can’t join channels.
if you remove
, 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:
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.
Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.