From f8f147f9f4cc03410fe4f35a584e06593b6a638e Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 15:53:27 +0500 Subject: [PATCH] Fix certificate for webhooks --- telebot/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 6c63667..4018e52 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -26,6 +26,8 @@ import string # webhooks module from telebot.extensions import SyncWebhookListener +import ssl + logger = logging.getLogger('TeleBot') formatter = logging.Formatter( @@ -350,10 +352,17 @@ class TeleBot: if not webhook_url: webhook_url = "{}://{}:{}/{}".format(protocol, listen, port, url_path) - + if certificate and certificate_key: + ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) + ssl_ctx.load_cert_chain(certificate, certificate_key) + else: + ssl_ctx = None + + # open certificate if it exists + cert_file = open(certificate, 'rb') if certificate else None self.set_webhook( url=webhook_url, - certificate=certificate, + certificate=cert_file, max_connections=max_connections, allowed_updates=allowed_updates, ip_address=ip_address, @@ -361,6 +370,7 @@ class TeleBot: timeout=timeout, secret_token=secret_token ) + if cert_file: cert_file.close() ssl_context = (certificate, certificate_key) if certificate else None self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug)