Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change constructor signature for CallbackIntercace. #100

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions dff/messengers/telegram/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,17 @@ class CallbackTelegramInterface(CallbackMessengerInterface): # pragma: no cover
:param port:
Port of the app.
Defaults to 8443.
:param debug:
Run the Flask app in debug mode.
:param load_dotenv:
Whether or not the .env file in the project folder
should be used to set environment variables.
:param full_uri:
Full public IP of your webhook that is accessible by https.
Defaults to `"https://{host}:{port}{endpoint}"`.
:param wsgi_options:
Keyword arguments to forward to `Flask.run` method.
Use these to set `ssl_context` and other WSGI options.
"""

def __init__(
Expand All @@ -201,9 +209,12 @@ def __init__(
app: Optional[Flask] = None,
host: str = "localhost",
port: int = 8443,
debug: Optional[bool] = None,
load_dotenv: bool = True,
endpoint: str = "/telegram-webhook",
full_uri: Optional[str] = None,
messenger: Optional[TelegramMessenger] = None,
**wsgi_options,
):
if not flask_imported:
raise ModuleNotFoundError("Flask is not installed. Install it with `pip install flask`.")
Expand All @@ -212,6 +223,9 @@ def __init__(
self.app = app if app else Flask(__name__)
self.host = host
self.port = port
self.debug = debug
self.load_dotenv = load_dotenv
self.wsgi_options = wsgi_options
self.endpoint = endpoint
self.full_uri = full_uri if full_uri is not None else "".join([f"https://{host}:{port}", endpoint])

Expand All @@ -221,7 +235,9 @@ async def endpoint():

json_string = request.get_data().decode("utf-8")
update = types.Update.de_json(json_string)
return self.on_request(*extract_telegram_request_and_id(update, self.messenger))
resp = self.on_request(*extract_telegram_request_and_id(update, self.messenger))
self.messenger.send_response(resp.id, resp.last_response)
return ""

self.app.route(self.endpoint, methods=["POST"])(endpoint)

Expand All @@ -231,4 +247,6 @@ async def connect(self, callback: PipelineRunnerFunction):
self.messenger.remove_webhook()
self.messenger.set_webhook(self.full_uri)

self.app.run(host=self.host, port=self.port)
self.app.run(
host=self.host, port=self.port, load_dotenv=self.load_dotenv, debug=self.debug, **self.wsgi_options
)