How to retrieve all threads which have replies since a given timestamp?
Asked Answered
G

3

2

I am ideally looking for an API that returns all the messages posted(including replies) since a given timestamp.

conversations.history is supposed to be the one I should be using, but it does not return replies, it only returns parent message (only if the timestamp of the parent message satisfies the "oldest" param I supply - i.e. if the oldest supplied in the query is later than parent's ts but earlier than replies, neither parent nor replies will be returned).

Hence, I am trying to find if there is any other API that returns the threads based on "oldest" timestamp. i.e. all the threads which have replies since a given timestamp.

Note: I looked at conversations.replies, it is only useful if you know which thread's replies you are fetching.

Grizzly answered 15/7, 2021 at 12:31 Comment(0)
N
0

Currently there is no API to do what you aspire to do.

The best work around is manually fetching all threads data in-memory and then applying filter.

Namecalling answered 16/7, 2021 at 1:34 Comment(0)
V
0

Did you find an alternative solution to this question? I have the same use case and when contacting Slack support I received the same response that we need to use the combination of conversations.history & conversations.replies. This will be quite an intensive and continuously growing number of calls if we need to call conversations.replies for all threaded messages just to filter out the timestamps that fit the date range. This would be catastrophic in the long run.

Ideally slack need to update conversations.replies API to support getting all replies between oldest & latest parameter just like in history.

Another alternative I am considering is to change the implementation and use the Events API instead of the Web Client API and use queueing to store all incoming messages then this will make sure that all messages are captured and stored then apply the required filters.

Valaree answered 7/6, 2022 at 1:39 Comment(2)
no, i'm afraid.Grizzly
@Valaree you mean you would have to call conversations.replies on each message returned by conversations.history, right? i.e. first determine which message is actually a thread, and only then filter for timestamps. Very large OOrford
R
0

I needed this for a slack related project to feed channel history to gemini and came up with this:

        channel_history = get_channel_messages(
            slack_client, channel_id, oldest=search_timestamp
        )
        thread_history = []
        for history_message in channel_history:
            if "thread_ts" in history_message:
                thread_messages = get_message_thread(
                    slack_client,
                    channel_id=channel_id,
                    thread_ts=history_message["thread_ts"],
                )
                thread_history.extend(thread_messages)
        # add the threads
        channel_history.extend(thread_history)
        # sort it all by ts
        sorted_history = sorted(channel_history, key=itemgetter("ts"))

where the get_channel_messages and get_message_thread are thus:

def get_channel_messages(slack_client, channel_id, message_count=0, oldest=1):
# return conversation history
conversation_history = []

try:
    # Call the conversations.history method using the WebClient
    # conversations.history returns the first 100 messages by default
    # paginated, get the first page
    result = slack_client.conversations_history(
        channel=channel_id, limit=message_count, oldest=oldest
    )
    conversation_history = result["messages"] if "messages" in result else []
    while getValueByPath(result, "result.response_metadata.next_cursor"):
        logger.debug(
            f"CURSOR: {getValueByPath(result,'result.response_metadata.next_cursor')}"
        )
        result = slack_client.conversations_history(
            channel=channel_id,
            limit=message_count,
            cursor=result["response_metadata"]["next_cursor"],
            latest=result["messages"][1]["ts"],
        )
        conversation_history.extend(result["messages"])

    # results
    logger.debug(
        "{} messages found in {}".format(len(conversation_history), channel_id)
    )
    return conversation_history

except SlackApiError as e:
    logger.error("Error accessing history: {}".format(e))
def get_message_thread(slack_client, channel_id, thread_ts):
    thread_history = []
    try:
        # Call the conversations.replies method using the WebClient
        # conversations.history returns the first 100 messages by default
        # paginated, get the first page
        result = slack_client.conversations_replies(channel=channel_id, ts=thread_ts)
        thread_history = result["messages"] if "messages" in result else []
        while getValueByPath(result, "result.response_metadata.next_cursor"):
            logger.debug(
                f"CURSOR: {getValueByPath(result,'result.response_metadata.next_cursor')}"
            )
            result = slack_client.conversations_replies(
                channel=channel_id,
                ts=thread_ts,
                cursor=result["response_metadata"]["next_cursor"],
                latest=result["messages"][1]["ts"],
            )
            thread_history.extend(result["messages"])

        # results
        logger.debug("{} messages found in {}".format(len(thread_history), thread_ts))
        return thread_history

    except SlackApiError as e:
        logger.error("Error accessing history: {}".format(e))

You can see it in context in this open source project: https://github.com/jeffbryner/gcp-ai-slackbot/blob/main/code/source/main.py#L157

Retake answered 2/6 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.