Skip to content

Commit f603f5b

Browse files
authored
Merge pull request #29 from green-api/dev
Some fixes
2 parents 526346a + 0da56ad commit f603f5b

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ business logic of your system.
185185
| `sending.sendListMessage` | The method is designed to send a message with a selection button from a list of values to a personal or group chat | [SendListMessage](https://green-api.com/en/docs/api/sending/SendListMessage/) |
186186
| `sending.sendFileByUpload` | The method is designed to send a file loaded through a form (form-data) | [SendFileByUpload](https://green-api.com/en/docs/api/sending/SendFileByUpload/) |
187187
| `sending.sendFileByUrl` | The method is designed to send a file downloaded via a link | [SendFileByUrl](https://green-api.com/en/docs/api/sending/SendFileByUrl/) |
188+
| `sending.uploadFile` | The method is designed to upload a file to the cloud storage, which can be sent using the sendFileByUrl method | [UploadFile](https://green-api.com/en/docs/api/sending/UploadFile/) |
188189
| `sending.sendLocation` | The method is designed to send a geolocation message | [SendLocation](https://green-api.com/en/docs/api/sending/SendLocation/) |
189190
| `sending.sendContact` | The method is for sending a message with a contact | [SendContact](https://green-api.com/en/docs/api/sending/SendContact/) |
190191
| `sending.sendLink` | The method is designed to send a message with a link that will add an image preview, title and description | [SendLink](https://green-api.com/en/docs/api/sending/SendLink/) |

README_RUS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ onEvent - ваша функция, которая должен содержат
171171
| `sending.sendListMessage` | Метод предназначен для отправки сообщения с кнопкой выбора из списка значений в личный или групповой чат | [SendListMessage](https://green-api.com/docs/api/sending/SendListMessage/) |
172172
| `sending.sendFileByUpload` | Метод предназначен для отправки файла, загружаемого через форму (form-data) | [SendFileByUpload](https://green-api.com/docs/api/sending/SendFileByUpload/) |
173173
| `sending.sendFileByUrl` | Метод предназначен для отправки файла, загружаемого по ссылке | [SendFileByUrl](https://green-api.com/docs/api/sending/SendFileByUrl/) |
174+
| `sending.uploadFile` | Метод предназначен для загрузки файла в облачное хранилище, который можно отправить методом sendFileByUrl | [UploadFile](https://green-api.com/docs/api/sending/UploadFile/) |
174175
| `sending.sendLocation` | Метод предназначен для отправки сообщения геолокации | [SendLocation](https://green-api.com/docs/api/sending/SendLocation/) |
175176
| `sending.sendContact` | Метод предназначен для отправки сообщения с контактом | [SendContact](https://green-api.com/docs/api/sending/SendContact/) |
176177
| `sending.sendLink` | Метод предназначен для отправки сообщения со ссылкой, по которой будут добавлены превью изображения, заголовок и описание | [SendLink](https://green-api.com/docs/api/sending/SendLink/) |

whatsapp_api_client_python/tools/sending.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pathlib
33
from typing import Dict, List, Optional, TYPE_CHECKING, Union
44

5+
from requests import Session
6+
57
from ..response import Response
68

79
if TYPE_CHECKING:
@@ -163,14 +165,21 @@ def uploadFile(self, path: str) -> Response:
163165
file_name = pathlib.Path(path).name
164166
content_type = mimetypes.guess_type(file_name)[0]
165167

166-
files = {"file": (file_name, open(path, "rb"), content_type)}
167-
168-
return self.api.request(
169-
"POST", (
170-
"{{media}}/waInstance{{idInstance}}/"
171-
"uploadFile/{{apiTokenInstance}}"
172-
), files=files
173-
)
168+
try:
169+
with open(path, "rb") as file:
170+
with Session() as session:
171+
response = session.request(
172+
method="POST",
173+
url=(
174+
f"{self.api.media}/waInstance{self.api.idInstance}/"
175+
f"uploadFile/{self.api.apiTokenInstance}"
176+
),
177+
data=file.read(),
178+
headers={"Content-Type": content_type}
179+
)
180+
except Exception as error:
181+
return Response(None, f"Other error occurred: {error}.")
182+
return Response(response.status_code, response.text)
174183

175184
def sendLocation(
176185
self,

0 commit comments

Comments
 (0)