8000 python-slackclient/docs/basic_usage.html at master · erohmensing/python-slackclient · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
545 lines (489 loc) · 46.1 KB

File metadata and controls

545 lines (489 loc) · 46.1 KB
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Usage &mdash; Slack Developer Kit for Python</title>
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-KFZ5MK7');</script>
<!-- End Google Tag Manager -->
<link href="https://a.slack-edge.com/4f227/style/rollup-slack_kit_legacy_adapters.css" rel="stylesheet" type="text/css">
<link href="https://a.slack-edge.com/3e02c0/style/rollup-api_site.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="./_static/default.css" type="text/css" />
<link rel="stylesheet" href="./_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="./_static/docs.css" type="text/css" />
<link id="favicon" rel="shortcut icon" href="https://a.slack-edge.com/4f28/img/icons/favicon-32.png" type="image/png" />
<link rel="top" title="Slack Developer Kit for Python 1.0.1 documentation" href="index.html" />
</head>
<body class="api light_theme">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KFZ5MK7" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<header>
<a id="menu_toggle" class="no_transition show_on_mobile">
<span class="menu_icon"></span>
<span class="vert_divider"></span>
</a>
<a href="https://api.slack.com/" id="header_logo" class="api hide_on_mobile" style="float:left; display: inline-block;">
<img alt="Slack API" src="https://a.slack-edge.com/3026cb/img/slack_api_logo_vogue.png" style="width: 225px; padding-right: 25px; border-right: 1px solid #DDD;" />
</a>
<span style="display: inline-block; padding-left: 20px; margin-top: 25px; font-weight: bold;">
<a style="color: #555459;" href="./index.html">Slack Developer Kit for Python</a>
</span>
<div class="header_nav">
<a href="https://github.com/SlackAPI/python-slackclient" class="btn header_btn float_right" data-qa="go_to_slack">Go
to GitHub</a>
</div>
</header>
<div id="page">
<div id="page_contents" class="clearfix">
<!-- Sidebar Content -->
<nav id="api_nav" class="col span_1_of_4">
<div id="api_sections">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="index.html">Slack Developer Kit for Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="auth.html">Tokens &amp; Authentication</a><ul>
<li class="toctree-l2"><a class="reference internal" href="auth.html#handling-tokens-and-other-sensitive-data">Handling tokens and other sensitive data</a></li>
<li class="toctree-l2"><a class="reference internal" href="auth.html#single-workspace-apps">Single-Workspace Apps</a></li>
<li class="toctree-l2"><a class="reference internal" href="auth.html#the-oauth-flow">The OAuth flow</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Basic Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#sending-a-message">Sending a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="#customizing-a-message-s-layout">Customizing a message’s layout</a></li>
<li class="toctree-l2"><a class="reference internal" href="#replying-to-messages-and-creating-threads">Replying to messages and creating threads</a></li>
<li class="toctree-l2"><a class="reference internal" href="#updating-the-content-of-a-message">Updating the content of a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="#deleting-a-message">Deleting a message</a></li>
<li class="toctree-l2"><a class="reference internal" href="#adding-or-removing-an-emoji-reaction">Adding or removing an emoji reaction</a></li>
<li class="toctree-l2"><a class="reference internal" href="#getting-a-list-of-channels">Getting a list of channels</a></li>
<li class="toctree-l2"><a class="reference internal" href="#getting-a-channel-s-info">Getting a channel’s info</a></li>
<li class="toctree-l2"><a class="reference internal" href="#joining-a-channel">Joining a channel</a></li>
<li class="toctree-l2"><a class="reference internal" href="#leaving-a-channel">Leaving a channel</a></li>
<li class="toctree-l2"><a class="reference internal" href="#get-a-list-of-team-members">Get a list of team members</a></li>
<li class="toctree-l2"><a class="reference internal" href="#uploading-files">Uploading files</a></li>
<li class="toctree-l2"><a class="reference internal" href="#web-api-rate-limits">Web API Rate Limits</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="conversations.html">Conversations API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#creating-a-direct-message-or-multi-person-direct-message">Creating a direct message or multi-person direct message</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#creating-a-public-or-private-channel">Creating a public or private channel</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#getting-information-about-a-conversation">Getting information about a conversation</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#getting-a-list-of-conversations">Getting a list of conversations</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#leaving-a-conversation">Leaving a conversation</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#get-conversation-members">Get conversation members</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="real_time_messaging.html">Real Time Messaging (RTM)</a><ul>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#connecting-to-the-rtm-api">Connecting to the RTM API</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#rtm-start-vs-rtm-connect">rtm.start vs rtm.connect</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#rtm-events">RTM Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="real_time_messaging.html#sending-messages-via-the-rtm-api">Sending messages via the RTM API</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="faq.html#what-even-is-product-name-and-why-should-i-care">What even is Slack Developer Kit for Python and why should I care?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#omg-i-found-a-bug">OMG I found a bug!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#hey-there-s-a-feature-missing">Hey, there’s a feature missing!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#i-d-like-to-contribute-but-how">I’d like to contribute…but how?</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#how-do-i-compile-the-documentation">How do I compile the documentation?</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a><ul>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-3-0-2018-09-11">v1.3.0 (2018-09-11)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-2-1-2018-03-26">v1.2.1 (2018-03-26)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-2-0-2018-03-20">v1.2.0 (2018-03-20)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-3-2018-03-01">v1.1.3 (2018-03-01)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-2-2018-01-31">v1.1.2 (2018-01-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-1-2018-01-30">v1.1.1 (2018-01-30)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-1-0-2017-11-21">v1.1.0 (2017-11-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-9-2017-08-31">v1.0.9 (2017-08-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-8-2017-08-31">v1.0.8 (2017-08-31)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-7-2017-08-02">v1.0.7 (2017-08-02)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-6-2017-06-12">v1.0.6 (2017-06-12)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-5-2017-01-23">v1.0.5 (2017-01-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-4-2016-12-15">v1.0.4 (2016-12-15)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-3-2016-12-13">v1.0.3 (2016-12-13)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-2-2016-09-22">v1.0.2 (2016-09-22)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-1-2016-03-25">v1.0.1 (2016-03-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-0-0-2016-02-28">v1.0.0 (2016-02-28)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v0-18-0-2016-02-21">v0.18.0 (2016-02-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v0-17-0-2016-02-15">v0.17.0 (2016-02-15)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a><ul>
<li class="toctree-l2"><a class="reference internal" href="about.html#product-name">Slack Developer Kit for Python</a></li>
</ul>
</li>
</ul>
<div id="footer">
<ul id="footer_nav">
<li><a href="https://github.com/SlackAPI/python-slackclient/blob/master/LICENSE">License</a></li>
<li><a href="https://slackhq.github.io/code-of-conduct">Code of Conduct</a></li>
<li><a href="https://github.com/slackapi/python-slackclient/blob/master/.github/contributing.md">Contributing</a></li>
<li><a href="https://docs.google.com/a/slack-corp.com/forms/d/e/1FAIpQLSfzjVoCM7ohBnjWf7eDYQxzti1EPpinsIJQA5RAUBwJKRUQHg/viewform">Contributor License Agreement</a></li>
</ul>
<p id="footer_signature">Made with <i class="ts_icon ts_icon_heart"></i> by Slack<br/>and our Lovely
Community
</p>
</div>
</div>
</nav>
<!-- /Sidebar Content -->
<!-- Body Content -->
<div class="col span_3_of_4">
<div class="section-title">Basic Usage</div>
<div class="card">
<div class="section" id="basic-usage">
<span id="web-api-examples"></span><h1>Basic Usage<a class="headerlink" href="#basic-usage" title="Permalink to this headline"></a></h1>
<p>The Slack Web API allows you to build applications that interact with Slack in more complex ways than the integrations
we provide out of the box.</p>
<p>This package is a modular wrapper designed to make Slack <a class="reference external" href="https://api.slack.com/web">Web API</a> calls simpler and easier for your
app. Provided below are examples of how to interact with commonly used API endpoints, but this is by no means
a complete list. Review the full list of available methods <a class="reference external" href="https://api.slack.com/methods">here</a>.</p>
<p>See <a class="reference internal" href="auth.html#handling-tokens"><span class="std std-ref">Tokens &amp; Authentication</span></a> for API token handling best practices.</p>
<hr class="docutils" />
<div class="section" id="sending-a-message">
<h2>Sending a message<a class="headerlink" href="#sending-a-message" title="Permalink to this headline"></a></h2>
<p>The primary use of Slack is sending messages. Whether you’re sending a message
to a user or to a channel, this method handles both.</p>
<p>To send a message to a channel, use the channel’s ID. For IMs, use the user’s ID.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postMessage&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello from Python! :tada:&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>There are some unique options specific to sending IMs, so be sure to read the <strong>channels</strong>
section of the <a class="reference external" href="https://api.slack.com/methods/chat.postMessage#channels">chat.postMessage</a>
page for a full list of formatting and authorship options.</p>
<p>Sending an ephemeral message, which is only visible to an assigned user in a specified channel, is nearly the same
as sending a regular message, but with an additional <code class="docutils literal notranslate"><span class="pre">user</span></code> parameter.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postEphemeral&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello from Python! :tada:&quot;</span><span class="p">,</span>
<span class="n">user</span><span class="o">=</span><span class="s2">&quot;U0XXXXXXX&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/chat.postEphemeral">chat.postEphemeral</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="customizing-a-message-s-layout">
<h2>Customizing a message’s layout<a class="headerlink" href="#customizing-a-message-s-layout" title="Permalink to this headline"></a></h2>
<p>The chat.postMessage method takes an optional blocks argument that allows you to customize the layout of a message.
Blocks for Web API methods are all specified in a single object literal, so just add additional keys for any optional argument.</p>
<p>To send a message to a channel, use the channel’s ID. For IMs, use the user’s ID.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postMessage&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">blocks</span><span class="o">=</span><span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;mrkdwn&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Danny Torrence left the following review for your property:&quot;</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;mrkdwn&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;&lt;https://example.com|Overlook Hotel&gt; </span><span class="se">\n</span><span class="s2"> :star: </span><span class="se">\n</span><span class="s2"> Doors had too many axe holes, guest in room &quot;</span> <span class="o">+</span>
<span class="s2">&quot;237 was far too rowdy, whole place felt stuck in the 1920s.&quot;</span>
<span class="p">},</span>
<span class="s2">&quot;accessory&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;image&quot;</span><span class="p">,</span>
<span class="s2">&quot;image_url&quot;</span><span class="p">:</span> <span class="s2">&quot;https://images.pexels.com/photos/750319/pexels-photo-750319.jpeg&quot;</span><span class="p">,</span>
<span class="s2">&quot;alt_text&quot;</span><span class="p">:</span> <span class="s2">&quot;Haunted hotel image&quot;</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
<span class="s2">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;mrkdwn&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;*Average Rating*</span><span class="se">\n</span><span class="s2">1.0&quot;</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">)</span>
</pre></div>
</div>
<p><strong>Note:</strong> You can use the <a href="#id1"><span class="problematic" id="id2">`</span></a>Block Kit Builder &lt;<a class="reference external" href="https://api.slack.com/tools/block-kit-builder">https://api.slack.com/tools/block-kit-builder</a>&gt;`for a playground where you can prototype your message’s look and feel.</p>
</div>
<hr class="docutils" />
<div class="section" id="replying-to-messages-and-creating-threads">
<h2>Replying to messages and creating threads<a class="headerlink" href="#replying-to-messages-and-creating-threads" title="Permalink to this headline"></a></h2>
<p>Threaded messages are just like regular messages, except thread replies are grouped together to provide greater context
to the user. You can reply to a thread or start a new threaded conversation by simply passing the original message’s <code class="docutils literal notranslate"><span class="pre">ts</span></code>
ID in the <code class="docutils literal notranslate"><span class="pre">thread_ts</span></code> attribute when posting a message. If you’re replying to a threaded message, you’ll pass the <cite>thread_ts</cite>
ID of the message you’re replying to.</p>
<p>A channel or DM conversation is a nearly linear timeline of messages exchanged between people, bots, and apps.
When one of these messages is replied to, it becomes the parent of a thread. By default, threaded replies do not
appear directly in the channel, instead relegated to a kind of forked timeline descending from the parent message.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postMessage&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello from Python! :tada:&quot;</span><span class="p">,</span>
<span class="n">thread_ts</span><span class="o">=</span><span class="s2">&quot;1476746830.000003&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>By default, <code class="docutils literal notranslate"><span class="pre">reply_broadcast</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>. To indicate your reply is germane to all members of a channel,
set the <code class="docutils literal notranslate"><span class="pre">reply_broadcast</span></code> boolean parameter to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postMessage&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello from Python! :tada:&quot;</span><span class="p">,</span>
<span class="n">thread_ts</span><span class="o">=</span><span class="s2">&quot;1476746830.000003&quot;</span><span class="p">,</span>
<span class="n">reply_broadcast</span><span class="o">=</span><span class="bp">True</span>
<span class="p">)</span>
</pre></div>
</div>
<p><strong>Note:</strong> While threaded messages may contain attachments and message buttons, when your reply is broadcast to the
channel, it’ll actually be a reference to your reply, not the reply itself.
So, when appearing in the channel, it won’t contain any attachments or message buttons. Also note that updates and
deletion of threaded replies works the same as regular messages.</p>
<p>See the <a class="reference external" href="https://api.slack.com/docs/message-threading#forking_conversations">Threading messages together</a>
article for more information.</p>
</div>
<hr class="docutils" />
<div class="section" id="updating-the-content-of-a-message">
<h2>Updating the content of a message<a class="headerlink" href="#updating-the-content-of-a-message" title="Permalink to this headline"></a></h2>
<p>Let’s say you have a bot which posts the status of a request. When that request
is updated, you’ll want to update the message to reflect it’s state. Or your user
might want to fix a typo or change some wording. This is how you’ll make those changes.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.update&quot;</span><span class="p">,</span>
<span class="n">ts</span><span class="o">=</span><span class="s2">&quot;1476746830.000003&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello from Python! :tada:&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/chat.update">chat.update</a> for formatting options
and some special considerations when calling this with a bot user.</p>
</div>
<hr class="docutils" />
<div class="section" id="deleting-a-message">
<h2>Deleting a message<a class="headerlink" href="#deleting-a-message" title="Permalink to this headline"></a></h2>
<p>Sometimes you need to delete things.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.delete&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span>
<span class="n">ts</span><span class="o">=</span><span class="s2">&quot;1476745373.000002&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/chat.delete">chat.delete</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="adding-or-removing-an-emoji-reaction">
<h2>Adding or removing an emoji reaction<a class="headerlink" href="#adding-or-removing-an-emoji-reaction" title="Permalink to this headline"></a></h2>
<p>You can quickly respond to any message on Slack with an emoji reaction. Reactions
can be used for any purpose: voting, checking off to-do items, showing excitement — and just for fun.</p>
<p>This method adds a reaction (emoji) to an item (<code class="docutils literal notranslate"><span class="pre">file</span></code>, <code class="docutils literal notranslate"><span class="pre">file</span> <span class="pre">comment</span></code>, <code class="docutils literal notranslate"><span class="pre">channel</span> <span class="pre">message</span></code>, <code class="docutils literal notranslate"><span class="pre">group</span> <span class="pre">message</span></code>, or <code class="docutils literal notranslate"><span class="pre">direct</span> <span class="pre">message</span></code>). One of file, file_comment, or the combination of channel and timestamp must be specified.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;reactions.add&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXXX&quot;</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;thumbsup&quot;</span><span class="p">,</span>
<span class="n">timestamp</span><span class="o">=</span><span class="s2">&quot;1234567890.123456&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Removing an emoji reaction is basically the same format, but you’ll use <code class="docutils literal notranslate"><span class="pre">reactions.remove</span></code> instead of <code class="docutils literal notranslate"><span class="pre">reactions.add</span></code></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;reactions.remove&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXXX&quot;</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;thumbsup&quot;</span><span class="p">,</span>
<span class="n">timestamp</span><span class="o">=</span><span class="s2">&quot;1234567890.123456&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/reactions.add">reactions.add</a> and <a class="reference external" href="https://api.slack.com/methods/reactions.remove">reactions.remove</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="getting-a-list-of-channels">
<h2>Getting a list of channels<a class="headerlink" href="#getting-a-list-of-channels" title="Permalink to this headline"></a></h2>
<p>At some point, you’ll want to find out what channels are available to your app. This is how you get that list.</p>
<p><strong>Note:</strong> This call requires the <code class="docutils literal notranslate"><span class="pre">channels:read</span></code> scope.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span><span class="s2">&quot;channels.list&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Archived channels are included by default. You can exclude them by passing <code class="docutils literal notranslate"><span class="pre">exclude_archived=1</span></code> to your request.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;channels.list&quot;</span><span class="p">,</span>
<span class="n">exclude_archived</span><span class="o">=</span><span class="mi">1</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/channels.list">channels.list</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="getting-a-channel-s-info">
<h2>Getting a channel’s info<a class="headerlink" href="#getting-a-channel-s-info" title="Permalink to this headline"></a></h2>
<p>Once you have the ID for a specific channel, you can fetch information about that channel.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;channels.info&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXXX&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/channels.info">channels.info</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="joining-a-channel">
<h2>Joining a channel<a class="headerlink" href="#joining-a-channel" title="Permalink to this headline"></a></h2>
<p>Channels are the social hub of most Slack teams. Here’s how you hop into one:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;channels.join&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXXY&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>If you are already in the channel, the response is slightly different.
<code class="docutils literal notranslate"><span class="pre">already_in_channel</span></code> will be true, and a limited <code class="docutils literal notranslate"><span class="pre">channel</span></code> object will be returned. Bot users cannot join a channel on their own, they need to be invited by another user.</p>
<p>See <a class="reference external" href="https://api.slack.com/methods/channels.join">channels.join</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="leaving-a-channel">
<h2>Leaving a channel<a class="headerlink" href="#leaving-a-channel" title="Permalink to this headline"></a></h2>
<p>Maybe you’ve finished up all the business you had in a channel, or maybe you
joined one by accident. This is how you leave a channel.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;channels.leave&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="s2">&quot;C0XXXXXXX&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/channels.leave">channels.leave</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="get-a-list-of-team-members">
<h2>Get a list of team members<a class="headerlink" href="#get-a-list-of-team-members" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span><span class="s2">&quot;users.list&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/users.list">users.list</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="uploading-files">
<h2>Uploading files<a class="headerlink" href="#uploading-files" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;thinking_very_much.png&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file_content</span><span class="p">:</span>
<span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;files.upload&quot;</span><span class="p">,</span>
<span class="n">channels</span><span class="o">=</span><span class="s2">&quot;C3UKJTQAC&quot;</span><span class="p">,</span>
<span class="nb">file</span><span class="o">=</span><span class="n">file_content</span><span class="p">,</span>
<span class="n">title</span><span class="o">=</span><span class="s2">&quot;Test upload&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="https://api.slack.com/methods/files.upload">files.upload</a> for more info.</p>
</div>
<hr class="docutils" />
<div class="section" id="web-api-rate-limits">
<h2>Web API Rate Limits<a class="headerlink" href="#web-api-rate-limits" title="Permalink to this headline"></a></h2>
<p>Slack allows applications to send no more than one message per second. We allow bursts over that
limit for short periods. However, if your app continues to exceed the limit over a longer period
of time it will be rate limited.</p>
<p>Here’s a very basic example of how one might deal with rate limited requests.</p>
<p>If you go over these limits, Slack will start returning a HTTP 429 Too Many Requests error,
a JSON object containing the number of calls you have been making, and a Retry-After header
containing the number of seconds until you can retry.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slackclient</span> <span class="kn">import</span> <span class="n">SlackClient</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="n">slack_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;SLACK_API_TOKEN&quot;</span><span class="p">]</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SlackClient</span><span class="p">(</span><span class="n">slack_token</span><span class="p">)</span>
<span class="c1"># Simple wrapper for sending a Slack message</span>
<span class="k">def</span> <span class="nf">send_slack_message</span><span class="p">(</span><span class="n">channel</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="k">return</span> <span class="n">sc</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
<span class="s2">&quot;chat.postMessage&quot;</span><span class="p">,</span>
<span class="n">channel</span><span class="o">=</span><span class="n">channel</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="n">message</span>
<span class="p">)</span>
<span class="c1"># Make the API call and save results to `response`</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">send_slack_message</span><span class="p">(</span><span class="s2">&quot;C0XXXXXX&quot;</span><span class="p">,</span> <span class="s2">&quot;Hello, from Python!&quot;</span><span class="p">)</span>
<span class="c1"># Check to see if the message sent successfully.</span>
<span class="c1"># If the message succeeded, `response[&quot;ok&quot;]`` will be `True`</span>
<span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s2">&quot;ok&quot;</span><span class="p">]:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">&quot;Message posted successfully: &quot;</span> <span class="o">+</span> <span class="n">response</span><span class="p">[</span><span class="s2">&quot;message&quot;</span><span class="p">][</span><span class="s2">&quot;ts&quot;</span><span class="p">])</span>
<span class="c1"># If the message failed, check for rate limit headers in the response</span>
<span class="k">elif</span> <span class="n">response</span><span class="p">[</span><span class="s2">&quot;ok&quot;</span><span class="p">]</span> <span class="ow">is</span> <span class="bp">False</span> <span class="ow">and</span> <span class="n">response</span><span class="p">[</span><span class="s2">&quot;headers&quot;</span><span class="p">][</span><span class="s2">&quot;Retry-After&quot;</span><span class="p">]:</span>
<span class="c1"># The `Retry-After` header will tell you how long to wait before retrying</span>
<span class="n">delay</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">response</span><span class="p">[</span><span class="s2">&quot;headers&quot;</span><span class="p">][</span><span class="s2">&quot;Retry-After&quot;</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="s2">&quot;Rate limited. Retrying in &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">delay</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; seconds&quot;</span><span class="p">)</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">delay</span><span class="p">)</span>
<span class="n">send_slack_message</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">channel</span><span class="p">)</span>
</pre></div>
</div>
<p>See the documentation on <a class="reference external" href="https://api.slack.com/docs/rate-limits">Rate Limiting</a> for more info.</p>
</div>
</div>
<div class="clear_both large_bottom_margin"></div>
</div>
</div>
<!-- /Body Content -->
</div>
</div>
<footer>
<p class="light tiny align_center">© 2019 Slack Technologies, Inc. and contributors</p>
</footer>
<script>
window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) }; ga.l = +new Date;
ga('create', 'UA-56978219-13', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
</body>
</html>
0