Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 814
ThorPa Avatar asked Sep 15 '25 07:09

ThorPa


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

like image 173
Caleb Njiiri Avatar answered Sep 16 '25 22:09

Caleb Njiiri