Tryng to use @channel, @here and @user with python-slackclient

Viewed 811

I've tried to send a message with @channel, @here and @user, but they are sent as text.

enter image description here

This is the code....

import os
from slack import WebClient
from slack.errors import SlackApiError

client = WebClient(token='xoxb-**************************')

try:
    response = client.chat_postMessage(
        channel='#prueba',
        text="@channel Hello world!",
        as_user=False)
    assert response["message"]["text"] == "Hello world!"
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']}")

I've read both PythonSlackclient Docs and the API Docs, but I don't find an option to broadcast my messages or how to mention someone.

1 Answers

You need to change your code to

response = client.chat_postMessage(
    channel='#prueba',
    text="<!channel> Hello world!",
    as_user=False)
assert response["message"]["text"] == "@channel Hello world!"

Slack API has one of the best documentations, to get started with linking users read this

Related