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?
x in one of the [ ])x in each of the [ ])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
@staticdev Probably, doing like this is a possible solution for it.
https://github.com/slackapi/python-slackclient/blob/db08cccfcf288545d962c674a35639ab93ba94f6/integration_tests/samples/issues/issue_506.py
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 =).
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 =).