-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
172 lines (128 loc) · 5 KB
/
Copy pathexample_usage.py
File metadata and controls
172 lines (128 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Example usage of the Slack File Uploader
This script demonstrates different ways to use the SlackFileUploader class.
"""
import os
from dotenv import load_dotenv
from slack_file_uploader import SlackFileUploader
def example_basic_upload():
"""Example 1: Basic file upload without sharing to a channel."""
print("\n" + "="*60)
print("Example 1: Basic Upload (No Channel Sharing)")
print("="*60)
load_dotenv()
token = os.getenv("SLACK_BOT_TOKEN")
uploader = SlackFileUploader(token)
# This uploads the file to Slack but doesn't share it to any channel
# The file will be private and only accessible via direct link
result = uploader.upload_file(
file_path="example_document.txt",
title="Example Document"
)
print(f"\nFile uploaded successfully!")
print(f"File ID: {result['files'][0]['id']}")
def example_upload_to_channel():
"""Example 2: Upload and share file to a specific channel."""
print("\n" + "="*60)
print("Example 2: Upload and Share to Channel")
print("="*60)
load_dotenv()
token = os.getenv("SLACK_BOT_TOKEN")
channel_id = os.getenv("SLACK_CHANNEL_ID")
uploader = SlackFileUploader(token)
# Upload and share to channel
result = uploader.upload_file(
file_path="example_document.txt",
title="Shared Document",
channel_id=channel_id
)
print(f"\nFile shared to channel successfully!")
def example_upload_with_comment():
"""Example 3: Upload with an initial comment."""
print("\n" + "="*60)
print("Example 3: Upload with Initial Comment")
print("="*60)
load_dotenv()
token = os.getenv("SLACK_BOT_TOKEN")
channel_id = os.getenv("SLACK_CHANNEL_ID")
uploader = SlackFileUploader(token)
# Upload with a message
result = uploader.upload_file(
file_path="example_document.txt",
title="Document with Comment",
channel_id=channel_id,
initial_comment="📄 Here's the document you requested. Please review and let me know if you have any questions!"
)
print(f"\nFile shared with comment successfully!")
def example_step_by_step():
"""Example 4: Manual step-by-step upload process."""
print("\n" + "="*60)
print("Example 4: Manual Step-by-Step Process")
print("="*60)
load_dotenv()
token = os.getenv("SLACK_BOT_TOKEN")
channel_id = os.getenv("SLACK_CHANNEL_ID")
uploader = SlackFileUploader(token)
file_path = "example_document.txt"
file_size = os.path.getsize(file_path)
# Step 1: Get upload URL
print("\nStep 1: Requesting upload URL...")
upload_url, file_id = uploader.get_upload_url("example_document.txt", file_size)
print(f" Upload URL: {upload_url[:50]}...")
print(f" File ID: {file_id}")
# Step 2: Upload file content
print("\nStep 2: Uploading file content...")
uploader.upload_file_content(upload_url, file_path)
print(" File content uploaded!")
# Step 3: Complete upload
print("\nStep 3: Completing upload and sharing...")
result = uploader.complete_upload(
file_id=file_id,
title="Step-by-Step Upload",
channel_id=channel_id,
initial_comment="This file was uploaded using the manual step-by-step process."
)
print(" Upload completed!")
print(f"\nFinal result: {result['files'][0]['name']} uploaded successfully!")
def create_sample_file():
"""Create a sample text file for testing."""
sample_content = """
Slack File Upload Assignment
=============================
This is a sample document demonstrating file upload to Slack using REST APIs.
Key Points:
- Modern API uses a 3-step process
- files.getUploadURLExternal to get upload URL
- POST file content to the URL
- files.completeUploadExternal to finalize
This approach is more scalable and handles large files better than the deprecated files.upload method.
"""
with open("example_document.txt", "w") as f:
f.write(sample_content.strip())
print("✓ Created example_document.txt for testing")
if __name__ == "__main__":
# Create a sample file if it doesn't exist
if not os.path.exists("example_document.txt"):
create_sample_file()
print("\n" + "="*60)
print("Slack File Uploader - Example Usage")
print("="*60)
# Check for required environment variables
load_dotenv()
if not os.getenv("SLACK_BOT_TOKEN"):
print("\n❌ Error: SLACK_BOT_TOKEN not found in .env file")
print("Please create a .env file with your Slack credentials")
exit(1)
# Run examples
try:
# Uncomment the examples you want to run:
# example_basic_upload()
# example_upload_to_channel()
# example_upload_with_comment()
example_step_by_step()
print("\n" + "="*60)
print("Examples completed successfully!")
print("="*60 + "\n")
except Exception as e:
print(f"\n❌ Error: {e}")
exit(1)