Viktor.w
Viktor.w

Reputation: 2297

Using slack API: "Module slack has no attribute WebClient "

I try to use the slack API to send a message to a workspace, I found this piece of code on their docs but I have an issue with the module slack. This is the code I used:

import os
import slack

client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])

response = client.chat_postMessage(
    channel='#viktor',
    text="Hello world!")
assert response["ok"]
assert response["message"]["text"] == "Hello world!"

I have put my app token but it does not recognize WebClient... any idea?

Upvotes: 7

Views: 8667

Answers (3)

Lokesh
Lokesh

Reputation: 3112

I use python-slack-sdk, it works great. See https://github.com/slackapi/python-slack-sdk#uploading-files-to-slack

To install, run pip3 install slack_sdk

To upload a file

import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

client = WebClient(token=os.environ['SLACK_BOT_TOKEN'])

try:
    filepath="./tmp.txt"
    response = client.files_upload(channels='#random', file=filepath)
    assert response["file"]  # the uploaded file
except SlackApiError as e:
    # You will get a SlackApiError if "ok" is False
    assert e.response["ok"] is False
    assert e.response["error"]  # str like 'invalid_auth', 'channel_not_found'
    print(f"Got an error: {e.response['error']}")

Upvotes: 0

techkuz
techkuz

Reputation: 3956

Make sure you don't have any user files named slack slack_client.
A simple filename change to something more original solved my problem.

This was my setup:

  • MacOs HS
  • Python 3.7
  • latest (2.4) version of slackclient installed

Upvotes: 5

Dustin Sun
Dustin Sun

Reputation: 5532

pip install slack
pip install slackclient

Upvotes: 10

Related Questions