Python slackclient는 Slack Web API와 Real Time Messaging(RTM) API의 interface를 제공하는 개발자 도구입니다. python 3.6 version 이상에서 동작합니다.
Slack Python에 대한 설명문서는 오른쪽 주소에서 확인하실 수 있습니다. https://slack.dev/python-slackclient/
여러분이 당신의 팀을 위한 custom app을 만들거나 다른 third party service를 당신의 Slack workflows에 통합하려고 하면, Python 기반의 Slack Developer Kit은 python의 유연성을 활용해서 당신의 프로젝트가 가능한 빠르게 진행될수 있도록 도와줄겁니다!!
Python slackclient는 다음 것들과 상호작용 할 수 있게 해줍니다.
- Slack web api의 사용 API Docs site
- 우리의 RTM과의 상호작용RTM API
만약에 여러분이 Event API를 사용하길 원하신다면, Slack Events API adapter for Python를 확인해보세요.
Tokens과 Authentication(인증)에 관한 세부사항은 Auth Guide 에서 찾을 수 있습니다.
이 library는 Python 3.6 이상의 버전이 필요합니다. 만약에 여러분이 Python 2를 사용중이라면, SlackClient - v1.x을 사용해주세요. 만약 Python Version 확인법을 모르시면 아래 방법을 통해서 확인 가능합니다!
Note: You may need to use
python3before your commands to ensure you use the correct Python path. e.g.python3 --version
python --version
-- or --
python3 --version우리는 PyPI를 사용해서 Slack Developer Kit을 설치하는걸 추천드립니다.
$ pip3 install slackclient우리는 이 [tutorial[(/tutorial)을 10분보다 짧은 시간안에 기본적인 Slack app을 만들어 볼수 있도록 만들었습니다. 이걸 진행하는데 기본적인 프로그래밍 지식이 필요하고, 물론 Python에 대한 기본지식도 필요합니다. 이 tutorial은 Slack의 Web과 RTM API와 연결하는데 집중되어 있습니다. 이 tutorial을 통해서 어떻게 SDK를 활용해야 하는지 알아보세요!
Read the tutorial to get started!
Slack provide a Web API that gives you the ability to build applications that interact with Slack in a variety of ways. This Development Kit is a module based wrapper that makes interaction with that API easier. We have a basic example here with some of the more common uses but a full list of the available methods are available here. More detailed examples can be found in our Basic Usage guide
One of the most common use-cases is sending a message to Slack. If you want to send a message as your app, or as a user, this method can do both. In our examples, we specify the channel name, however it is recommended to use the channel_id where possible.
import os
from slack import WebClient
from slack.errors import SlackApiError
client = WebClient(token=os.environ['SLACK_API_TOKEN'])
try:
response = client.chat_postMessage(
channel='#random',
text="Hello world!")
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']}")Here we also ensure that the response back from Slack is a successful one and that the message is the one we sent by using the assert statement.
We've changed the process for uploading files to Slack to be much easier and straight forward. You can now just include a path to the file directly in the API call and upload it that way. You can find the details on this api call here
import os
from slack import WebClient
from slack.errors import SlackApiError
client = WebClient(token=os.environ['SLACK_API_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']}")The Real Time Messaging (RTM) API is a WebSocket-based API that allows you to receive events from Slack in real time and send messages as users.
If you prefer events to be pushed to you instead, we recommend using the HTTP-based Events API instead. Most event types supported by the RTM API are also available in the Events API. You can check out our Python Slack Events Adaptor if you want to use this API instead.
An RTMClient allows apps to communicate with the Slack Platform's RTM API.
The event-driven architecture of this client allows you to simply link callbacks to their corresponding events. When an event occurs this client executes your callback while passing along any information it receives. We also give you the ability to call our web client from inside your callbacks.
In our example below, we watch for a message event that contains "Hello" and if its received, we call the say_hello() function. We then issue a call to the web client to post back to the channel saying "Hi" to the user.
import os
from slack import RTMClient
from slack.errors import SlackApiError
@RTMClient.run_on(event='message')
def say_hello(**payload):
data = payload['data']
web_client = payload['web_client']
rtm_client = payload['rtm_client']
if 'text' in data and 'Hello' in data.get('text', []):
channel_id = data['channel']
thread_ts = data['ts']
user = data['user']
try:
response = web_client.chat_postMessage(
channel=channel_id,
text=f"Hi <@{user}>!",
thread_ts=thread_ts
)
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']}")
rtm_client = RTMClient(token=os.environ["SLACK_API_TOKEN"])
rtm_client.start()slackclient v2 and higher uses aiohttp and asyncio to enable async functionality.
Normal usage of the library does not run it in async, hence a kwarg of run_async=True is needed.
When in async mode its important to remember to await or run/run_until_complete the call.
import asyncio
import os
from slack import WebClient
from slack.errors import SlackApiError
client = WebClient(
token=os.environ['SLACK_API_TOKEN'],
run_async=True
)
future = client.chat_postMessage(
channel='#random',
text="Hello world!"
)
loop = asyncio.get_event_loop()
try:
# run_until_complete returns the Future's result, or raise its exception.
response = loop.run_until_complete(future)
assert response["message"]["text"] == "Hello world!"
except SlackApiError as e:
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']}")
finally:
loop.close()If you are using a framework invoking the asyncio event loop like : sanic/jupyter notebook/etc.
import os
from slack import WebClient
from slack.errors import SlackApiError
client = WebClient(
token=os.environ['SLACK_API_TOKEN'],
run_async=True # turn async mode on
)
# Define this as an async function
async def send_to_slack(channel, text):
try:
# Don't forget to have await as the client returns asyncio.Future
response = await client.chat_postMessage(
channel=channel,
text=text
)
assert response["message"]["text"] == text
except SlackApiError as e:
assert e.response["ok"] is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
raise e
# https://sanicframework.org/
from sanic import Sanic
from sanic.response import json
app = Sanic()
# e.g., http://localhost:3000/?text=foo&text=bar
@app.route('/')
async def test(request):
text = 'Hello World!'
if 'text' in request.args:
text = "\t".join(request.args['text'])
try:
await send_to_slack(channel="#random", text=text)
return json({'message': 'Done!'})
except SlackApiError as e:
return json({'message': f"Failed due to {e.response['error']}"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000)The Python slackclient v2 now uses AIOHttp under the hood.
Looking for a performance boost? Installing the optional dependencies (aiodns) may help speed up DNS resolving by the client. We've included it as an extra called "optional":
$ pip3 install slackclient[optional]Interested in SSL or Proxy support? Simply use their built-in SSL and Proxy arguments. You can pass these options directly into both the RTM and the Web client.
import os
from slack import WebClient
from ssl import SSLContext
sslcert = SSLContext()
# pip3 install proxy.py
# proxy --port 9000 --log-level d
proxyinfo = "http://localhost:9000"
client = WebClient(
token=os.environ['SLACK_API_TOKEN'],
ssl=sslcert,
proxy=proxyinfo
)
response = client.chat_postMessage(
channel="#random",
text="Hello World!")
print(response)We will always follow the standard process in AIOHttp for those proxy and SSL settings so for more information, check out their documentation page linked here.
If you're migrating from v1.x of slackclient to v2.x, Please follow our migration guide to ensure your app continues working after updating.
Check out the Migration Guide here!
If you get stuck, we’re here to help. The following are the best ways to get assistance working through your issue:
Use our Github Issue Tracker for reporting bugs or requesting features. Visit the Slack Community for getting help using Slack Developer Kit for Python or just generally bond with your fellow Slack developers.