我试图通过从我参与的松弛渠道中提取消息/响应来创建一个小的数据集。我想使用python从通道中提取数据,但是我在弄清楚自己的api密钥时遇到了麻烦。我已经在松弛状态下创建了一个应用程序,但不确定如何找到我的api密钥。我看到了我的客户机密,签名机密和验证令牌,但是找不到我的api密钥
这是我相信自己要实现的目标的基本示例:
import slack
sc = slack.SlackClient("api key")
sc.api_call(
"channels.history",
channel="C0XXXXXX"
)
如果可能的话,我也愿意手动下载数据。任何帮助将不胜感激。
答案 0 :(得分:2)
这是使用松弛的webapi。您将需要安装请求包。这应该获取通道中的所有消息。您需要可以从应用程序管理页面中获取的令牌。您可以使用getChannels()函数。抓住所有消息后,您需要查看谁写了什么消息才能进行ID匹配(将ID映射到用户名),然后可以使用getUsers()函数。如果您不想在应用程序中使用令牌,请遵循此 https://api.slack.com/custom-integrations/legacy-tokens 生成旧令牌。
def getMessages(token, channelId):
print("Getting Messages")
# this function get all the messages from the slack team-search channel
# it will only get all the messages from the team-search channel
slack_url = "https://slack.com/api/conversations.history?token=" + token + "&channel=" + channelId
messages = requests.get(slack_url).json()
return messages
def getChannels(token):
'''
function returns an object containing a object containing all the
channels in a given workspace
'''
channelsURL = "https://slack.com/api/conversations.list?token=%s" % token
channelList = requests.get(channelsURL).json()["channels"] # an array of channels
channels = {}
# putting the channels and their ids into a dictonary
for channel in channelList:
channels[channel["name"]] = channel["id"]
return {"channels": channels}
def getUsers(token):
# this function get a list of users in workplace including bots
users = []
channelsURL = "https://slack.com/api/users.list?token=%s&pretty=1" % token
members = requests.get(channelsURL).json()["members"]
return members
答案 1 :(得分:1)
有关如何从Python通道中提取消息的示例代码,请参见下文。
conversations_history
分页。因此它将与
任何类型的频道,如果
需要。请注意,conversations.history
端点将不返回线程消息。必须为每个您要为其检索消息的线程调用conversations.replies
来额外地检索这些消息。
可以通过检查消息中的threads_ts
属性来在每个通道的消息中标识线程。如果存在,则附加一个线程。有关线程如何工作的更多详细信息,请参见此page。
该脚本不会将ID替换为名称。如果需要的话,这里有一些如何实现它的指针:
users_list
,conversations_list
和usergroups_list
从API获取用户,频道和用户组的列表,需要通过bots_info逐个获取机器人(如果需要) )<@U12345678>
(用于用户)或<#C1234567>
(用于频道)。这些可以出现在顶级text
属性中,也可以出现在附件和块中。示例代码
import os
import slack
import json
from time import sleep
CHANNEL = "C12345678"
MESSAGES_PER_PAGE = 200
MAX_MESSAGES = 1000
# init web client
client = slack.WebClient(token=os.environ['SLACK_TOKEN'])
# get first page
page = 1
print("Retrieving page {}".format(page))
response = client.conversations_history(
channel=CHANNEL,
limit=MESSAGES_PER_PAGE,
)
assert response["ok"]
messages_all = response['messages']
# get additional pages if below max message and if they are any
while len(messages_all) + MESSAGES_PER_PAGE <= MAX_MESSAGES and response['has_more']:
page += 1
print("Retrieving page {}".format(page))
sleep(1) # need to wait 1 sec before next call due to rate limits
response = client.conversations_history(
channel=CHANNEL,
limit=MESSAGES_PER_PAGE,
cursor=response['response_metadata']['next_cursor']
)
assert response["ok"]
messages = response['messages']
messages_all = messages_all + messages
print(
"Fetched a total of {} messages from channel {}".format(
len(messages_all),
CHANNEL
))
# write the result to a file
with open('messages.json', 'w', encoding='utf-8') as f:
json.dump(
messages_all,
f,
sort_keys=True,
indent=4,
ensure_ascii=False
)