Skip to content

Instantly share code, notes, and snippets.

@benoit-cty
Last active August 25, 2023 16:32
Show Gist options
  • Save benoit-cty/a5855dea9a4b7af03f1f53c07ee48d3c to your computer and use it in GitHub Desktop.
Save benoit-cty/a5855dea9a4b7af03f1f53c07ee48d3c to your computer and use it in GitHub Desktop.

Revisions

  1. benoit-cty revised this gist Apr 16, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions slack_backup.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    '''
    Script to archive Slack messages from a channel list.
    You have to create a Slack Bot and invite him to private channels.
    View https://github.com/docmarionum1/slack-archive-bot for how to configure your account.
    Then provide the bot token to this script with the list of channels.
    '''

  2. benoit-cty renamed this gist Apr 16, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. benoit-cty created this gist Apr 16, 2021.
    55 changes: 55 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    '''
    Script to archive Slack messages from a channel list.
    You have to create a Slack Bot and invite him to private channels.
    Then provide the bot token to this script with the list of channels.
    '''

    TOKEN='xoxb-xxxxx-xxxxxx-xxxxxxxxxxx'

    channels = {
    'général': 'CSNLXXXXXX',
    'veille': 'G01XXXXXX'
    }

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

    # 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=TOKEN)
    # Store conversation history
    conversation_history = []


    def backup_channel(channel_name, channel_id):
    '''
    :channel_id: ID of the channel you want to send the message to
    '''
    try:
    print('Getting messages from', channel_name)
    # Call the conversations.history method using the WebClient
    # conversations.history returns the first 100 messages by default
    # These results are paginated
    result = client.conversations_history(channel=channel_id)
    all_message = []
    all_message += result["messages"]
    while result['has_more']:
    print("\tGetting more...")
    result = client.conversations_history(channel=channel_id, cursor=result['response_metadata']['next_cursor'])
    all_message += result["messages"]
    # Save to disk
    filename = f'{channel_name}.json'
    print(f' We have downloaded {len(all_message)} messages from {channel_name}.')
    print(' Saving to', filename)
    with open(filename, 'w') as outfile:
    json.dump(all_message, outfile)
    except SlackApiError as e:
    print("Error using conversation: {}".format(e))


    if __name__ == "__main__":
    # Iterate channels
    for chan_name, chan_id in channels.items():
    backup_channel(chan_name, chan_id)