Python-slack-sdk: WebClient or RTMClient?

Created on 4 Sep 2019  路  7Comments  路  Source: slackapi/python-slack-sdk

Description

I have a working bot using slackclient in version 1.3.1 (https://github.com/staticdev/k8s-python-slackbot/blob/master/echobot.py). I got a bit confused with the Migration Guide with this new two clients. I tried separately to use both, but with 'RTMClient' has no 'api_call' for me to get the users list and with 'WebClient' I see no start() method to start the server and I see no example of how to receive messages. What would be the recommended client in this case?

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x] question
  • [x] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [ ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [ ] I've read and agree to the Code of Conduct.
  • [ ] I've searched for any related issues and avoided creating a duplicate issue.

2x docs question rtm-client web-client

Most helpful comment

Perfect @seratch!! I already corrected the bugs with your solution. Thanks a lot for the effort. So in the end, the answer to the initial question is RTMClient. Issue closed =).

All 7 comments

The WebClient and RTMClient are two separate things. They were in 1.3.1 also, so there's no difference there. One difference in 2.x is that if you use the RTMClient it will automatically create a WebClient for you and pass you the handle to it to all of your event handlers.

You'll probably find this helpful: https://python-slackclient.readthedocs.io/en/latest/real_time_messaging.html

@justdave Thanks for answering the question here.
@staticdev Do you have anything further to ask here? If not, could you close this issue now?

@seratch @justdave The whole problem is that I not using either WebClient or RTMClient. I am using SlackClient and trying to migrate using the guide. So I don't know if I use 'RTMClient' or 'WebClient'. The link you sent me did not help me at all, since RTMClient has the start() method I am using.

@staticdev
For the v2 migration of https://github.com/staticdev/k8s-python-slackbot/blob/master/echobot.py, the app will be mainly using RTMClient for receiving events from Slack. But, the app will also use WebClient for Web API calls.

For instance, the following code:

SLACK_CLIENT = slackclient.SlackClient(SLACK_BOT_TOKEN)
API_CALL = SLACK_CLIENT.api_call('users.list')
if API_CALL.get('ok'):
    # retrieve all users so we can find our bot
    USERS = API_CALL.get('members')
    for member in USERS:
        if 'name' in member and member.get('name') == SLACK_BOT_NAME:
            BOT_ID = member.get('id')

can be as below with slackclient v2.

from slack import WebClient, RTMClient

web_client = WebClient(token=SLACK_BOT_TOKEN)
res = web_client.auth_test() # auth.test recently started returning bot_id
BOT_ID = res["bot_id"]

For examples with RTMClient, refer to https://slack.dev/python-slackclient/real_time_messaging.html

When you need WebClient in RTM callbacks, it is available in payloads:

@RTMClient.run_on(event="message")
def say_hello(**payload):
  data = payload['data']
  web_client = payload['web_client']

Nice @seratch, thanks. So if RTMClient creates a WebClient, is it possible to create a RTMClient in the begining instead, test the connection (with auth_test() from the WebClient created) and get the bot_id instead?

Somehow I changed the code, it connects but I don't see my bot online on Slack anymore. Other very strange behavior is that it does not stop senting the echo message (it sends infinite times):
https://github.com/staticdev/k8s-python-slackbot/pull/8

Perfect @seratch!! I already corrected the bugs with your solution. Thanks a lot for the effort. So in the end, the answer to the initial question is RTMClient. Issue closed =).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marshallino16 picture marshallino16  路  3Comments

seratch picture seratch  路  3Comments

tinoargentino picture tinoargentino  路  4Comments

LMPK picture LMPK  路  3Comments

Terrance picture Terrance  路  3Comments