diff --git a/whatsapp_api_client_python/API.py b/whatsapp_api_client_python/API.py index 7cfe050..49a2352 100644 --- a/whatsapp_api_client_python/API.py +++ b/whatsapp_api_client_python/API.py @@ -27,6 +27,7 @@ class GreenApi: media: str idInstance: str apiTokenInstance: str + useDirectHost: bool def __init__( self, @@ -34,6 +35,7 @@ def __init__( apiTokenInstance: str, debug_mode: bool = False, raise_errors: bool = False, + useDirectHost: bool = False, host: str = "https://api.green-api.com", media: str = "https://media.green-api.com", host_timeout: float = 180, # sec per retry @@ -43,8 +45,10 @@ def __init__( self.media = media self.debug_mode = debug_mode self.raise_errors = raise_errors + self.useDirectHost = useDirectHost + self.host, self.media = self._determine_host(idInstance) if self.useDirectHost else (host, media) - # Change default values in init() if required + # Change default values in init() if required (slow connection) self.host_timeout = host_timeout self.media_timeout = media_timeout @@ -69,6 +73,18 @@ def __init__( self.logger = logging.getLogger("whatsapp-api-client-python") self.__prepare_logger() + def _determine_host(self, idInstance: str) -> str: + mapping = { + "1103": ("https://1103.api.green-api.com", "https://1103.media.green-api.com"), + "1104": ("https://1103.api.green-api.com", "https://1103.media.green-api.com"), + "5700": ("https://5700.api.green-api.com", "https://5700.media.green-api.com"), + "7103": ("https://7103.api.greenapi.com", "https://7103.media.greenapi.com"), + "7105": ("https://7105.api.greenapi.com", "https://7105.media.greenapi.com"), + "77": ("https://7700.api.greenapi.com", "https://7700.media.greenapi.com"), + "7": ("https://api.greenapi.com", "https://media.greenapi.com") + } + return next((url for key, url in mapping.items() if idInstance.startswith(key)), ("https://api.green-api.com", "https://media.green-api.com")) + def request( self, method: str, @@ -213,4 +229,4 @@ def request( url = url.replace("{{partnerToken}}", self.partnerToken) - return super().request(method, url, payload, files) \ No newline at end of file + return super().request(method, url, payload, files)