mirror of
https://github.com/nikdoof/smsbot.git
synced 2025-12-19 12:49:24 +00:00
Cleanup types
This commit is contained in:
@@ -8,18 +8,16 @@ from werkzeug.middleware.dispatcher import DispatcherMiddleware
|
||||
|
||||
from smsbot.utils import TwilioWebhookPayload, get_smsbot_version
|
||||
|
||||
REQUEST_TIME = Summary(
|
||||
"webhook_request_processing_seconds", "Time spent processing request"
|
||||
)
|
||||
REQUEST_TIME = Summary("webhook_request_processing_seconds", "Time spent processing request")
|
||||
MESSAGE_COUNT = Counter("webhook_message_count", "Total number of messages processed")
|
||||
CALL_COUNT = Counter("webhook_call_count", "Total number of calls processed")
|
||||
|
||||
|
||||
|
||||
class TwilioWebhookHandler(object):
|
||||
"""
|
||||
A wrapped Flask app handling webhooks received from Twilio
|
||||
"""
|
||||
|
||||
def __init__(self, account_sid: str | None = None, auth_token: str | None = None):
|
||||
self.app = Flask(self.__class__.__name__)
|
||||
self.app.add_url_rule("/", "index", self.index, methods=["GET"])
|
||||
@@ -50,9 +48,7 @@ class TwilioWebhookHandler(object):
|
||||
async def decorated_function(*args, **kwargs):
|
||||
# Create an instance of the RequestValidator class
|
||||
if not self.auth_token:
|
||||
current_app.logger.warning(
|
||||
"Twilio request validation skipped due to Twilio Auth Token missing"
|
||||
)
|
||||
current_app.logger.warning("Twilio request validation skipped due to Twilio Auth Token missing")
|
||||
return await func(*args, **kwargs)
|
||||
validator = RequestValidator(self.auth_token)
|
||||
|
||||
@@ -75,10 +71,10 @@ class TwilioWebhookHandler(object):
|
||||
def set_bot(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
async def index(self):
|
||||
async def index(self) -> str:
|
||||
return f'smsbot v{get_smsbot_version()} - <a href="https://github.com/nikdoof/smsbot">GitHub</a>'
|
||||
|
||||
async def health(self):
|
||||
async def health(self) -> dict[str, str | int]:
|
||||
"""Return basic health information"""
|
||||
return {
|
||||
"version": get_smsbot_version(),
|
||||
@@ -87,29 +83,24 @@ class TwilioWebhookHandler(object):
|
||||
}
|
||||
|
||||
@time(REQUEST_TIME)
|
||||
async def message(self):
|
||||
async def message(self) -> str:
|
||||
"""Handle incoming SMS messages from Twilio"""
|
||||
current_app.logger.info(
|
||||
"Received SMS from {From}: {Body}".format(**request.values.to_dict())
|
||||
)
|
||||
|
||||
await self.bot.send_subscribers(
|
||||
TwilioWebhookPayload.parse(request.values.to_dict()).to_markdownv2()
|
||||
)
|
||||
current_app.logger.info("Received SMS from {From}: {Body}".format(**request.values.to_dict()))
|
||||
hook_data = TwilioWebhookPayload.parse(request.values.to_dict())
|
||||
if hook_data:
|
||||
await self.bot.send_subscribers(hook_data.to_markdownv2())
|
||||
|
||||
# Return a blank response
|
||||
MESSAGE_COUNT.inc()
|
||||
return '<?xml version="1.0" encoding="UTF-8"?><Response></Response>'
|
||||
|
||||
@time(REQUEST_TIME)
|
||||
async def call(self):
|
||||
async def call(self) -> str:
|
||||
"""Handle incoming calls from Twilio"""
|
||||
current_app.logger.info(
|
||||
"Received Call from {From}".format(**request.values.to_dict())
|
||||
)
|
||||
await self.bot.send_subscribers(
|
||||
TwilioWebhookPayload.parse(request.values.to_dict()).to_markdownv2()
|
||||
)
|
||||
current_app.logger.info("Received Call from {From}".format(**request.values.to_dict()))
|
||||
hook_data = TwilioWebhookPayload.parse(request.values.to_dict())
|
||||
if hook_data:
|
||||
await self.bot.send_subscribers(hook_data.to_markdownv2())
|
||||
|
||||
# Always reject calls
|
||||
CALL_COUNT.inc()
|
||||
|
||||
Reference in New Issue
Block a user