python-slackclient DocumentationRelease 1.0.1
Ryan Huber, Jeff Ammons
November 02, 2016
Contents
1 Classes & Methods 3
2 Overview 7
3 Installation 93.1 Automatic w/ PyPI (virtualenv is recommended.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Usage 114.1 Basic API methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.2 Real Time Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.3 Objects & Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5 Contributing 135.1 Submitting a Pull Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135.2 Building Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Python Module Index 15
i
ii
python-slackclient Documentation, Release 1.0.1
Contents:
Contents 1
python-slackclient Documentation, Release 1.0.1
2 Contents
CHAPTER 1
Classes & Methods
class slackclient.SlackClient(token)The SlackClient makes API Calls to the Slack Web API as well as managing connections to the Real-timeMessaging API via websocket
It also manages some of the Client state for Channels that the associated token (User or Bot) is associated with.
For more information, check out the Slack API Docs
Init:
Args token (str): Your Slack Authentication token. You can find or generate a test token hereNote: Be careful with your token
api_call(method, **kwargs)Call the Slack Web API as documented here: https://api.slack.com/web
Args method (str): The API Method to call. See the full list here
Kwargs (optional) kwargs: any arguments passed here will be bundled and sent to the api re-quester as post_data and will be passed along to the API.
Example:
sc.server.api_call("channels.setPurpose",channel="CABC12345",purpose="Writing some code!"
)
Returns str – returns the text of the HTTP response.
Examples:
u'{"ok":true,"purpose":"Testing bots"}'oru'{"ok":false,"error":"channel_not_found"}'
See here for more information on responses: https://api.slack.com/web
process_changes(data)Internal method which processes RTM events and modifies the local data store accordingly.
Stores new channels when joining a group (Multi-party DM), IM (DM) or channel.
Stores user data on a team join event.
3
python-slackclient Documentation, Release 1.0.1
rtm_connect()Connects to the RTM Websocket
Args None
Returns False on exceptions
rtm_read()Reads from the RTM Websocket stream then calls self.process_changes(item) for each line in the returneddata.
Multiple events may be returned, always returns a list [], which is empty if there are no incoming messages.
Args None
Returns data (json) - The server response. For example:
[{u'presence': u'active', u'type': u'presence_change', u'user': u'UABC1234'}]
Raises SlackNotConnected if self.server is not defined.
rtm_send_message(channel, message)Sends a message to a given channel.
Args channel (str) - the string identifier for a channel or channel name (e.g. ‘C1234ABC’, ‘bot-test’ or ‘#bot-test’) message (message) - the string you’d like to send to the channel
Returns None
class slackclient._server.Server(token, connect=True)The Server object owns the websocket connection and all attached channel information.
api_call(method, **kwargs)Call the Slack Web API as documented here: https://api.slack.com/web
Args method (str): The API Method to call. See here for a list: https://api.slack.com/methods
Kwargs (optional) kwargs: any arguments passed here will be bundled and sent to the api re-quester as post_data
and will be passed along to the API.
Example:
sc.server.api_call("channels.setPurpose",channel="CABC12345",purpose="Writing some code!"
)
Returns: str – returns the text of the HTTP response.
Examples:
u'{"ok":true,"purpose":"Testing bots"}'oru'{"ok":false,"error":"channel_not_found"}'
See here for more information on responses: https://api.slack.com/web
4 Chapter 1. Classes & Methods
python-slackclient Documentation, Release 1.0.1
join_channel(name)Join a channel by name.
Note: this action is not allowed by bots, they must be invited to channels.
send_to_websocket(data)Send a JSON message directly to the websocket. See RTM documentation <https://api.slack.com/rtm forallowed types.
Args data (dict) the key/values to send the websocket.
websocket_safe_read()Returns data if available, otherwise ‘’. Newlines indicate multiple messages
class slackclient._channel.Channel(server, name, channel_id, members=None)A Channel represents a public or private Slack Channel instance
send_message(message)Sends a message to a this Channel.
Args message (message) - the string you’d like to send to the channel
Returns None
5
python-slackclient Documentation, Release 1.0.1
6 Chapter 1. Classes & Methods
CHAPTER 2
Overview
This plugin is a light wrapper around the Slack API. In its basic form, it can be used to call any API method and beexpected to return a dict of the JSON reply.
The optional RTM connection allows you to create a persistent websocket connection, from which you can read eventsjust like an official Slack client. This allows you to respond to events in real time without polling and send messageswithout making a full HTTPS request.
See python-rtmbot for an active project utilizing this library.
7
python-slackclient Documentation, Release 1.0.1
8 Chapter 2. Overview
CHAPTER 3
Installation
3.1 Automatic w/ PyPI (virtualenv is recommended.)
pip install slackclient
3.2 Manual
git clone https://github.com/slackhq/python-slackclient.gitpip install -r requirements.txt
9
python-slackclient Documentation, Release 1.0.1
10 Chapter 3. Installation
CHAPTER 4
Usage
See examples in docs/examples
Note: You must obtain a token for the user/bot. You can find or generate these at the Slack API page.
4.1 Basic API methods
from slackclient import SlackClient
token = "xoxp-28192348123947234198234" # found at https://api.slack.com/web#authenticationsc = SlackClient(token)print sc.api_call("api.test")print sc.api_call("channels.info", channel="1234567890")print sc.api_call(
"chat.postMessage", channel="#general", text="Hello from Python! :tada:",username='pybot', icon_emoji=':robot_face:'
)
4.2 Real Time Messaging
import timefrom slackclient import SlackClient
token = "xoxp-28192348123947234198234"# found at https://api.slack.com/web#authenticationsc = SlackClient(token)if sc.rtm_connect():
while True:print sc.rtm_read()time.sleep(1)
else:print "Connection Failed, invalid token?"
4.3 Objects & Methods
slackclient.SlackClient The basic proxy-model for accessing most of the API and RTM functionality
slackclient._server.Server object owns the websocket and all nested channel information.
11
python-slackclient Documentation, Release 1.0.1
SlackClient.server.channels A searchable list of all known channels within the parent server. Call print(sc instance) to see the entire list.
Methods DescriptionSlack-Client.server.channels.find([identifier])
The identifier can be either name or Slack channel ID. See above forexamples.
Slack-Client.server.channels[int].send_message([text])
Send message [text] to [int] channel in the channels list.
Slack-Client.server.channels.find([identifier]).send_message([text])
Send message [text] to channel [identifier], which can be eitherchannel name or ID. Ex “#general” or “C182391”
12 Chapter 4. Usage
CHAPTER 5
Contributing
5.1 Submitting a Pull Request
Please feel free to sent us pull requests or issues that you’d like to see included in the python-slackclient!
We ask that you include any appropriate tests or documentation in the pull request and try to follow good commitmessage hygiene in your commit message and PR commentary.
5.2 Building Documentation
To build this documentation:
pip install -r requirements-dev.txt -r requirements.txtcd docsmake html
Once it’s built, there will be a file at /docs/_build/html/index.html that you can open in your browser to see the docs.
5.2.1 Indices and tables
• genindex
• modindex
• search
13
python-slackclient Documentation, Release 1.0.1
14 Chapter 5. Contributing
Python Module Index
sslackclient, 3slackclient._channel, 5slackclient._server, 4
15
python-slackclient Documentation, Release 1.0.1
16 Python Module Index
Index
Aapi_call() (slackclient._server.Server method), 4api_call() (slackclient.SlackClient method), 3
CChannel (class in slackclient._channel), 5
Jjoin_channel() (slackclient._server.Server method), 4
Pprocess_changes() (slackclient.SlackClient method), 3
Rrtm_connect() (slackclient.SlackClient method), 3rtm_read() (slackclient.SlackClient method), 4rtm_send_message() (slackclient.SlackClient method), 4
Ssend_message() (slackclient._channel.Channel method),
5send_to_websocket() (slackclient._server.Server
method), 5Server (class in slackclient._server), 4SlackClient (class in slackclient), 3slackclient (module), 3slackclient._channel (module), 5slackclient._server (module), 4
Wwebsocket_safe_read() (slackclient._server.Server
method), 5
17