Skip to content

Commit

Permalink
fix-issue-1145 (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-aldaz authored Oct 31, 2022
1 parent 2f926a6 commit 54f05e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 61 deletions.
39 changes: 8 additions & 31 deletions docs-src/real_time_messaging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RTM Client
Real Time Messaging (RTM)
---------------------------------------

.. parsed-literal ::
**rtm.start method has been deprecated for apps created after Nov 30th, 2021.** See details `here <https://api.slack.com/changelog/2021-10-rtm-start-to-stop>`_
The `Real Time Messaging (RTM) API`_ is a WebSocket-based API that allows you to receive events from Slack in real time and send messages as users.

If you prefer events to be pushed to your app, we recommend using the HTTP-based `Events API <https://api.slack.com/events-api>`_ along with `Socket Mode <https://api.slack.com/socket-mode>`_ instead. The Events API contains some events that aren't supported in the RTM API (like `app_home_opened event <https://api.slack.com/events/app_home_opened>`_), and it supports most of the event types in the RTM API. If you'd like to use the Events API, you can use the `Python Slack Events Adaptor <https://github.com/slackapi/python-slack-events-api>`_.
Expand Down Expand Up @@ -50,7 +53,7 @@ Note that the import here is not ``from slack_sdk.rtm import RTMClient`` but ``f
thread_ts=thread_ts
)
rtm.start()
rtm.connect()
**Connecting to the RTM API (v1 client)**
Expand Down Expand Up @@ -80,40 +83,14 @@ Below is a code snippet that uses the legacy version of ``RTMClient``. For new a
slack_token = os.environ["SLACK_BOT_TOKEN"]
rtm_client = RTMClient(token=slack_token)
rtm_client.start()
rtm_client.connect()
**rtm.start vs rtm.connect (v1 client)**

By default, the RTM client uses ``rtm.connect`` to establish a WebSocket connection with Slack. The response contains basic information about the team and WebSocket url.

If you'd rather use ``rtm.start`` to establish the connection, which provides more information about the conversations and users on the team, you can set the ``connect_method`` option to ``rtm.start`` when instantiating the RTM Client. Note that on larger teams, use of ``rtm.start`` can be slow and unreliable.

.. code-block:: python
import os
from slack_sdk.rtm import RTMClient
@RTMClient.run_on(event="message")
def say_hello(**payload):
data = payload['data']
web_client = payload['web_client']
if 'text' in data and 'Hello' in data['text']:
channel_id = data['channel']
thread_ts = data['ts']
user = data['user'] # This is not username but user ID (the format is either U*** or W***)
web_client.chat_postMessage(
channel=channel_id,
text=f"Hi <@{user}>!",
thread_ts=thread_ts
)
.. parsed-literal ::
**rtm.start method has been deprecated for apps created after Nov 30th, 2021.** See details `here <https://api.slack.com/changelog/2021-10-rtm-start-to-stop>`_
slack_token = os.environ["SLACK_BOT_TOKEN"]
rtm_client = RTMClient(
token=slack_token,
connect_method='rtm.start'
)
rtm_client.start()
By default, the RTM client uses ``rtm.connect`` to establish a WebSocket connection with Slack. The response contains basic information about the team and WebSocket url.

Read the `rtm.connect docs <https://api.slack.com/methods/rtm.connect>`_ and the `rtm.start docs <https://api.slack.com/methods/rtm.start>`_ for more details. Also, note that ``slack.rtm_v2.RTMClient`` does not support ``rtm.start``.

Expand Down
33 changes: 4 additions & 29 deletions docs/real_time_messaging.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
<span id="real-time-messaging"></span><h1>RTM Client<a class="headerlink" href="#rtm-client" title="Permalink to this heading"></a></h1>
<section id="real-time-messaging-rtm">
<h2>Real Time Messaging (RTM)<a class="headerlink" href="#real-time-messaging-rtm" title="Permalink to this heading"></a></h2>
<pre class="literal-block"><strong>rtm.start method deprecation after Nov 30th, 2021.</strong> see details <a class="reference external" href="https://api.slack.com/changelog/2021-10-rtm-start-to-stop">here</a></pre>
<p>The <a class="reference external" href="https://api.slack.com/rtm">Real Time Messaging (RTM) API</a> is a WebSocket-based API that allows you to receive events from Slack in real time and send messages as users.</p>
<p>If you prefer events to be pushed to your app, we recommend using the HTTP-based <a class="reference external" href="https://api.slack.com/events-api">Events API</a> along with <a class="reference external" href="https://api.slack.com/socket-mode">Socket Mode</a> instead. The Events API contains some events that aren’t supported in the RTM API (like <a class="reference external" href="https://api.slack.com/events/app_home_opened">app_home_opened event</a>), and it supports most of the event types in the RTM API. If you’d like to use the Events API, you can use the <a class="reference external" href="https://github.com/slackapi/python-slack-events-api">Python Slack Events Adaptor</a>.</p>
<p>The RTMClient allows apps to communicate with the Slack Platform’s RTM API.</p>
Expand Down Expand Up @@ -244,7 +245,7 @@ <h2>Real Time Messaging (RTM)<a class="headerlink" href="#real-time-messaging-rt
<span class="n">thread_ts</span><span class="o">=</span><span class="n">thread_ts</span>
<span class="p">)</span>

<span class="n">rtm</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">rtm</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
</pre></div>
</div>
<p><strong>Connecting to the RTM API (v1 client)</strong></p>
Expand All @@ -270,38 +271,12 @@ <h2>Real Time Messaging (RTM)<a class="headerlink" href="#real-time-messaging-rt

<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_BOT_TOKEN&quot;</span><span class="p">]</span>
<span class="n">rtm_client</span> <span class="o">=</span> <span class="n">RTMClient</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">rtm_client</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">rtm_client</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
</pre></div>
</div>
<p><strong>rtm.start vs rtm.connect (v1 client)</strong></p>
<pre class="literal-block"><strong>rtm.start method deprecation after Nov 30th, 2021.</strong> see details <a class="reference external" href="https://api.slack.com/changelog/2021-10-rtm-start-to-stop">here</a></pre>
<p>By default, the RTM client uses <code class="docutils literal notranslate"><span class="pre">rtm.connect</span></code> to establish a WebSocket connection with Slack. The response contains basic information about the team and WebSocket url.</p>
<p>If you’d rather use <code class="docutils literal notranslate"><span class="pre">rtm.start</span></code> to establish the connection, which provides more information about the conversations and users on the team, you can set the <code class="docutils literal notranslate"><span class="pre">connect_method</span></code> option to <code class="docutils literal notranslate"><span class="pre">rtm.start</span></code> when instantiating the RTM Client. Note that on larger teams, use of <code class="docutils literal notranslate"><span class="pre">rtm.start</span></code> can be slow and unreliable.</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">from</span> <span class="nn">slack_sdk.rtm</span> <span class="kn">import</span> <span class="n">RTMClient</span>

<span class="nd">@RTMClient</span><span class="o">.</span><span class="n">run_on</span><span class="p">(</span><span class="n">event</span><span class="o">=</span><span class="s2">&quot;message&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">say_hello</span><span class="p">(</span><span class="o">**</span><span class="n">payload</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">payload</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">]</span>
<span class="n">web_client</span> <span class="o">=</span> <span class="n">payload</span><span class="p">[</span><span class="s1">&#39;web_client&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="s1">&#39;text&#39;</span> <span class="ow">in</span> <span class="n">data</span> <span class="ow">and</span> <span class="s1">&#39;Hello&#39;</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;text&#39;</span><span class="p">]:</span>
<span class="n">channel_id</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;channel&#39;</span><span class="p">]</span>
<span class="n">thread_ts</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;ts&#39;</span><span class="p">]</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;user&#39;</span><span class="p">]</span> <span class="c1"># This is not username but user ID (the format is either U*** or W***)</span>

<span class="n">web_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_id</span><span class="p">,</span>
<span class="n">text</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;Hi &lt;@</span><span class="si">{</span><span class="n">user</span><span class="si">}</span><span class="s2">&gt;!&quot;</span><span class="p">,</span>
<span class="n">thread_ts</span><span class="o">=</span><span class="n">thread_ts</span>
<span class="p">)</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_BOT_TOKEN&quot;</span><span class="p">]</span>
<span class="n">rtm_client</span> <span class="o">=</span> <span class="n">RTMClient</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">connect_method</span><span class="o">=</span><span class="s1">&#39;rtm.start&#39;</span>
<span class="p">)</span>
<span class="n">rtm_client</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>Read the <a class="reference external" href="https://api.slack.com/methods/rtm.connect">rtm.connect docs</a> and the <a class="reference external" href="https://api.slack.com/methods/rtm.start">rtm.start docs</a> for more details. Also, note that <code class="docutils literal notranslate"><span class="pre">slack.rtm_v2.RTMClient</span></code> does not support <code class="docutils literal notranslate"><span class="pre">rtm.start</span></code>.</p>
<p><strong>RTM Events</strong></p>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="w"></span>
Expand Down
Loading

0 comments on commit 54f05e6

Please sign in to comment.