0

Basically i want to loop through parent messages in a slack channel and fetch parent messages in front end but i also want to check if there are any replies under any parent message and fetch them under the parent message in front end. Bear in mind that there are two methods involved from slack, conversations.history and conversations.replies where conversations.replies requires the parameter of ts (timestamp). So what im trying to do is to check the timestamps of each message and if its found in conversations.replies then propagate the message or at least thats what im trying to do.

python:

@app.route("/", methods=['GET', 'POST'])
def home():
    convo_history = client.conversations_history(channel=channel_to_listen)
    messages = convo_history['messages']

    for thread_ts in messages:
        thread_reply = thread_ts['ts']
        # app.logger.info(thread_reply)

    convo_replies = client.conversations_replies(channel=channel_to_listen, ts=thread_reply)
    # app.logger.info(convo_replies)
    messages_replies = convo_replies['messages']

    assert convo_history['ok']

    return render_template('index.html', messages=messages, messages_replies=messages_replies)

Jinja:

{% for message in messages %}
{% if message['ts'] in messages_replies %}
<h2>{{ message['ts'] | ctime}}</h2>
<h3>{{ message['text'] }}</h3>
<h4>{{ messages_replies['text'] }}</h4>
{% else %}
<h2>{{ message['ts'] | ctime}}</h2>
<h3>{{ message['text'] }}</h3>
<br>
{% endif %}
{% endfor %}
  • Hi! Can you please add what is not working, what error you get? And just in general: This approach will be very slow since you have to wait for rate limit (e.g. 1 request per sec). – Erik Kalkoken May 05 '20 at 11:28
  • In the front end im able to fetch the timestamp and the parent messages with the for loop but it doesnt show the threaded replies. What is the 1 request per sec (kinda newbie here)? Do you mean the api request? How can i mitigate that? – Makis Kahrimanidis May 05 '20 at 11:41
  • All API requests in Slack are rate limited. One average to max 1 request per second. You have to wait accordingly, or you get API errors. So if you have many threads in your channel it will take a long time to load. – Erik Kalkoken May 05 '20 at 11:51
  • I think you have two ways to mitigate: asynchronous loading or pre-loading – Erik Kalkoken May 05 '20 at 11:51
  • I would really appreciate an example or some help here on first how to fetch the threaded replies according to their parent messages and how to do the asynchronous or pre-loading. – Makis Kahrimanidis May 05 '20 at 11:57
  • I am happy to help, but the scope of your question goes a bit beyond one question. But I can give you some pointers. – Erik Kalkoken May 05 '20 at 12:13
  • One of my apps does all that you need. It will fetch messages from channels incl. threads. For fetching all message from a channel incl. paging and rate limiting: [_fetch_messages_from_channel()](https://github.com/ErikKalkoken/slackchannel2pdf/blob/master/slackchannel2pdf/slackchannel2pdf.py#L347) – Erik Kalkoken May 05 '20 at 12:16
  • The same module also has _fetch_threads_from_messages() to fetch threads – Erik Kalkoken May 05 '20 at 12:17
  • Thanks for the comments mate, but like i said i m a newbie so didnt get much of your code. Thanks for the effort tho :) – Makis Kahrimanidis May 05 '20 at 15:21
  • No worries. You can see I put sleep() after each request to ensure I adhere to the rate limit. Also you need to deal with paging when requesting message from both conversations.history and conversations.replies. – Erik Kalkoken May 05 '20 at 15:49
  • Here is another example how to load messages form a channel (no threads though): https://stackoverflow.com/a/56755815/4379151 – Erik Kalkoken May 06 '20 at 18:25

0 Answers0