|
| 1 | +# Copyright 2021, Slack Technologies, LLC. All rights reserved. |
| 2 | + |
| 3 | +import logging, os, json |
| 4 | +from slack_discovery_sdk import DiscoveryClient # type: ignore |
| 5 | +from utils import export_json_to_file # type: ignore |
| 6 | + |
| 7 | +logging.basicConfig(level=logging.DEBUG) |
| 8 | + |
| 9 | +enterprise_token = os.environ["SLACK_DISCOVERY_SDK_TEST_ENTERPRISE_TOKEN"] |
| 10 | + |
| 11 | +client = DiscoveryClient(token=enterprise_token) |
| 12 | + |
| 13 | +CONVERSATIONS_HISTORY_FILENAME = "discovery_conversations" |
| 14 | + |
| 15 | +# CALL PATTERN OVERVIEW - User Based Discovery with Edits |
| 16 | +# Step 1 - We need a userID. This will be the user which we will query to see their conversations. |
| 17 | +# Step 2 - Retrieve a list of conversation IDs a user is a member of using discovery.user.conversations |
| 18 | +# Step 3 - Retrieve the message history for each conversation returned in Step 2 using discovery.conversations.history |
| 19 | +# Step 4 - If the conversation has edits call the discovery.conversations.edits endpoint to see edit history |
| 20 | + |
| 21 | +# Step 1 - Get the user ID of the user you'd like to see conversation history from |
| 22 | + |
| 23 | +auth_test = client.auth_test() |
| 24 | + |
| 25 | +# At this point, you should change the user_id variable to be the user_id of whichever user |
| 26 | +# you'd like to see the conversation of. The auth_test is just used for testing purposes here. |
| 27 | +user_id = auth_test["user_id"] |
| 28 | + |
| 29 | +# Step 2 - Use `discovery.user.conversations` to retrieve a list of conversation ID's |
| 30 | +# a user is a member of. user_id should be set to something *other* than auth_test at this point. |
| 31 | + |
| 32 | +list_of_conversations = client.discovery_user_conversations(user=user_id, limit=500) |
| 33 | + |
| 34 | +for conversation in list_of_conversations["channels"]: |
| 35 | + |
| 36 | + channel_id = conversation["id"] |
| 37 | + team_id = conversation["team_id"] |
| 38 | + |
| 39 | + # Step 3 - Retrieve the message history for each conversation returned in Step 2 |
| 40 | + # using discovery.conversations.history |
| 41 | + |
| 42 | + channel_conversation = client.discovery_conversations_history( |
| 43 | + channel=channel_id, team=team_id |
| 44 | + ) |
| 45 | + |
| 46 | + channel_conversation_json = json.dumps(channel_conversation.body, indent=4) |
| 47 | + |
| 48 | + # Step 4 - check if the conversation has edits, and if it does, call the conversation.edits endpoint |
| 49 | + if channel_conversation.body["has_edits"] is False: |
| 50 | + export_json_to_file( |
| 51 | + new_items=channel_conversation_json, |
| 52 | + base_dir="./example-outputs/", |
| 53 | + logs_type=CONVERSATIONS_HISTORY_FILENAME, |
| 54 | + channel_id=channel_id, |
| 55 | + user_id=user_id, |
| 56 | + ) |
| 57 | + else: |
| 58 | + edits_response = client.discovery_conversations_edits( |
| 59 | + channel=channel_id, team=team_id |
| 60 | + ) |
| 61 | + # If has_edits is true, merge the edits into the conversation history file. |
| 62 | + channel_conversation.body["edits"] = edits_response["edits"] |
| 63 | + edits_response_json = json.dumps(channel_conversation.body, indent=4) |
| 64 | + export_json_to_file( |
| 65 | + new_items=edits_response_json, |
| 66 | + base_dir="./example-outputs/", |
| 67 | + logs_type=CONVERSATIONS_HISTORY_FILENAME, |
| 68 | + channel_id=channel_id, |
| 69 | + user_id=user_id, |
| 70 | + ) |
0 commit comments