Best answer

How to get a parent message of a Slack thread in Zapier?


Userlevel 1
Badge +1

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.environ["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"][0]

        # 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 
 

 

icon

Best answer by Troy Tessalone 3 January 2023, 23:07

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

20 replies

Userlevel 7
Badge +14

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.
Userlevel 1
Badge +1

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?

Userlevel 7
Badge +14

@Fahad 

Please don’t expose your API key in screenshots!

Userlevel 7
Badge +14

@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 .#####

 

Userlevel 1
Badge +1

​​​​​​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?

 

Userlevel 7
Badge +14

@Fahad 

Thread TS will be the parent Message.

Userlevel 1
Badge +1

@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?

 

Userlevel 7
Badge +14

@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

 

Userlevel 1
Badge +1

@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
 

 

Userlevel 7
Badge +14

@Fahad 

 

Userlevel 1
Badge +1

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

Userlevel 7
Badge +14

@Fahad 

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

Userlevel 7
Badge +14

@Fahad 

Please post screenshots with the encountered error.

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

 

Userlevel 1
Badge +1

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
 

 

Userlevel 7
Badge +14

@Fahad 

Check the required scope permissions are selected.

 

Userlevel 1
Badge +1

@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.

 

Userlevel 7
Badge +14

@Fahad 

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

Go to Your Apps

Then under Features, OAuth & Permissions

 

Userlevel 1
Badge +1

@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?

 

 

Userlevel 7
Badge +14

@Fahad 

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

 

Userlevel 1
Badge +1

@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