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

Skip to content

Latest commit

 

History

History
572 lines (549 loc) · 43.6 KB

File metadata and controls

572 lines (549 loc) · 43.6 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#keeping-tokens-safe">Keeping tokens safe</a></li>
<li class="toctree-l2"><a class="reference internal" href="auth.html#single-workspace-install">Single Workspace Install</a></li>
<li class="toctree-l2"><a class="reference internal" href="auth.html#multiple-workspace-install">Multiple Workspace Install</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="#formatting-with-block-kit">Formatting with Block Kit</a></li>
<li class="toctree-l2"><a class="reference internal" href="#threading-messages">Threading Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="#updating-a-message">Updating 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="#opening-a-modal">Opening a modal</a></li>
<li class="toctree-l2"><a class="reference internal" href="#updating-and-pushing-modals">Updating and pushing modals</a></li>
<li class="toctree-l2"><a class="reference internal" href="#emoji-reactions">Emoji reactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#listing-channels">Listing 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="#listing-team-members">Listing 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#direct-messages">Direct messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#creating-channels">Creating channels</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#getting-more-information">Getting more information</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversations.html#listing-conversations">Listing 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#getting-members">Getting 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>
</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#i-found-a-bug">I found a bug!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#there-s-a-feature-missing">There’s a feature missing!</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#how-do-i-contribute">How do I contribute?</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#v2-2-0-2019-09-25">v2.2.0 (2019-09-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-1-0-2019-07-01">v2.1.0 (2019-07-01)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v2-0-0-2019-04-29">v2.0.0 (2019-04-29)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#v1-3-1-2019-02-28">v1.3.1 (2019-02-28)</a></li>
<li class="toctree-l2"&g 8000 t;<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>Access Slack’s API methods requires an OAuth token – see the <a class="reference external" href="auth">Tokens &amp; Authentication</a> section for more on how Slack uses OAuth tokens as well as best practices.</p>
<p><a class="reference external" href="https://api.slack.com/methods">Each of these API methods</a> is fully documented on our developer site at api.slack.com</p>
<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>One of the primary uses of Slack is posting messages to a channel using the channel ID or as a DM to another person using their user ID. This method will handle either a channel ID or a user ID passed to the <code class="docutils literal notranslate"><span class="pre">channel</span></code> parameter.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">slack</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">client</span> <span class="o">=</span> <span class="n">slack</span><span class="o">.</span><span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">client</span><span class="o">.</span><span class="n">chat_postMessage</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 your app! :tada:&quot;</span>
<span class="p">)</span>
</pre></div>
</div>
<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">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">slack</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">client</span> <span class="o">=</span> <span class="n">slack</span><span class="o">.</span><span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">slack_token</span><span class="p">)</span>
<span class="n">client</span><span class="o">.</span><span class="n">chat_postEphemeral</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 silently from your app! :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="formatting-with-block-kit">
<h2>Formatting with Block Kit<a class="headerlink" href="#formatting-with-block-kit" title="Permalink to this headline">¶</a></h2>
<p>Messages posted from apps can contain more than just text, though. They can include full user interfaces composed of <a class="reference external" href="https://api.slack.com/block-kit">blocks</a>.</p>
<p>The chat.postMessage method takes an optional blocks argument that allows you to customize the layout of a message. Blocks 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">client</span><span class="o">.</span><span class="n">chat_postMessage</span><span class="p" 7D4E >(</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 class="reference external" href="https://api.slack.com/tools/block-kit-builder">Block Kit Builder</a> to prototype your message’s look and feel.</p>
</div>
<hr class="docutils" />
<div class="section" id="threading-messages">
<h2>Threading Messages<a class="headerlink" href="#threading-messages" title="Permalink to this headline">¶</a></h2>
<p>Threaded messages are a way of grouping messages together to provide greater context. 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="n">client</span><span class="o">.</span><span class="n">chat_postMessage</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 your app! :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, and therefore a notification of the reply should be posted in-channel, set the <code class="docutils literal notranslate"><span class="pre">reply_broadcast</span></code> 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="n">client</span><span class="o">.</span><span class="n">chat_postMessage</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 your app! :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-a-message">
<h2>Updating a message<a class="headerlink" href="#updating-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 changes, you’ll want to update the message to reflect it’s state.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">client</span><span class="o">.</span><span class="n">chat_update</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;updates from your app! :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="n">client</span><span class="o">.</span><span class="n">chat_delete</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="opening-a-modal">
<h2>Opening a modal<a class="headerlink" href="#opening-a-modal" title="Permalink to this headline">¶</a></h2>
<p>Modals allow you to collect data from users and display dynamic information in a focused surface.</p>
<p>Modals use the same blocks that compose messages with the addition of an <cite>input</cite> block.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">client</span><span class="o">.</span><span class="n">views_open</span><span class="p">(</span>
<span class="n">trigger_id</span><span class="o">=</span><span class="s2">&quot;3213746830.000023&quot;</span><span class="p">,</span>
<span class="n">view</span><span class="o">=</span><span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;modal&quot;</span><span class="p">,</span>
<span class="n">callback_id</span><span class="p">:</span> <span class="s2">&quot;view_identifier&quot;</span><span class="p">,</span>
<span class="n">title</span><span class="p">:</span> <span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="p">:</span> <span class="s2">&quot;Modal title&quot;</span>
<span class="p">},</span>
<span class="n">blocks</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;input&quot;</span><span class="p">,</span>
<span class="n">label</span><span class="p">:</span> <span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="p">:</span> <span class="s2">&quot;Input label&quot;</span>
<span class="p">},</span>
<span class="n">element</span><span class="p">:</span> <span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;plain_text_input&quot;</span><span class="p">,</span>
<span class="n">action_id</span><span class="p">:</span> <span class="s2">&quot;value_indentifier&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>See <a class="reference external" href="https://api.slack.com/methods/views.open">views.open</a> more details and additional parameters.</p>
</div>
<hr class="docutils" />
<div class="section" id="updating-and-pushing-modals">
<h2>Updating and pushing modals<a class="headerlink" href="#updating-and-pushing-modals" title="Permalink to this headline">¶</a></h2>
<p>You can dynamically update a view inside of a modal by calling <cite>views.update</cite> and passing the view ID returned in the previous <cite>views.open</cite> call.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">client</span><span class="o">.</span><span class="n">views_update</span><span class="p">(</span>
<span class="n">view_id</span><span class="o">=</span><span class="s2">&quot;V123401&quot;</span>
<span class="n">view</span><span class="o">=</span><span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;modal&quot;</span><span class="p">,</span>
<span class="n">callback_id</span><span class="p">:</span> <span class="s2">&quot;view_identifier&quot;</span><span class="p">,</span>
<span class="n">title</span><span class="p">:</span> <span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="p">:</span> <span class="s2">&quot;Modal title&quot;</span>
<span class="p">},</span>
<span class="n">blocks</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="p">:</span> <span class="p">{</span>
<span class="nb">type</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
<span class="n">text</span><span class="p">:</span> <span class="s2">&quot;An updated modal, indeed&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>See <a class="reference external" href="https://api.slack.com/methods/views.update">views.update</a> for more info.</p>
<p>If you want to push a new view onto the modal instead of updating an existing view, reference the <a class="reference external" href="https://api.slack.com/methods/views.push">views.push</a> documentation.</p>
</div>
<hr class="docutils" />
<div class="section" id="emoji-reactions">
<h2>Emoji reactions<a class="headerlink" href="#emoji-reactions" 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 -— or 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="n">client</span><span class="o">.</span><span class="n">reactions_add</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">client</span><span class="o">.</span><span class="n">reactions_remove</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="listing-channels">
<h2>Listing channels<a class="headerlink" href="#listing-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>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">client</span><span class="o">.</span><span class="n">channels_list</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="n">client</span><span class="o">.</span><span class="n">channels_list</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="n">client</span><span class="o">.</span><span class="n">channels_info</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="n">client</span><span class="o">.</span><span class="n">channels_join</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="n">client</span><span class="o">.</span><span class="n">channels_leave</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="listing-team-members">
<h2>Listing team members<a class="headerlink" href="#listing-team-members" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">client</span><span class="o">.</span><span class="n">users_list</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="n">client</span><span class="o">.</span><span class="n">files_upload</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="s2">&quot;files.pdf&quot;</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>When posting messages to a channel, Slack allows applications to send no more than one message per channel 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. Different API methods have other rate limits – be sure to <a class="reference external" href="https://api.slack.com/docs/rate-limits">check the limits</a> and test that your application has a graceful fallback if it should hit those limits.</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>
<p>Here’s a very basic example of how one might deal with rate limited requests.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">slack</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">client</span> <span class="o">=</span> <span class="n">slack</span><span class="o">.</span><span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</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">client</span><span class="o">.</span><span class="n">chat_postMessage</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="n">f</span><span class="s2">&quot;Message posted successfully: {response[&quot;</span><span class="n">message</span><span class="s2">&quot;][&quot;</span><span class="n">ts</span><span class="s2">&quot;]}&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 5C81 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