Pulling historical channel messages python

被刻印的时光 ゝ 提交于 2020-05-08 16:47:49

问题


I am attempting to create a small dataset by pulling messages/responses from a slack channel I am a part of. I would like to use python to pull the data from the channel however I am having trouble figuring out my api key. I have created an app on slack but I am not sure how to find my api key. I see my client secret, signing secret, and verification token but can't find my api key

Here is a basic example of what I believe I am trying to accomplish:

import slack
sc = slack.SlackClient("api key")
sc.api_call(
  "channels.history",
  channel="C0XXXXXX"
)

I am willing to just download the data manually if that is possible as well. Any help is greatly appreciated.


回答1:


messages

See below for is an example code on how to pull messages from a channel in Python.

  • It uses the official Python Slack library and calls conversations_history with paging. It will therefore work with any type of channel and can fetch large amounts of messages if needed.
  • The result will be written to a file as JSON array.
  • You can specify channel and max message to be retrieved

threads

Note that the conversations.history endpoint will not return thread messages. Those have to be retrieved additionaly with one call to conversations.replies for every thread you want to retrieve messages for.

Threads can be identified in the messages for each channel by checking for the threads_ts property in the message. If it exists there is a thread attached to it. See this page for more details on how threads work.

IDs

This script will not replace IDs with names though. If you need that here are some pointers how to implement it:

  • You need to replace IDs for users, channels, bots, usergroups (if on a paid plan)
  • You can fetch the lists for users, channels and usergroups from the API with users_list, conversations_list and usergroups_list respectively, bots need to be fetched one by one with bots_info (if needed)
  • IDs occur in many places in messages:
    • user top level property
    • bot_id top level property
    • as link in any property that allows text, e.g. <@U12345678> for users or <#C1234567> for channels. Those can occur in the top level text property, but also in attachments and blocks.

Example code

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
    )



回答2:


This is using the slack webapi. You would need to install requests package. This should grab all the messages in channel. You need a token which can be grabbed from apps management page. And you can use the getChannels() function. Once you grab all the messages you will need to see who wrote what message you need to do id matching(map ids to usernames) you can use getUsers() functions. Follow this https://api.slack.com/custom-integrations/legacy-tokens to generate a legacy-token if you do not want to use a token from your app.

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


来源:https://stackoverflow.com/questions/56744339/pulling-historical-channel-messages-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!