A Slack bot with Python’s 3.5 asyncio

Testing the token

  • Any async def is called a coroutine. It’s a function that can be stopped and resumed from any await it contains.
  • async with defines a context that can resume its execution during the enter and the exit phases.
  • And the return await response.json() will pause this piece of code and resume it when the JSON version of the response body is ready.

Connecting the bot

  1. it authenticates itself to the RTM API using the normal API (on HTTP);
  2. it connects to the WebSocket and waits for new messages before printing them on the console as they come.

Pushing messages further

Using queues

Sending messages back to Slack

  1. you may simply send messages over the WebSocket connection whenever you feel like it using ws.send_str().
  2. for bigger and more complex messages, like files.upload; you’ll have to use the Web API via api_call.

--

--

Senior developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store