如何使用python slackclient读取松弛通道消息

如何使用python slackclient读取松弛通道消息,python,slack,slack-api,Python,Slack,Slack Api,我想从我的空闲通道“general”获取消息,可能带有参数,如retrieve last 50 messages 我检查了一下,所有的东西都有,比如发送消息、列出频道、离开频道、查找频道ID等等。但我没有找到任何东西可以帮助我使用该频道ID“一次”获取频道消息 此函数在python slackclient中可用。或任何解决方法?您正在寻找一种方法,该方法提取对话的最后100个消息事件。问题很简单: import os # Import WebClient from Python SDK (git

我想从我的空闲通道“general”获取消息,可能带有参数,如retrieve last 50 messages

我检查了一下,所有的东西都有,比如发送消息、列出频道、离开频道、查找频道ID等等。但我没有找到任何东西可以帮助我使用该频道ID“一次”获取频道消息

此函数在python slackclient中可用。或任何解决方法?

您正在寻找一种方法,该方法提取对话的最后100个消息事件。问题很简单:

import os
# Import WebClient from Python SDK (github.com/slackapi/python-slack-sdk)
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

# WebClient insantiates a client that can call API methods
# When using Bolt, you can use either `app.client` or the `client` passed to listeners.
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
# Store conversation history
conversation_history = []
# ID of the channel you want to send the message to
channel_id = "C12345"

try:
    # Call the conversations.history method using the WebClient
    # conversations.history returns the first 100 messages by default
    # These results are paginated, see: https://api.slack.com/methods/conversations.history$pagination
    result = client.conversations_history(channel=channel_id)

    conversation_history = result["messages"]

    # Print results
    logger.info("{} messages found in {}".format(len(conversation_history), id))

except SlackApiError as e:
    logger.error("Error creating conversation: {}".format(e))

在获得通道ID后,您可以使用api_调用来检索如下消息

history = client.api_call(api_method='conversations.history',data={'channel':'CHANNEL_ID_HERE'})
print(history)