Slack Bot reading and writing from a channel

From the last post we have and idea on what we are going to build let start out. To start with i just want to create a very simple app that can read from a channel and post to a channel. These are the basic functionality of a karma bot and should be the first thing we test.

Creating a Slack app

First before we can code we need to create a slack app. To create a slack app we first go to https://api.slack.com/apps and click on Create New App. Give the app a name and add it to a slack workspace. (You can create a free slack workspace to play around in).

Scope

Now that we have an app we need to add Scopes. Scopes are the permission our app is going to have. There are tone of Scope that are bot can do, but we specifically just want to start with Reading and Writing to a channel, so the scope we’ll need are

  • channels:history — this returns the history of a channel
  • channels:read — get you basic information about channels
  • chat:write — lets you bot write in a channel

We only need these token for Bot, and not user.

Code

Now we have enough to start coding so let with connecting to slack

First we import slack (if you don’t have slack pip install slackclient. We next set the token to the token we got in the from Slack. YES YES YES i know in a real app hard coding your token in a file is super super super bad. But i’m just trying out methods, and will move it to a better location later

import slack
import time
import logging
logging.basicConfig()
token = "YOUR KEY"
slack_client = slack.WebClient(token=token)

Now after playing with the methods for a while we have some annoying thing with slack. To read a channel you need to know the channel ID. To write to a channel you need to know the channel name with a #. When you get a list of a channel you get it with out a # sign. So that fun.

So first make sure you have invite you bot to the channel in slack or this will not work (We’ll add automatically adding out bot to a channel on our list of things to do). So we are going to hard code the name of our channel for now ‘test-channel’ and we are going to want to find it ID. What this bit of code does is ask Slack for all the channel, and if a channel name is ‘test-channel we get the ID of the channel

id = None
mychannel = 'test-channel'
list = slack_client.channels_list()
for each in list.data.get('channels'):
    if each.get('name') == mychannel:
        id = each.get('id')

Next let see if we can write and read from a channel. What i have is a loop that runs every 2 seconds. It first will post a message in the channel. Then it will get the channel history and print it.

while True:
    response = slack_client.chat_postMessage(
        channel='#' + mychannel,
        text="This is a message")
    if id:
        l = slack_client.channels_history(channel=id)
        print(str(l))
        print('----------')
    time.sleep(2)

If we put this all together and run it, it works. A message will show up in slack, and when we print the history we get the message W00t…

(venv) carchi karmabot $ python3 karma.py
{'ok': True, 'messages': [{'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633623.005000', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631533.000400', 'user': 'US40GFF0S', 'text': '<@US40GFF0S> has joined the channel', 'inviter': 'US9KQ4BBJ'}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631523.000200', 'user': 'US9KQ4BBJ', 'text': '<@US9KQ4BBJ> has joined the channel'}], 'has_more': False, 'channel_actions_ts': None, 'channel_actions_count': 0}
----------
{'ok': True, 'messages': [{'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633625.005100', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633623.005000', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631533.000400', 'user': 'US40GFF0S', 'text': '<@US40GFF0S> has joined the channel', 'inviter': 'US9KQ4BBJ'}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631523.000200', 'user': 'US9KQ4BBJ', 'text': '<@US9KQ4BBJ> has joined the channel'}], 'has_more': False, 'channel_actions_ts': None, 'channel_actions_count': 0}
----------
{'ok': True, 'messages': [{'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633628.005200', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633625.005100', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'bot_id': 'BS6H4FLQH', 'type': 'message', 'text': 'This is a message', 'user': 'US40GFF0S', 'ts': '1578633623.005000', 'team': 'TRY5Y008K', 'bot_profile': {'id': 'BS6H4FLQH', 'deleted': False, 'name': 'Karma Bot', 'updated': 1578630647, 'app_id': 'ASJJ8SP9D', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'TRY5Y008K'}}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631533.000400', 'user': 'US40GFF0S', 'text': '<@US40GFF0S> has joined the channel', 'inviter': 'US9KQ4BBJ'}, {'type': 'message', 'subtype': 'channel_join', 'ts': '1578631523.000200', 'user': 'US9KQ4BBJ', 'text': '<@US9KQ4BBJ> has joined the channel'}], 'has_more': False, 'channel_actions_ts': None, 'channel_actions_count': 0}
----------

There are some issues we will need to fix

  • We had to manually invite the bot to the channel
  • When we get the history, we get the entire history of the channel. We don’t want that, we only want the history from the last time we got the history. So we’ll need to figure out how to do that later

One thought on “Slack Bot reading and writing from a channel

Add yours

Leave a comment

Blog at WordPress.com.

Up ↑