jason m
jason m

Reputation: 6835

Sending my first message on the slack API

I registered my first app, and it looks like this: enter image description here

All of the fields are populated below the screen shot.

Now, I have some basic python code using the examples found on their repo.

I create the following test script:

import traceback
app_id = 'FAKE.VALUE'
client_id = 'FAKE.VALUE'
client_secret = 'FAKE.VALUE'
signin_secret = 'FAKE.VALUE'
verification_token = 'FAKE.VALUE'

items = locals()

import os
import slack

items = locals().copy()

for k in items:

    if '__' not in k:
        val = items[k]

        try:
            client = slack.WebClient(token=val)

            response = client.chat_postMessage(
                channel='CE476K9HT',
                text='Hello-----' + str(val))

            print(response)
        except:
            print(k)
            traceback.print_exc()
            print('-'*50)

But all of the responses I get say:

The server responses with: {'ok':False,'error':'invalid_auth'}

For some reason, is it necessary to use path variables?

It is unclear to me what type of auth is required here.


After doing what Erik suggested,

enter image description here

I have a xoxp code and registered the redirect url to http://localhost.

and added the following scopes: enter image description here

and updated my code to look like:

oauth_token ='xoxp-*****************'

import os
import slack

items = locals().copy()

client = slack.WebClient(token=oauth_token)

response = client.chat_postMessage(
    channel='my_channel_id',
    text='Hello-----')

I got my channel ID from the url:

https://app.slack.com/client/FOO/my_channel_id

When I run my code, I get the following back:

Traceback (most recent call last):
  File "/home/usr/git/slack_messaging/slack_messaging.py", line 20, in <module>
    text='Hello-----')
  File "/home/usr/git/python-slackclient/slack/web/client.py", line 382, in chat_postMessage
    return self.api_call("chat.postMessage", json=kwargs)
  File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 172, in api_call
    return self._event_loop.run_until_complete(future)
  File "/home/usr/anaconda2/envs/beer/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
    return future.result()
  File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 241, in _send
    return SlackResponse(**{**data, **res}).validate()
  File "/home/usr/git/python-slackclient/slack/web/slack_response.py", line 176, in validate
    raise e.SlackApiError(message=msg, response=self)
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'missing_scope', 'needed': 'chat:write:user', 'provided': 'admin,identify'}

Process finished with exit code 1

Upvotes: 4

Views: 7315

Answers (1)

Erik Kalkoken
Erik Kalkoken

Reputation: 32698

You need two things to make your script work.

1. OAuth token

You need a valid OAuth token and provide it when initializing the Slack Client:

client = slack.WebClient(token="xoxb-xxx")

To get a token you need to install your Slack app to a workspace. You can do that on the app management page under "Install App". Your OAuth token will also be displayed on that page once you installed it.

2. Permissions

Your Oauth Token / Slack app needs to have the permission to post messages. On the app management pages go to "OAuth & permission" and add the needed permission to your app:. e.g. chat:write:user for user tokens.

Note that you need to reinstall your app every time to add a permission.

Upvotes: 3

Related Questions