Skip to content

Commit 5a93e08

Browse files
committed
Logging setup & aliased response keys settings added in WebhookServer init
1 parent 30482e6 commit 5a93e08

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

whatsapp_api_webhook_server_python_v2/main.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import logging
12
import os
23
import signal
34
from typing import Annotated, Any, Callable, Dict, Optional, Union
45

56
import uvicorn
7+
import uvicorn.config
68
from fastapi import Depends, FastAPI, Header, HTTPException, status
79

810
from .webhook_dto import WebhookData
@@ -18,6 +20,12 @@ class GreenAPIWebhookServer:
1820
- `event_handler`: callable object for handling events. Must receive two args:
1921
`webhook_type: str` & `webhook_data: dict`
2022
- `webhook_auth_header`: Your GreenAPI auth header key
23+
- `return_keys_by_alias: bool = False`: if this value is set to `True`,
24+
webhook keys will be returned in camelCase,
25+
otherwise in snake_case. Default `False` (snake_case)
26+
- `enable_info_logs: bool = False`: if this value is set to `True`,
27+
INFO logs from server will be enabled, otherwise - only ERROR.
28+
Default `False` (ERROR)
2129
2230
call `.start()` method for starting server
2331
"""
@@ -28,19 +36,26 @@ def __init__(
2836
host: str = "0.0.0.0",
2937
port: int = 8080,
3038
webhook_auth_header: Optional[str] = None,
39+
return_keys_by_alias: bool = False,
40+
enable_info_logs: bool = False,
3141
):
3242
self._host = host
3343
self._port = port
3444
self._event_handler = event_handler
3545
self._webhook_auth_header = webhook_auth_header
46+
self._return_keys_by_alias = return_keys_by_alias
47+
self._enable_info_logs = enable_info_logs
3648

3749
self._init_server()
3850

3951
def _handle_webhook(self, webhook_data: WebhookData, handler_func: Callable):
4052
"""
4153
Handles the incoming webhook data by calling the event handler function
4254
"""
43-
parsed_data = webhook_data.model_dump(exclude_none=True)
55+
parsed_data = webhook_data.model_dump(
56+
exclude_none=True,
57+
by_alias=self._return_keys_by_alias,
58+
)
4459
handler_func(webhook_data.type_webhook, parsed_data)
4560

4661
def _init_server(self):
@@ -83,6 +98,7 @@ def start(self):
8398
host=self._host,
8499
port=self._port,
85100
reload=False,
101+
log_level=logging.INFO if self._enable_info_logs else logging.ERROR,
86102
)
87103
except KeyboardInterrupt:
88104
os.kill(os.getpid(), signal.SIGTERM)

whatsapp_api_webhook_server_python_v2/webhook_dto.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ class ContactsArrayMessage(BaseModel):
114114

115115

116116
class Button(BaseModel):
117+
117118
button_id: Optional[str] = Field(None, alias="buttonId")
118119
button_text: Optional[str] = Field(None, alias="buttonText")
119120

120121

121122
class ButtonsMessage(BaseModel):
123+
122124
content_text: Optional[str] = Field(None, alias="contentText")
123125
footer: Optional[str] = Field(None, alias="footer")
124126
buttons: Optional[List[Button]] = Field(None, alias="buttons")
@@ -127,17 +129,20 @@ class ButtonsMessage(BaseModel):
127129

128130

129131
class ListMessageRow(BaseModel):
132+
130133
title: Optional[str] = Field(None, alias="title")
131134
row_id: Optional[str] = Field(None, alias="rowId")
132135
description: Optional[str] = Field(None, alias="description")
133136

134137

135138
class ListMessageSection(BaseModel):
139+
136140
title: Optional[str] = Field(None, alias="title")
137141
rows: Optional[List[ListMessageRow]] = Field(None, alias="rows")
138142

139143

140144
class ListMessage(BaseModel):
145+
141146
content_text: Optional[str] = Field(None, alias="contentText")
142147
title: Optional[str] = Field(None, alias="title")
143148
footer: Optional[str] = Field(None, alias="footer")
@@ -178,6 +183,7 @@ class TemplateMessageButton(BaseModel):
178183

179184

180185
class TemplateMessage(BaseModel):
186+
181187
content_text: Optional[str] = Field(None, alias="contentText")
182188
footer: Optional[str] = Field(None, alias="footer")
183189
buttons: Optional[List[TemplateMessageButton]] = Field(None, alias="buttons")
@@ -186,6 +192,7 @@ class TemplateMessage(BaseModel):
186192

187193

188194
class GroupInviteMessageDataExpiration(BaseModel):
195+
189196
low: Optional[int] = Field(None, alias="low")
190197
high: Optional[int] = Field(None, alias="high")
191198
unsigned: Optional[bool] = Field(None, alias="unsigned")
@@ -205,15 +212,18 @@ class GroupInviteMessageData(BaseModel):
205212

206213

207214
class PollMessageOption(BaseModel):
215+
208216
option_name: Optional[str] = Field(None, alias="optionName")
209217

210218

211219
class PollMessageVote(BaseModel):
220+
212221
option_name: Optional[str] = Field(None, alias="optionName")
213222
option_voters: Optional[List[str]] = Field(None, alias="optionVoters")
214223

215224

216225
class PollMessageData(BaseModel):
226+
217227
stanza_id: Optional[str] = Field(None, alias="stanzaId")
218228
name: Optional[str] = Field(None, alias="name")
219229
options: Optional[List[PollMessageOption]] = Field(None, alias="options")

0 commit comments

Comments
 (0)