Skip to content

Commit 0467feb

Browse files
committed
Update local_file_upload.py
Add simplify the script, add explanatory comments, and make the script structure consistent with the other example scripts.
1 parent d46efdb commit 0467feb

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

examples/local_file_upload.py

100644100755
Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,73 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""Demo script showing how to upload a local file.
4+
5+
A simple script showing how to upload a local file when creating a message in
6+
a Webex Teams space.
7+
8+
You upload a file by using the `files=` parameter of the
9+
`WebexTeamsAPI.messages.create()` method, which expects to receive a list
10+
containing a single string with the path to file to be attached to the created
11+
message (Example: `files=["./image.png"]`). The files parameter receives a
12+
list to allow for future expansion; however today, only one file may be
13+
included when creating a message via the Webex Teams APIs.
14+
15+
The WebexTeamsSDK natively retrieves your Webex Teams access token from the
16+
WEBEX_TEAMS_ACCESS_TOKEN environment variable. You must have this environment
17+
variable set to run this script.
18+
19+
20+
Copyright (c) 2016-2019 Cisco and/or its affiliates.
21+
22+
Permission is hereby granted, free of charge, to any person obtaining a copy
23+
of this software and associated documentation files (the "Software"), to deal
24+
in the Software without restriction, including without limitation the rights
25+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26+
copies of the Software, and to permit persons to whom the Software is
27+
furnished to do so, subject to the following conditions:
28+
29+
The above copyright notice and this permission notice shall be included in all
30+
copies or substantial portions of the Software.
31+
32+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
SOFTWARE.
39+
"""
40+
41+
42+
from __future__ import print_function
43+
144
import os
2-
import sys
3-
from webexteamssdk import WebexTeamsAPI, ApiError
445

5-
cwd = os.path.abspath(os.path.dirname('.'))
6-
sys.path.append(cwd)
7-
testfile = '/'+cwd+'/test.png' #expected format: '//Users/userid/dir/test.png'
46+
from webexteamssdk import WebexTeamsAPI
47+
48+
49+
ROOM_ID = "<your_room_id>"
50+
FILE_PATH = "<the_path_to_the_local_file>"
51+
52+
53+
# Create a WebexTeamsAPI connection object; uses your WEBEX_TEAMS_ACCESS_TOKEN
54+
api = WebexTeamsAPI()
55+
56+
57+
# Let's make sure the file exists
58+
if not os.path.isfile(FILE_PATH):
59+
print("ERROR: File {} does not exist.".format(FILE_PATH))
60+
61+
62+
# Not a requirement but to be completely clear let's make sure we are using
63+
# an absolute path.
64+
abs_path = os.path.abspath(FILE_PATH)
65+
866

9-
file_list = []
10-
file_list.append(testfile)
67+
# The files parameter expects to receive a list containing a single string with
68+
# the path to the file to be uploaded.
69+
file_list = [abs_path]
1170

12-
token = 'your_bot_token'
13-
api = WebexTeamsAPI(access_token=token)
14-
headers = {'Authorization': 'Bearer '+token}
1571

16-
api.messages.create(roomId='your_room_id', files=file_list)
72+
# It takes just a single statement to upload the file and create the message
73+
api.messages.create(roomId=ROOM_ID, files=file_list)

0 commit comments

Comments
 (0)