Bots are definitely not a new thing
Anybody who's been around long enough to have used IRC is very familiar with BOTS like egg drop that would manage IRC channels for you essentially BOTS or anything with a conversational interface there are conversational interface to the execution of code usually they run sort of autonomously.
In the 90s, there is interaction with BOTS or through text base multiplayer games muds and mousse back, had BOTS for enemies and BOTS for like non-player characters you can talk to things like that.
In the early 2000s, there was a startup called smarter child. well smarter child was their first product it was an aim bot that you could talk to and at the peak of its popularity it had had conversations with something like 30 million people which considering the internet in the early 2000s is a pretty big number.
you may have used chat systems like campfire or hip chat and now slack.
This talk is specifically about slack BOTS
We have our little ancient aliens dude, our ancient aliens dude was actually created by a bot at Vox media we use slack pretty extensively and we have a bot called CF bot which we started using camp fire it was built for camp fire and we've ported it through the years but to make it really easy to use functionality that otherwise you would have to go into other places to execute so one of the things that CF bot can do is it can caption images we use gif messages to communicate feelings to each other a lot at slack so this is particularly helpful so we can do things like this and this is how I made the bots gif.
So, select BOTS they can sit in channels and you can talk to them and ask them to do stuff for you.
I hope that you will have as much of an understanding of how you build
BOTS for slack with python as you would need.
Slack i/o methods
Slack supports incoming webhooks.
Incoming webhooks are all about information flowing into slack.
when you create an incoming web book integration in slack you go to a page and you say I want to create like an incoming webhook for Jenkins so whenever Jenkins wants to do something it's going to use this webhook URL this unique sort of a password URL because it's very long and not easy to guess and it's going to let Jenkins post stuff into slack without any other name or any other authentication.
what Jenkins or whatever external system you'll have will get this URL it will send a JSON payload with a message optionally an icon for its user optionally a nickname for its user and it posts that to slack and it appears in your channel.
outgoing web hooks are the other side of Incoming webhooks.
you can say that every time something is posted in a channel or every time a trigger word like a sentence starts with a blank post to a URL.
In this case you're essentially building a web application that responds to posts with messages they get pushed back into slack while they end up back in the slack.
A post comes in with message text and who said it and what channel it was in and you reply with some JSON that's like oh here this is the message that you should display in that channel.
This only works for public chats you can't have a private chat and have an outgoing web hook work.
There are also slash commands this is when you do like slash weather zip code this is a HTTP post as well but only the user who types the slash gets the response.
This is for things where you want to look up something but you're not necessarily wanting to share it with anybody else.
Web API (OAUTH, HTTP RPC)
There is a Web API that is HTTP RPC style.
Slack also supports IRC and XMPP integrations.
The Slack real time messaging API
Slack has a real-time messaging API because slack is super modern.
WebSockets / Used by all clients
It uses WebSockets and it's used by all the slack clients.
Whatever you log into slack through your iPhone or through the desktop application all of these things connect to the web socket API so your BOTS connect to the same endpoints and use the same communication methods that clients do.
Pushed all messages in all channels
you are pushed met all messages in all channels that you were in which is why you're select client can show you
like hey these messages letter 10 unread messages in here your slack client is keeping track of State.
You're pushed all the messages and all the channels are in are also pushed things like this user became active this user started typing stuff like that well we'll look at that in a second.
commonly used with bot’s users
Bot’s users as a special type of slack user they can't join channels by themselves they have to be invited.
There are some other restrictions they're always active unless you specifically say I'm Away.
Bot users don't log in with user names and passwords they have API tokens.
you can customize their name give them an icon give them a first and last name.
Sending a message
post function art parts of our arguments and config bars of course the config and then it creates an instance of the slack client you feed the slack client your token once you have your sock client back you call our TM connect which opens a WebSocket connection to slack and once that is done you can post things.
I said the bot users can't join channels they have to be invited in channels so the first thing we do is some error checking to make sure that the bot user (the user we are logging in on) is actually in the channel.
We find the channel that you're trying to post in and we err out if the channel wasn't found.
We make (loop for user) does the user actually exist and if the user ID is not in Channel members then we can't post into that channel so a warning message.
converse has a debug option on it which prints out every message that you get back from slack.
When you log into slack you get a type hello message it basically tells you that yes the API responded to you and you get other messages like presents change, user became active if I started typing you would see user typing.
When slack throws stuff around it throws around unique IDs for channels and users and slack accounts it doesn't use the names it actually uses unique internal.
Three features that you might expect from a bot:
- Some simple kind of respond to me, I type some text give me some text back.
- Some that's kind of unique to Who I am as a user.
- Some that actually seems to have executed some code somewhere.
pretty easy to create a bot that listens inside of slack and responds to messages that you want.
Framework & Wrappers
There's the slack clients like client is a pretty thin API wrapper they've also built Python-rtmbot but I on rpmbot is a lot like converse except it has support for plugins and it supports scheduled messages so you can book people in slack every 10 minutes if you want.
It is a good place to start if you want to build something from scratch.
If you're just looking to set up a slack bot and you would like a lot of functionality quickly slack has a list of Python projects that connect to slack up there are a big list of them.
- err/ popular chatpot W/ Slack Support
err is a pretty popular chat bot it seems it has slack support it also is HipChat support and campfire support and IRC support and it has plugins and if you want a lot of features out of the box it's probably a good place to start it's a bigger project.
- Butterfield – Uses asyncio
There's something called Butterfield that uses a asyncio.
One interesting thing is that if you don't do a synchronous stuff your chat BOTS can block if you're like calling out to some script that generates like funny gif images so some bots will use something like celery or some other background processor to be able to respond quickly to things that they can respond quickly for but let other more long-running things sort of percolate in the background and then push the response back into the Select channel when they're done with them.
- slacker / Popular wrapper
There's another API wrapper called slacker that's very active.