0

I'm trying to create a python script that reads all messages from a channel (e.g. Retrieving conversation history). However, there is no implementation of that directly in this SDK. Closest thing is listing all channels.

There is a way to call any api method using this general form (also shown below). I cannot figure out the syntax for passing arguments with the "conversations.history" method found here. My current try is at the bottom.

response = client.api_call(
  api_method='chat.postMessage',
  json={'channel': '#random','text': "Hello world!"}
)

##########

response = client.api_call(
  api_method='conversations.history',
  token=slack_token,
  channel="C0XXXXXXXXX"
)

This search on the GitHub may also help for those inclined.

nate
  • 101
  • 1
  • 2
    Does this answer your question? [Pulling historical channel messages python](https://stackoverflow.com/questions/56744339/pulling-historical-channel-messages-python) – Jai Pandya Sep 24 '20 at 21:50
  • 1
    Yes! Thank you @JaiPandya. I figured the answer was out there somewhere I just could not find it with what I was searching. I also dug into the docs more and found https://github.com/slackapi/python-slackclient/blob/c758d7f03667e711f7403060fa4002418fd031fb/slack/web/base_client.py#L72 really helpful. – nate Sep 24 '20 at 21:56

2 Answers2

0

You can use the following method:

client.conversations_history(
    channel=CHANNEL,
    limit=MESSAGES_PER_PAGE,
)

This is explained in another answer here.

Jai Pandya
  • 2,129
  • 18
  • 29
0

I had the same issue. The problem is their example doesn't include any params, just the json body of a POST message which might give the reader the impression that all params go into json=.

What you need is:

response = client.api_call(
  api_method='conversations.history',
  params={'token': slack_token,
          'channel': "C0XXXXXXXXX"}
)

You need to pass post body as json= and query params as params= I received the following error:

The request to the Slack API failed. (url: https://www.slack.com/api/conversations.members)
The server responded with: {'ok': False, 'error': 'invalid_arguments', 'response_metadata': {'messages': ['[ERROR] missing required field: channel']}}

Here is a definition of api_call in base_client.py https://github.com/slackapi/python-slack-sdk/blob/6ab8bd2fcdcabb55e722ee520e6946c5b0b43f1d/slack_sdk/web/base_client.py#L93

darKoram
  • 1,113
  • 1
  • 14
  • 25