6

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?

Viktor.w
  • 1,787
  • 2
  • 20
  • 46

3 Answers3

8
pip install slack
pip install slackclient
Dustin Sun
  • 5,292
  • 9
  • 49
  • 87
5

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
techkuz
  • 3,608
  • 5
  • 34
  • 62
0

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']}")
Lokesh
  • 2,842
  • 7
  • 32
  • 47