我想从闲暇频道“常规”中获取消息,可能带有诸如检索最近50条消息之类的参数。
我检查了文档,发现所有内容,例如发送消息,列出频道,离开频道,查找频道ID等。但是我没有发现任何可以帮助我使用该频道ID一次获得频道消息的东西。
这个功能在python-slackclient中可用吗?或任何解决方法?
您正在寻找该conversations.history
方法,该方法可以拉出对话的最后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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句