slackapi / python-slack-sdk

Slack Developer Kit for Python
https://tools.slack.dev/python-slack-sdk/
MIT License
3.86k stars 832 forks source link

client.rtm_read() only returns a single message in the list #231

Closed aoberoi closed 5 years ago

aoberoi commented 7 years ago

Description

This is a report coming from an internal support system and an investigation done with platform engineering.

Long story short: rtm_read() is documented as able to return multiple messages, but the underlying implementation in websocket-client (websocket.recv()) only returns the next available frame. This means there's a subtle bug in the RTM section of the official documentation (http://slackapi.github.io/python-slackclient/real_time_messaging.html#connecting-to-the-real-time-messaging-api): since we sleep one second after every call to rtm_read(), on a busy team we'll fall farther and farther behind the current state of the RTM stream.

You can verify this by changing sleep(1) to sleep(10) and observing that the first few messages at the beginning of the stream are printed one at a time, with a 10-second delay after each message. If you send additional messages during one of these 10-second windows, you'll also see those messages one at a time. (If the function worked as described, you would see all of the messages before the next sleep.)

a potential solution we identified is to iterate over the internal websocket object (it is defined as an iterable) to collect as many frames as are available before processing each message and returning the list. we're not sure if this is as simple as list(websocket) since that calls into a never ending loop (while True:...).

another consideration while fixing this issue is that we should reach out to the author of one of the most popular tutorials for using this package, because there's a logical issue with the sample code that author has published. (https://www.fullstackpython.com/blog/build-first-slack-bot-python.html)

in https://github.com/mattmakai/slack-starterbot/blob/master/starterbot.py#L39 the handling is weird because it will drop anything after the first message with an at-mention. but i think that’s their bug. our API contract didn’t change.

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


Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

slackclient version: 1.0.9

python version: all

OS version(s): all

Steps to reproduce:

  1. Write a program that is meant to receive many messages in a short period of time.
  2. Call client.rtm_read()
  3. Inspect the return value

Expected result:

many messages in the returned list

Actual result:

just one message in the returned list

harlowja commented 7 years ago

This would be very nice to fix! +1

RodneyU215 commented 5 years ago

Thanks for reporting this! I'm closing this issue as we've decided to address this enhancement in the v2 redesign of the client. Please see https://github.com/slackapi/python-slackclient/pull/384 and https://github.com/slackapi/python-slackclient/pull/394 for more details.