Skip to content

Commit

Permalink
fix: changed input error_handler to accept self instance as first par…
Browse files Browse the repository at this point in the history
…ameter
  • Loading branch information
AntonioVentilii committed Apr 8, 2024
1 parent e3e46a0 commit 49676fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ wheel
twine
requests
firestore-wrapper
pydub
ffmpeg-downloader
ffmpeg-python
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='whatsapp_wrapper',
version='0.1.0',
version='0.1.1',
author='Antonio Ventilii',
author_email='[email protected]',
license='MIT',
Expand All @@ -29,7 +29,10 @@
keywords='WhatsApp, Firestore, API, integration, messaging',
install_requires=[
'requests',
'firestore-wrapper'
'firestore-wrapper',
'pydub',
'ffmpeg-downloader',
'ffmpeg-python',
],
python_requires='>=3.8',
entry_points={
Expand Down
12 changes: 0 additions & 12 deletions whatsapp_wrapper/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@

LAST_API_VERSION = 'v19.0'

"""
When CONTEXT_AS_CHAIN is True, the user needs to reply to the last message in the conversation chain of answer and
replies, in order to continue the conversation. If the user sends a new message without replying, the conversation
chain will be reset.
When CONTEXT_AS_CHAIN is False and CONTEXT_HISTORY_DURATION is a positive non-null number, the bot considers the entire
conversation history as context for generating a reply, starting from the message that was CONTEXT_HISTORY_DURATION
seconds ago.
In all other cases, the bot will generate a reply without any context.
"""
CONTEXT_AS_CHAIN = True
CONTEXT_HISTORY_DURATION = 24 * 60 * 60 # 24 hours

CACHE_DIR = os.path.join(tempfile.gettempdir(), "whatsapp_wrapper_cache")
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
18 changes: 12 additions & 6 deletions whatsapp_wrapper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import time
import warnings
from typing import Any, Callable, Optional

import requests
from requests import Response
Expand All @@ -13,10 +12,8 @@
from .message_object import MessageObject
from .whatsapp_db import DatabaseConfig, WhatsAppDB, configure_database

ErrorHandlerType = Callable[[Response, dict], Optional[Any]]


def _default_error_handler(response: Response, data: dict):
def _default_error_handler(_, response: Response, data: dict):
status_code = response.status_code
error_message = (
f"Request was failed with status code: {status_code}."
Expand All @@ -33,7 +30,7 @@ class WhatsAppAPI:
_MEDIA_URI = 'media'

def __init__(self, mobile_id: str, api_token: str, version: str = LAST_API_VERSION,
database_config: DatabaseConfig = None, error_handler: ErrorHandlerType = None):
database_config: DatabaseConfig = None, error_handler=None):
self.mobile_id = mobile_id
self.bearer_token = api_token
self.version = version or self.DEFAULT_API_VERSION
Expand All @@ -43,7 +40,16 @@ def __init__(self, mobile_id: str, api_token: str, version: str = LAST_API_VERSI
warnings.warn(txt, WhatsAppAPIWarning)
else:
self._db = configure_database(database_config)
self.error_handler: Callable[[Response, dict | None], Any | None] = error_handler or _default_error_handler
self.error_handler = self._custom_error_handler_factory(error_handler or _default_error_handler)

def _custom_error_handler_factory(self, external_error_handler):
"""Bind an external error handler to the current instance."""

def error_handler(response: Response, data: dict):
# Call the external error handler with self as the first argument
external_error_handler(self, response, data)

return error_handler

@property
def db(self) -> WhatsAppDB:
Expand Down

0 comments on commit 49676fe

Please sign in to comment.