Skip to main content

Hey, I want to get the parent message in a slack thread using a thread id.  I have never worked with API's but today I gave it a try.  I got this code from chat gpt

import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

def main(input_data):
    # Retrieve the Slack API token from the environment variables
    SLACK_API_TOKEN = os.environn"SLACK_API_TOKEN"]

    # Initialize the WebClient with the API token
    client = WebClient(token=SLACK_API_TOKEN)

    try:
        # Call the conversations.history method using the WebClient
        # The client passes the token you included in initialization    
        result = client.conversations_history(
            channel=input_data"Thread_ID"],
            inclusive=True,
            oldest="1610144875.000600",
            limit=1
        )

        # Get the first message in the result
        message = result "messages"]g0]

        # Print the message text to the console
        print(message#"text"])

    except SlackApiError as e:
        print(f"Error: {e}")



I used this python code found there but I got Traceback error, I also didn't know how to get the slack api token so didn’t add them there, could anyone tell me how to do them and what am doing wrong 
 

 

Hi @Fahad 

Good question.

Limitations with Code steps

  • You cannot require external libraries or install libraries commonly referred to as "pip modules". Only the standard Python library and requests are available in the Code app and requests is already included in the namespace.

Hi @Fahad 

Good question.

Limitations with Code steps

  • You cannot require external libraries or install libraries commonly referred to as "pip modules". Only the standard Python library and requests are available in the Code app and requests is already included in the namespace.

This post has been edited by a moderator to remove personal information. Please remember that this is a public forum and to remove any sensitive information prior to posting.

What would you suggest I do then to get the parent message of replies a thread?

I tried to use webhooks and this is how it looks like
 

I get a “channel not found” error, why’s that?


@Fahad 

Please don’t expose your API key in screenshots!


@Fahad

Try this Slack API endpoint instead: https://api.slack.com/methods/conversations.history#retrieving-a-single-message

 

Try setting the content type header: https://api.slack.com/methods/conversations.replies

 

This TS is of a thread as indicated by the .#####

 


​​​​​​Thanks, have updated the webhook based on your suggestion and this is what it looks like now. Is everything right here?


 

This TS is of a thread as indicated by the .#####

Sorry but I didn’t quite understand what you meant by the “TS starts with #”, I didn’t find any that begins with that. Here’s the TS related fields, which one should I choose?

 


@Fahad 

Thread TS will be the parent Message.


@Fahad

Thread TS will be the parent Message.

Thanks, I have used “Thread TS” as TS.

I get like a cluttered text as output, this is how it is. Did I do something wrong?

 


@Fahad 

That is not the correct Slack API endpoint.

Make sure to reference the specs in the Slack API docs: https://api.slack.com/methods/conversations.history

 


@Fahad

That is not the correct Slack API endpoint.

Make sure to reference the specs in the Slack API docs: https://api.slack.com/methods/conversations.history

 

Thanks, I have used this URL “https://slack.com/api/conversations.replies”

I went through the API docs and tried to replicate things but I still get error. I am creating API calls for the first time so I must be doing something wrong here
 

 


@Fahad 

 


This post has been edited by a moderator to remove personal information. Please remember that this is a public forum and to remove any sensitive information prior to posting.

Thanks for sharing that and being patient with me,

I did exactly as you mentioned in the screenshot and also gone through the docs again, got an error again and now it says “channel not found”. 

This is the input

I am not sure what am doing wrong here


@Fahad 

Again, please don’t expose your app API keys in screenshots!


@Fahad 

Please post screenshots with the encountered error.

Can you confirm the Channel ID exists with screenshots from Slack?

 


Please post screenshots with the encountered error.

Sure, Here’s the screenshot of error I get



 

@Fahad

Please post screenshots with the encountered error.

Can you confirm the Channel ID exists with screenshots from Slack?

 

Yes, the API docs said that the error happened because of invalid channel value so I checked the slack ID before itself and it’s the same as the number in zapier
 

 


@Fahad 

Check the required scope permissions are selected.

 


@Troy Tessalone I apologies but how can I do that, the bot I installed is called the “My Powerfull app”

I click on it and nothing in particular shows up, what can I do to enable those permissions.

 


@Fahad 

In the Slack API: https://api.slack.com/scopes

Go to Your Apps

Then under Features, OAuth & Permissions

 


@Troy Tessalone Thank you! I added the tokens and added the app to the private channel and the data shows up.

There is one problem though, instead of only brining in the parent message of a thread, it brings in the whole message history of the channel as line items.

I am planning to use loops and filters to get the message I need but is there a way the output of the webhook only shows one message?

 

 


@Fahad 

You shouldn’t have to do looping, if you follow these instructions.

 


@Fahad

You shouldn’t have to do looping, if you follow these instructions.

 

Hey @Troy Tessalone, I tried to follow the instructions but I just couldn't understand what most of them means in that image.

I tried to add inclusive and limit and still didn’t get the single message, here’s an image of the input in case you wanna check them and let me know what am doing wrong here