Skip to content

Commit 8ca8bfc

Browse files
authored
Merge pull request #22 from green-api/SW-961
Added UploadFile method
2 parents 1979746 + 6f93bb9 commit 8ca8bfc

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from os.path import basename
2+
from urllib.parse import urlparse
3+
4+
from whatsapp_api_client_python import API
5+
6+
greenAPI = API.GreenApi(
7+
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
8+
)
9+
10+
11+
def main():
12+
upload_file_response = greenAPI.sending.uploadFile(
13+
"data/green-api-logo_2.png"
14+
)
15+
if upload_file_response.code == 200:
16+
print(upload_file_response.data)
17+
18+
url_file = upload_file_response.data["urlFile"]
19+
20+
url = urlparse(url_file)
21+
file_name = basename(url.path)
22+
23+
send_file_by_url_response = greenAPI.sending.sendFileByUrl(
24+
"11001234567@c.us", url_file, file_name
25+
)
26+
if send_file_by_url_response.code == 200:
27+
print(send_file_by_url_response.data)
28+
else:
29+
print(send_file_by_url_response.error)
30+
else:
31+
print(upload_file_response.error)
32+
33+
34+
if __name__ == '__main__':
35+
main()

tests/test_methods.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def sending_methods(self):
101101
api.sending.sendListMessage("", "", "", []),
102102
api.sending.sendFileByUpload("", path),
103103
api.sending.sendFileByUrl("", "", ""),
104+
# api.sending.uploadFile(""),
104105
api.sending.sendLocation("", 0.0, 0.0),
105106
api.sending.sendContact("", {}),
106107
api.sending.sendLink("", ""),

whatsapp_api_client_python/tools/sending.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ def sendFileByUrl(
154154
), request_body
155155
)
156156

157+
def uploadFile(self, path: str) -> Response:
158+
"""
159+
The method is designed to upload a file to the cloud storage,
160+
which can be sent using the sendFileByUrl method.
161+
"""
162+
163+
file_name = pathlib.Path(path).name
164+
content_type = mimetypes.guess_type(file_name)[0]
165+
166+
files = {"file": (file_name, open(path, "rb"), content_type)}
167+
168+
return self.api.request(
169+
"POST", (
170+
"{{host}}/waInstance{{idInstance}}/"
171+
"uploadFile/{{apiTokenInstance}}"
172+
), files=files
173+
)
174+
157175
def sendLocation(
158176
self,
159177
chatId: str,

0 commit comments

Comments
 (0)