[英]How read slack channel messages using python-slackclient
我想從我的松弛頻道“general”獲取消息,可能帶有檢索最后 50 條消息之類的參數。
我檢查了文檔,有發送消息、列出頻道、離開頻道、查找頻道 ID 等所有內容。但我沒有找到任何可以幫助我使用該頻道 ID“一次”獲取頻道消息的東西。
這個 function 在 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))
獲得頻道 ID 后,您可以使用 api_calls 檢索這樣的消息
history = client.api_call(api_method='conversations.history',data={'channel':'CHANNEL_ID_HERE'})
print(history)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.