From eb4cd7aba0df59501bbf5d43c4853d46b207eb61 Mon Sep 17 00:00:00 2001 From: _run Date: Wed, 6 Jul 2022 21:31:03 +0500 Subject: [PATCH 01/12] Webhook processing function using flask for sync --- telebot/__init__.py | 75 +++++++++++++++++++++++++++++ telebot/extensions/webhooks.py | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 telebot/extensions/webhooks.py diff --git a/telebot/__init__.py b/telebot/__init__.py index 9e6e124..6c793af 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -18,6 +18,13 @@ import telebot.types from telebot.storage import StatePickleStorage, StateMemoryStorage +# random module to generate random string +import random +import string + + +# webhooks module +from telebot.extensions.webhooks import SyncWebhookListener logger = logging.getLogger('TeleBot') @@ -293,6 +300,74 @@ class TeleBot: return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address, drop_pending_updates, timeout, secret_token) + + def run_webhooks(self, + listen: Optional[str]="127.0.0.1", + port: Optional[int]=443, + url_path: Optional[str]=None, + certificate: Optional[str]=None, + certificate_key: Optional[str]=None, + webhook_url: Optional[str]=None, + max_connections: Optional[int]=None, + allowed_updates: Optional[List]=None, + ip_address: Optional[str]=None, + drop_pending_updates: Optional[bool] = None, + timeout: Optional[int]=None, + secret_token: Optional[str]=None, + secret_token_length: Optional[int]=20, + debug: Optional[bool]=False): + """ + This class sets webhooks and listens to a given url and port. + + :param listen: IP address to listen to. Defaults to + 0.0.0.0 + :param port: A port which will be used to listen to webhooks. + :param url_path: Path to the webhook. Defaults to /token + :param certificate: Path to the certificate file. + :param certificate_key: Path to the certificate key file. + :param webhook_url: Webhook URL. + :param max_connections: Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput. + :param allowed_updates: A JSON-serialized list of the update types you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used. + :param ip_address: The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS + :param drop_pending_updates: Pass True to drop all pending updates + :param timeout: Integer. Request connection timeout + :param secret_token: Secret token to be used to verify the webhook request. + :return: + """ + + # generate secret token if not set + if not secret_token: + secret_token = ''.join(random.choices(string.ascii_uppercase + string.digits, k=secret_token_length)) + + + if not url_path: + url_path = self.token + '/' + if url_path[-1] != '/': url_path += '/' + + + + protocol = "https" if certificate else "http" + if not webhook_url: + webhook_url = "{}://{}:{}/{}".format(protocol, listen, port, url_path) + + + self.set_webhook( + url=webhook_url, + certificate=certificate, + max_connections=max_connections, + allowed_updates=allowed_updates, + ip_address=ip_address, + drop_pending_updates=drop_pending_updates, + timeout=timeout, + secret_token=secret_token + ) + + ssl_context = (certificate, certificate_key) if certificate else None + self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) + self.webhook_listener.run_app() + return self.webhook_listener + + def delete_webhook(self, drop_pending_updates=None, timeout=None): """ Use this method to remove webhook integration if you decide to switch back to getUpdates. diff --git a/telebot/extensions/webhooks.py b/telebot/extensions/webhooks.py new file mode 100644 index 0000000..e1c8e32 --- /dev/null +++ b/telebot/extensions/webhooks.py @@ -0,0 +1,87 @@ +""" +This file is used by TeleBot.run_webhooks() & +AsyncTeleBot.run_webhooks() functions. + +Flask/Aiohttp is required to run this script. +""" + + +flask_installed = True +try: + import flask + from werkzeug.serving import _TSSLContextArg +except ImportError: + flask_installed = False + + +from telebot.types import Update + + +from typing import Optional + + + + + +class SyncWebhookListener: + def __init__(self, bot, + secret_token: str, host: Optional[str]="127.0.0.1", + port: Optional[int]=8000, + ssl_context: Optional[_TSSLContextArg]=None, + url_path: Optional[str]=None, + debug: Optional[bool]=False + ) -> None: + """ + Synchronous implementation of webhook listener + for synchronous version of telebot. + + :param bot: TeleBot instance + :param secret_token: Telegram secret token + :param host: Webhook host + :param port: Webhook port + :param ssl_context: SSL context + """ + if not flask_installed: + raise ImportError('Flask is not installed. Please install it via pip.') + self.app = flask.Flask(__name__) + self._secret_token = secret_token + self._bot = bot + self._port = port + self._host = host + self._ssl_context = ssl_context + self._url_path = url_path + self._debug = debug + self._prepare_endpoint_urls() + + + def _prepare_endpoint_urls(self): + self.app.add_url_rule(self._url_path, 'index', self.process_update, methods=['POST']) + + + def process_update(self): + """ + Processes updates. + """ + # header containsX-Telegram-Bot-Api-Secret-Token + if flask.request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: + # secret token didn't match + flask.abort(403) + if flask.request.headers.get('content-type') == 'application/json': + json_string = flask.request.get_data().decode('utf-8') + self._bot.process_new_updates([Update.de_json(json_string)]) + return '' + + flask.abort(403) + + + def run_app(self): + """ + Run app with the given parameters. + """ + self.app.run( + host=self._host, + port=self._port, + ssl_context=self._ssl_context, + debug=self._debug + ) + return self \ No newline at end of file From 3cf5305845aa8e052f6967284dc3a37c0f5e74a0 Mon Sep 17 00:00:00 2001 From: _run Date: Wed, 6 Jul 2022 22:06:49 +0500 Subject: [PATCH 02/12] Rename --- telebot/{extensions => extension}/webhooks.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename telebot/{extensions => extension}/webhooks.py (100%) diff --git a/telebot/extensions/webhooks.py b/telebot/extension/webhooks.py similarity index 100% rename from telebot/extensions/webhooks.py rename to telebot/extension/webhooks.py From 7b8c98d731519740945f3893306682d8420c0a24 Mon Sep 17 00:00:00 2001 From: _run Date: Wed, 6 Jul 2022 23:15:22 +0500 Subject: [PATCH 03/12] Test --- telebot/{extension => bababa}/webhooks.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename telebot/{extension => bababa}/webhooks.py (100%) diff --git a/telebot/extension/webhooks.py b/telebot/bababa/webhooks.py similarity index 100% rename from telebot/extension/webhooks.py rename to telebot/bababa/webhooks.py From ebca668979d171d530ff1768504aeb6aba6acf8a Mon Sep 17 00:00:00 2001 From: _run Date: Wed, 6 Jul 2022 23:35:59 +0500 Subject: [PATCH 04/12] Create __init__.py --- telebot/bababa/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 telebot/bababa/__init__.py diff --git a/telebot/bababa/__init__.py b/telebot/bababa/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/telebot/bababa/__init__.py @@ -0,0 +1 @@ + From e353572c03a9233f20513979aaa2f4563d2f747c Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 15:09:02 +0500 Subject: [PATCH 05/12] Update webhooks listeners. --- telebot/__init__.py | 2 +- telebot/extensions/__init__.py | 7 +++++++ telebot/{bababa => extensions}/webhooks.py | 0 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 telebot/extensions/__init__.py rename telebot/{bababa => extensions}/webhooks.py (100%) diff --git a/telebot/__init__.py b/telebot/__init__.py index 6c793af..6c63667 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -24,7 +24,7 @@ import string # webhooks module -from telebot.extensions.webhooks import SyncWebhookListener +from telebot.extensions import SyncWebhookListener logger = logging.getLogger('TeleBot') diff --git a/telebot/extensions/__init__.py b/telebot/extensions/__init__.py new file mode 100644 index 0000000..8fc3bc3 --- /dev/null +++ b/telebot/extensions/__init__.py @@ -0,0 +1,7 @@ +from .webhooks import SyncWebhookListener + + + +__all__ = [ + 'SyncWebhookListener' +] \ No newline at end of file diff --git a/telebot/bababa/webhooks.py b/telebot/extensions/webhooks.py similarity index 100% rename from telebot/bababa/webhooks.py rename to telebot/extensions/webhooks.py From 140befc13298253d8fc40e7846161a02efc27c83 Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 15:15:24 +0500 Subject: [PATCH 06/12] Typehint fix if there is no flask installed --- telebot/extensions/webhooks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/telebot/extensions/webhooks.py b/telebot/extensions/webhooks.py index e1c8e32..ddfae41 100644 --- a/telebot/extensions/webhooks.py +++ b/telebot/extensions/webhooks.py @@ -12,6 +12,7 @@ try: from werkzeug.serving import _TSSLContextArg except ImportError: flask_installed = False + _TSSLContextArg = None from telebot.types import Update From f8f147f9f4cc03410fe4f35a584e06593b6a638e Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 15:53:27 +0500 Subject: [PATCH 07/12] 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) From 2f3223668035e962f62b62d29aba40fa2dc1b996 Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 22:56:13 +0500 Subject: [PATCH 08/12] Added run_webhooks for asynctelebot --- .../webhooks/run_webhooks.py | 45 ++++++++ examples/webhook_examples/run_webhooks.py | 45 ++++++++ telebot/__init__.py | 2 +- telebot/async_telebot.py | 83 ++++++++++++++ telebot/extensions/__init__.py | 7 +- telebot/extensions/webhooks.py | 103 +++++++++++++++++- 6 files changed, 278 insertions(+), 7 deletions(-) create mode 100644 examples/asynchronous_telebot/webhooks/run_webhooks.py create mode 100644 examples/webhook_examples/run_webhooks.py diff --git a/examples/asynchronous_telebot/webhooks/run_webhooks.py b/examples/asynchronous_telebot/webhooks/run_webhooks.py new file mode 100644 index 0000000..fa145fc --- /dev/null +++ b/examples/asynchronous_telebot/webhooks/run_webhooks.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +# This is a simple echo bot using the decorator mechanism. +# It echoes any incoming text messages. +# Example on built-in function to receive and process webhooks. + +from telebot.async_telebot import AsyncTeleBot +import asyncio +bot = AsyncTeleBot('TOKEN') + + +WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate +WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key +DOMAIN = '123.12.33.22' # either domain, or ip address of vps + +# Quick'n'dirty SSL certificate generation: +# +# openssl genrsa -out webhook_pkey.pem 2048 +# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem +# +# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply +# with the same value in you put in WEBHOOK_HOST + + +# Handle '/start' and '/help' +@bot.message_handler(commands=['help', 'start']) +async def send_welcome(message): + await bot.reply_to(message, """\ +Hi there, I am EchoBot. +I am here to echo your kind words back to you. Just say anything nice and I'll say the exact same thing to you!\ +""") + + +# Handle all other messages with content_type 'text' (content_types defaults to ['text']) +@bot.message_handler(func=lambda message: True) +async def echo_message(message): + await bot.reply_to(message, message.text) + + +# it uses fastapi + uvicorn +asyncio.run(bot.run_webhooks( + listen=DOMAIN, + certificate=WEBHOOK_SSL_CERT, + certificate_key=WEBHOOK_SSL_PRIV +)) \ No newline at end of file diff --git a/examples/webhook_examples/run_webhooks.py b/examples/webhook_examples/run_webhooks.py new file mode 100644 index 0000000..8332d30 --- /dev/null +++ b/examples/webhook_examples/run_webhooks.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +# This is a simple echo bot using the decorator mechanism. +# It echoes any incoming text messages. +# Example on built-in function to receive and process webhooks. + +import telebot + +API_TOKEN = 'TOKEN' + +bot = telebot.TeleBot(API_TOKEN) + +WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate +WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key +DOMAIN = '123.12.33.22' # either domain, or ip address of vps + +# Quick'n'dirty SSL certificate generation: +# +# openssl genrsa -out webhook_pkey.pem 2048 +# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem +# +# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply +# with the same value in you put in WEBHOOK_HOST + + +# Handle '/start' and '/help' +@bot.message_handler(commands=['help', 'start']) +def send_welcome(message): + bot.reply_to(message, """\ +Hi there, I am EchoBot. +I am here to echo your kind words back to you. Just say anything nice and I'll say the exact same thing to you!\ +""") + + +# Handle all other messages with content_type 'text' (content_types defaults to ['text']) +@bot.message_handler(func=lambda message: True) +def echo_message(message): + bot.reply_to(message, message.text) + + +bot.run_webhooks( + listen=DOMAIN, + certificate=WEBHOOK_SSL_CERT, + certificate_key=WEBHOOK_SSL_PRIV +) \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index 4018e52..cc624bc 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -375,7 +375,7 @@ class TeleBot: ssl_context = (certificate, certificate_key) if certificate else None self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) self.webhook_listener.run_app() - return self.webhook_listener + def delete_webhook(self, drop_pending_updates=None, timeout=None): diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 000746e..fd21d30 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -30,6 +30,13 @@ REPLY_MARKUP_TYPES = Union[ types.ReplyKeyboardRemove, types.ForceReply] +# for webhooks +from telebot.extensions import AsyncWebhookListener +import string +import random +import ssl + + """ Module : telebot """ @@ -1438,6 +1445,82 @@ class AsyncTeleBot: drop_pending_updates, timeout, secret_token) + async def run_webhooks(self, + listen: Optional[str]="127.0.0.1", + port: Optional[int]=443, + url_path: Optional[str]=None, + certificate: Optional[str]=None, + certificate_key: Optional[str]=None, + webhook_url: Optional[str]=None, + max_connections: Optional[int]=None, + allowed_updates: Optional[List]=None, + ip_address: Optional[str]=None, + drop_pending_updates: Optional[bool] = None, + timeout: Optional[int]=None, + secret_token: Optional[str]=None, + secret_token_length: Optional[int]=20, + debug: Optional[bool]=False): + """ + This class sets webhooks and listens to a given url and port. + + :param listen: IP address to listen to. Defaults to + 0.0.0.0 + :param port: A port which will be used to listen to webhooks. + :param url_path: Path to the webhook. Defaults to /token + :param certificate: Path to the certificate file. + :param certificate_key: Path to the certificate key file. + :param webhook_url: Webhook URL. + :param max_connections: Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput. + :param allowed_updates: A JSON-serialized list of the update types you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used. + :param ip_address: The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS + :param drop_pending_updates: Pass True to drop all pending updates + :param timeout: Integer. Request connection timeout + :param secret_token: Secret token to be used to verify the webhook request. + :return: + """ + + # generate secret token if not set + if not secret_token: + secret_token = ''.join(random.choices(string.ascii_uppercase + string.digits, k=secret_token_length)) + + + if not url_path: + url_path = self.token + '/' + if url_path[-1] != '/': url_path += '/' + + + + protocol = "https" if certificate else "http" + 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 + await self.set_webhook( + url=webhook_url, + certificate=cert_file, + max_connections=max_connections, + allowed_updates=allowed_updates, + ip_address=ip_address, + drop_pending_updates=drop_pending_updates, + timeout=timeout, + secret_token=secret_token + ) + if cert_file: cert_file.close() + + ssl_context = (certificate, certificate_key) if certificate else (None, None) + self.webhook_listener = AsyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) + # create a new loop, set it, and pass it + asyncio.set_event_loop(asyncio.new_event_loop()) + await self.webhook_listener.run_app() + + async def delete_webhook(self, drop_pending_updates=None, timeout=None): """ diff --git a/telebot/extensions/__init__.py b/telebot/extensions/__init__.py index 9eb87c7..9535cbe 100644 --- a/telebot/extensions/__init__.py +++ b/telebot/extensions/__init__.py @@ -1,7 +1,8 @@ -from .webhooks import SyncWebhookListener +from .webhooks import SyncWebhookListener, AsyncWebhookListener __all__ = [ - 'SyncWebhookListener' -] + 'SyncWebhookListener', + 'AsyncWebhookListener' +] \ No newline at end of file diff --git a/telebot/extensions/webhooks.py b/telebot/extensions/webhooks.py index ddfae41..9f44c48 100644 --- a/telebot/extensions/webhooks.py +++ b/telebot/extensions/webhooks.py @@ -2,11 +2,14 @@ This file is used by TeleBot.run_webhooks() & AsyncTeleBot.run_webhooks() functions. -Flask/Aiohttp is required to run this script. +Flask/fastapi is required to run this script. """ - +# modules required flask_installed = True +fastapi_installed = True +uvicorn_installed = True + try: import flask from werkzeug.serving import _TSSLContextArg @@ -14,7 +17,20 @@ except ImportError: flask_installed = False _TSSLContextArg = None +try: + import fastapi + from fastapi.responses import JSONResponse +except ImportError: + fastapi_installed = False + +try: + from uvicorn import Server, Config +except ImportError: + uvicorn_installed = False + + +import asyncio from telebot.types import Update @@ -85,4 +101,85 @@ class SyncWebhookListener: ssl_context=self._ssl_context, debug=self._debug ) - return self \ No newline at end of file + + + + +class AsyncWebhookListener: + def __init__(self, bot, + secret_token: str, host: Optional[str]="127.0.0.1", + port: Optional[int]=8000, + ssl_context: Optional[_TSSLContextArg]=None, + url_path: Optional[str]=None, + debug: Optional[bool]=False + ) -> None: + """ + Synchronous implementation of webhook listener + for synchronous version of telebot. + + :param bot: TeleBot instance + :param secret_token: Telegram secret token + :param host: Webhook host + :param port: Webhook port + :param ssl_context: SSL context + """ + self._check_dependencies() + + self.app = fastapi.FastAPI() + self._secret_token = secret_token + self._bot = bot + self._port = port + self._host = host + self._ssl_context = ssl_context + self._url_path = url_path + self._debug = debug + self._prepare_endpoint_urls() + + + def _check_dependencies(self): + if not fastapi_installed: + raise ImportError('Fastapi is not installed. Please install it via pip.') + if not uvicorn_installed: + raise ImportError('Uvicorn is not installed. Please install it via pip.') + + import starlette + if starlette.__version__ < '0.20.2': + raise ImportError('Starlette version is too old. Please upgrade it.') + return + + + def _prepare_endpoint_urls(self): + self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"]) + + + async def process_update(self, request: fastapi.Request): + """ + Processes updates. + """ + # header containsX-Telegram-Bot-Api-Secret-Token + if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: + # secret token didn't match + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + if request.headers.get('content-type') == 'application/json': + json_string = await request.json() + asyncio.create_task(self._bot.process_new_updates([Update.de_json(json_string)])) + return JSONResponse('', status_code=200) + + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + + + async def run_app(self): + """ + Run app with the given parameters. + """ + + config = Config(app=self.app, + host=self._host, + port=self._port, + debug=self._debug, + ssl_certfile=self._ssl_context[0], + ssl_keyfile=self._ssl_context[1] + ) + server = Server(config) + await server.serve() + await self._bot.close_session() \ No newline at end of file From 0cf2a0fe77bbe8e6255abbc7fbfca847685f1b50 Mon Sep 17 00:00:00 2001 From: _run Date: Thu, 7 Jul 2022 23:02:51 +0500 Subject: [PATCH 09/12] Added extra dependencies and fixed tests --- setup.py | 4 ++++ telebot/extensions/webhooks.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index eb1c9ee..5552a43 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,11 @@ setup(name='pyTelegramBotAPI', 'json': 'ujson', 'PIL': 'Pillow', 'redis': 'redis>=3.4.1', + 'aioredis': 'aioredis', 'aiohttp': 'aiohttp', + 'flask': 'flask', + 'fastapi': 'fastapi', + 'uvicorn': 'uvicorn', }, classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/telebot/extensions/webhooks.py b/telebot/extensions/webhooks.py index 9f44c48..1d3d7db 100644 --- a/telebot/extensions/webhooks.py +++ b/telebot/extensions/webhooks.py @@ -152,7 +152,7 @@ class AsyncWebhookListener: self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"]) - async def process_update(self, request: fastapi.Request): + async def process_update(self, request): """ Processes updates. """ From 970b9d6be4f71555d25f327ff018b237907b2dcf Mon Sep 17 00:00:00 2001 From: _run Date: Fri, 8 Jul 2022 21:13:07 +0500 Subject: [PATCH 10/12] SyncWebhookListener was rewritten under fastapi. Extensions folder was divided into 2(namings are long, might be changed) --- telebot/__init__.py | 10 +- telebot/async_telebot.py | 9 +- telebot/extensions/__init__.py | 11 +-- telebot/extensions/asynchronous/__init__.py | 10 ++ telebot/extensions/asynchronous/webhooks.py | 103 ++++++++++++++++++++ telebot/extensions/synchronous/__init__.py | 10 ++ telebot/extensions/synchronous/webhooks.py | 101 +++++++++++++++++++ 7 files changed, 238 insertions(+), 16 deletions(-) create mode 100644 telebot/extensions/asynchronous/__init__.py create mode 100644 telebot/extensions/asynchronous/webhooks.py create mode 100644 telebot/extensions/synchronous/__init__.py create mode 100644 telebot/extensions/synchronous/webhooks.py diff --git a/telebot/__init__.py b/telebot/__init__.py index cc624bc..8d73f63 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -23,9 +23,6 @@ import random import string -# webhooks module -from telebot.extensions import SyncWebhookListener - import ssl logger = logging.getLogger('TeleBot') @@ -372,7 +369,12 @@ class TeleBot: ) if cert_file: cert_file.close() - ssl_context = (certificate, certificate_key) if certificate else None + ssl_context = (certificate, certificate_key) if certificate else (None, None) + # webhooks module + try: + from telebot.extensions.synchronous import SyncWebhookListener + except NameError: + raise ImportError("Please install uvicorn and fastapi in order to use `run_webhooks` method.") self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) self.webhook_listener.run_app() diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index fd21d30..1257e73 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -30,8 +30,6 @@ REPLY_MARKUP_TYPES = Union[ types.ReplyKeyboardRemove, types.ForceReply] -# for webhooks -from telebot.extensions import AsyncWebhookListener import string import random import ssl @@ -1515,9 +1513,12 @@ class AsyncTeleBot: if cert_file: cert_file.close() ssl_context = (certificate, certificate_key) if certificate else (None, None) + # for webhooks + try: + from telebot.extensions.asynchronous import AsyncWebhookListener + except NameError: + raise ImportError("Please install uvicorn and fastapi in order to use `run_webhooks` method.") self.webhook_listener = AsyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) - # create a new loop, set it, and pass it - asyncio.set_event_loop(asyncio.new_event_loop()) await self.webhook_listener.run_app() diff --git a/telebot/extensions/__init__.py b/telebot/extensions/__init__.py index 9535cbe..e417b38 100644 --- a/telebot/extensions/__init__.py +++ b/telebot/extensions/__init__.py @@ -1,8 +1,3 @@ -from .webhooks import SyncWebhookListener, AsyncWebhookListener - - - -__all__ = [ - 'SyncWebhookListener', - 'AsyncWebhookListener' -] \ No newline at end of file +""" +A folder with asynchronous and synchronous extensions. +""" diff --git a/telebot/extensions/asynchronous/__init__.py b/telebot/extensions/asynchronous/__init__.py new file mode 100644 index 0000000..3d2fd26 --- /dev/null +++ b/telebot/extensions/asynchronous/__init__.py @@ -0,0 +1,10 @@ +""" +A folder with all the async extensions. +""" + +from .webhooks import AsyncWebhookListener + + +__all__ = [ + "AsyncWebhookListener" +] \ No newline at end of file diff --git a/telebot/extensions/asynchronous/webhooks.py b/telebot/extensions/asynchronous/webhooks.py new file mode 100644 index 0000000..13e28ab --- /dev/null +++ b/telebot/extensions/asynchronous/webhooks.py @@ -0,0 +1,103 @@ +""" +This file is used by AsyncTeleBot.run_webhooks() function. + +Fastapi and starlette(0.20.2+) libraries are required to run this script. +""" + +# modules required for running this script +fastapi_installed = True +try: + import fastapi + from fastapi.responses import JSONResponse + from fastapi.requests import Request + from uvicorn import Server, Config +except ImportError: + fastapi_installed = False + +import asyncio + + +from telebot.types import Update + + +from typing import Optional + + +class AsyncWebhookListener: + def __init__(self, bot, + secret_token: str, host: Optional[str]="127.0.0.1", + port: Optional[int]=443, + ssl_context: Optional[tuple]=None, + url_path: Optional[str]=None, + debug: Optional[bool]=False + ) -> None: + """ + Aynchronous implementation of webhook listener + for asynchronous version of telebot. + + :param bot: TeleBot instance + :param secret_token: Telegram secret token + :param host: Webhook host + :param port: Webhook port + :param ssl_context: SSL context + :param url_path: Webhook url path + :param debug: Debug mode + """ + self._check_dependencies() + + self.app = fastapi.FastAPI() + self._secret_token = secret_token + self._bot = bot + self._port = port + self._host = host + self._ssl_context = ssl_context + self._url_path = url_path + self._debug = debug + self._prepare_endpoint_urls() + + + def _check_dependencies(self): + if not fastapi_installed: + raise ImportError('Fastapi or uvicorn is not installed. Please install it via pip.') + + import starlette + if starlette.__version__ < '0.20.2': + raise ImportError('Starlette version is too old. Please upgrade it: `pip3 install starlette -U`') + return + + + def _prepare_endpoint_urls(self): + self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"]) + + + async def process_update(self, request: Request, update: dict): + """ + Processes updates. + """ + # header containsX-Telegram-Bot-Api-Secret-Token + if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: + # secret token didn't match + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + if request.headers.get('content-type') == 'application/json': + json_string = update + asyncio.create_task(self._bot.process_new_updates([Update.de_json(json_string)])) + return JSONResponse('', status_code=200) + + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + + + async def run_app(self): + """ + Run app with the given parameters. + """ + + config = Config(app=self.app, + host=self._host, + port=self._port, + debug=self._debug, + ssl_certfile=self._ssl_context[0], + ssl_keyfile=self._ssl_context[1] + ) + server = Server(config) + await server.serve() + await self._bot.close_session() \ No newline at end of file diff --git a/telebot/extensions/synchronous/__init__.py b/telebot/extensions/synchronous/__init__.py new file mode 100644 index 0000000..f7728ae --- /dev/null +++ b/telebot/extensions/synchronous/__init__.py @@ -0,0 +1,10 @@ +""" +A folder with all the sync extensions. +""" + +from .webhooks import SyncWebhookListener + + +__all__ = [ + "SyncWebhookListener" +] \ No newline at end of file diff --git a/telebot/extensions/synchronous/webhooks.py b/telebot/extensions/synchronous/webhooks.py new file mode 100644 index 0000000..89a3ec9 --- /dev/null +++ b/telebot/extensions/synchronous/webhooks.py @@ -0,0 +1,101 @@ +""" +This file is used by TeleBot.run_webhooks() & +AsyncTeleBot.run_webhooks() functions. + +Flask/fastapi is required to run this script. +""" + +# modules required for running this script +fastapi_installed = True + +try: + import fastapi + from fastapi.responses import JSONResponse + from fastapi.requests import Request + import uvicorn +except ImportError: + fastapi_installed = False + + +from telebot.types import Update + + +from typing import Optional + + + + +class SyncWebhookListener: + def __init__(self, bot, + secret_token: str, host: Optional[str]="127.0.0.1", + port: Optional[int]=443, + ssl_context: Optional[tuple]=None, + url_path: Optional[str]=None, + debug: Optional[bool]=False + ) -> None: + """ + Synchronous implementation of webhook listener + for synchronous version of telebot. + + :param bot: TeleBot instance + :param secret_token: Telegram secret token + :param host: Webhook host + :param port: Webhook port + :param ssl_context: SSL context + :param url_path: Webhook url path + :param debug: Debug mode + """ + self._check_dependencies() + + self.app = fastapi.FastAPI() + self._secret_token = secret_token + self._bot = bot + self._port = port + self._host = host + self._ssl_context = ssl_context + self._url_path = url_path + self._debug = debug + self._prepare_endpoint_urls() + + + def _check_dependencies(self): + if not fastapi_installed: + raise ImportError('Fastapi or uvicorn is not installed. Please install it via pip.') + + import starlette + if starlette.__version__ < '0.20.2': + raise ImportError('Starlette version is too old. Please upgrade it: `pip3 install starlette -U`') + return + + + def _prepare_endpoint_urls(self): + self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"]) + + + def process_update(self, request: Request, update: dict): + """ + Processes updates. + """ + # header containsX-Telegram-Bot-Api-Secret-Token + if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: + # secret token didn't match + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + if request.headers.get('content-type') == 'application/json': + self._bot.process_new_updates([Update.de_json(update)]) + return JSONResponse('', status_code=200) + + return JSONResponse(status_code=403, content={"error": "Forbidden"}) + + + def run_app(self): + """ + Run app with the given parameters. + """ + + uvicorn.run(app=self.app, + host=self._host, + port=self._port, + debug=self._debug, + ssl_certfile=self._ssl_context[0], + ssl_keyfile=self._ssl_context[1] + ) From d67ee2a5c521194a9e3366aa48ab5f2ed8b810de Mon Sep 17 00:00:00 2001 From: _run Date: Fri, 8 Jul 2022 21:16:01 +0500 Subject: [PATCH 11/12] Delete webhooks.py --- telebot/extensions/webhooks.py | 185 --------------------------------- 1 file changed, 185 deletions(-) delete mode 100644 telebot/extensions/webhooks.py diff --git a/telebot/extensions/webhooks.py b/telebot/extensions/webhooks.py deleted file mode 100644 index 1d3d7db..0000000 --- a/telebot/extensions/webhooks.py +++ /dev/null @@ -1,185 +0,0 @@ -""" -This file is used by TeleBot.run_webhooks() & -AsyncTeleBot.run_webhooks() functions. - -Flask/fastapi is required to run this script. -""" - -# modules required -flask_installed = True -fastapi_installed = True -uvicorn_installed = True - -try: - import flask - from werkzeug.serving import _TSSLContextArg -except ImportError: - flask_installed = False - _TSSLContextArg = None - -try: - import fastapi - from fastapi.responses import JSONResponse -except ImportError: - fastapi_installed = False - - -try: - from uvicorn import Server, Config -except ImportError: - uvicorn_installed = False - - -import asyncio -from telebot.types import Update - - -from typing import Optional - - - - - -class SyncWebhookListener: - def __init__(self, bot, - secret_token: str, host: Optional[str]="127.0.0.1", - port: Optional[int]=8000, - ssl_context: Optional[_TSSLContextArg]=None, - url_path: Optional[str]=None, - debug: Optional[bool]=False - ) -> None: - """ - Synchronous implementation of webhook listener - for synchronous version of telebot. - - :param bot: TeleBot instance - :param secret_token: Telegram secret token - :param host: Webhook host - :param port: Webhook port - :param ssl_context: SSL context - """ - if not flask_installed: - raise ImportError('Flask is not installed. Please install it via pip.') - self.app = flask.Flask(__name__) - self._secret_token = secret_token - self._bot = bot - self._port = port - self._host = host - self._ssl_context = ssl_context - self._url_path = url_path - self._debug = debug - self._prepare_endpoint_urls() - - - def _prepare_endpoint_urls(self): - self.app.add_url_rule(self._url_path, 'index', self.process_update, methods=['POST']) - - - def process_update(self): - """ - Processes updates. - """ - # header containsX-Telegram-Bot-Api-Secret-Token - if flask.request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: - # secret token didn't match - flask.abort(403) - if flask.request.headers.get('content-type') == 'application/json': - json_string = flask.request.get_data().decode('utf-8') - self._bot.process_new_updates([Update.de_json(json_string)]) - return '' - - flask.abort(403) - - - def run_app(self): - """ - Run app with the given parameters. - """ - self.app.run( - host=self._host, - port=self._port, - ssl_context=self._ssl_context, - debug=self._debug - ) - - - - -class AsyncWebhookListener: - def __init__(self, bot, - secret_token: str, host: Optional[str]="127.0.0.1", - port: Optional[int]=8000, - ssl_context: Optional[_TSSLContextArg]=None, - url_path: Optional[str]=None, - debug: Optional[bool]=False - ) -> None: - """ - Synchronous implementation of webhook listener - for synchronous version of telebot. - - :param bot: TeleBot instance - :param secret_token: Telegram secret token - :param host: Webhook host - :param port: Webhook port - :param ssl_context: SSL context - """ - self._check_dependencies() - - self.app = fastapi.FastAPI() - self._secret_token = secret_token - self._bot = bot - self._port = port - self._host = host - self._ssl_context = ssl_context - self._url_path = url_path - self._debug = debug - self._prepare_endpoint_urls() - - - def _check_dependencies(self): - if not fastapi_installed: - raise ImportError('Fastapi is not installed. Please install it via pip.') - if not uvicorn_installed: - raise ImportError('Uvicorn is not installed. Please install it via pip.') - - import starlette - if starlette.__version__ < '0.20.2': - raise ImportError('Starlette version is too old. Please upgrade it.') - return - - - def _prepare_endpoint_urls(self): - self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"]) - - - async def process_update(self, request): - """ - Processes updates. - """ - # header containsX-Telegram-Bot-Api-Secret-Token - if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token: - # secret token didn't match - return JSONResponse(status_code=403, content={"error": "Forbidden"}) - if request.headers.get('content-type') == 'application/json': - json_string = await request.json() - asyncio.create_task(self._bot.process_new_updates([Update.de_json(json_string)])) - return JSONResponse('', status_code=200) - - return JSONResponse(status_code=403, content={"error": "Forbidden"}) - - - async def run_app(self): - """ - Run app with the given parameters. - """ - - config = Config(app=self.app, - host=self._host, - port=self._port, - debug=self._debug, - ssl_certfile=self._ssl_context[0], - ssl_keyfile=self._ssl_context[1] - ) - server = Server(config) - await server.serve() - await self._bot.close_session() \ No newline at end of file From 90a90d4a340d162fd3e6e2c375523ecda5e532e8 Mon Sep 17 00:00:00 2001 From: _run Date: Sat, 9 Jul 2022 22:30:36 +0500 Subject: [PATCH 12/12] Divided async and sync versions into aio & sync folders --- examples/asynchronous_telebot/webhooks/run_webhooks.py | 2 +- examples/webhook_examples/run_webhooks.py | 2 +- setup.py | 1 - telebot/__init__.py | 4 ++-- telebot/async_telebot.py | 4 ++-- telebot/{extensions => ext}/__init__.py | 0 telebot/{extensions/asynchronous => ext/aio}/__init__.py | 0 telebot/{extensions/asynchronous => ext/aio}/webhooks.py | 0 telebot/{extensions/synchronous => ext/sync}/__init__.py | 0 telebot/{extensions/synchronous => ext/sync}/webhooks.py | 0 10 files changed, 6 insertions(+), 7 deletions(-) rename telebot/{extensions => ext}/__init__.py (100%) rename telebot/{extensions/asynchronous => ext/aio}/__init__.py (100%) rename telebot/{extensions/asynchronous => ext/aio}/webhooks.py (100%) rename telebot/{extensions/synchronous => ext/sync}/__init__.py (100%) rename telebot/{extensions/synchronous => ext/sync}/webhooks.py (100%) diff --git a/examples/asynchronous_telebot/webhooks/run_webhooks.py b/examples/asynchronous_telebot/webhooks/run_webhooks.py index fa145fc..3b74c33 100644 --- a/examples/asynchronous_telebot/webhooks/run_webhooks.py +++ b/examples/asynchronous_telebot/webhooks/run_webhooks.py @@ -11,7 +11,7 @@ bot = AsyncTeleBot('TOKEN') WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key -DOMAIN = '123.12.33.22' # either domain, or ip address of vps +DOMAIN = '1.2.3.4' # either domain, or ip address of vps # Quick'n'dirty SSL certificate generation: # diff --git a/examples/webhook_examples/run_webhooks.py b/examples/webhook_examples/run_webhooks.py index 8332d30..eeac2d1 100644 --- a/examples/webhook_examples/run_webhooks.py +++ b/examples/webhook_examples/run_webhooks.py @@ -12,7 +12,7 @@ bot = telebot.TeleBot(API_TOKEN) WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key -DOMAIN = '123.12.33.22' # either domain, or ip address of vps +DOMAIN = '1.2.3.4' # either domain, or ip address of vps # Quick'n'dirty SSL certificate generation: # diff --git a/setup.py b/setup.py index 5552a43..2e60d91 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,6 @@ setup(name='pyTelegramBotAPI', 'redis': 'redis>=3.4.1', 'aioredis': 'aioredis', 'aiohttp': 'aiohttp', - 'flask': 'flask', 'fastapi': 'fastapi', 'uvicorn': 'uvicorn', }, diff --git a/telebot/__init__.py b/telebot/__init__.py index 8d73f63..1c88c17 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -372,8 +372,8 @@ class TeleBot: ssl_context = (certificate, certificate_key) if certificate else (None, None) # webhooks module try: - from telebot.extensions.synchronous import SyncWebhookListener - except NameError: + from telebot.ext.sync import SyncWebhookListener + except (NameError, ImportError): raise ImportError("Please install uvicorn and fastapi in order to use `run_webhooks` method.") self.webhook_listener = SyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) self.webhook_listener.run_app() diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 1257e73..e9ee889 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -1515,8 +1515,8 @@ class AsyncTeleBot: ssl_context = (certificate, certificate_key) if certificate else (None, None) # for webhooks try: - from telebot.extensions.asynchronous import AsyncWebhookListener - except NameError: + from telebot.ext.aio import AsyncWebhookListener + except (NameError, ImportError): raise ImportError("Please install uvicorn and fastapi in order to use `run_webhooks` method.") self.webhook_listener = AsyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) await self.webhook_listener.run_app() diff --git a/telebot/extensions/__init__.py b/telebot/ext/__init__.py similarity index 100% rename from telebot/extensions/__init__.py rename to telebot/ext/__init__.py diff --git a/telebot/extensions/asynchronous/__init__.py b/telebot/ext/aio/__init__.py similarity index 100% rename from telebot/extensions/asynchronous/__init__.py rename to telebot/ext/aio/__init__.py diff --git a/telebot/extensions/asynchronous/webhooks.py b/telebot/ext/aio/webhooks.py similarity index 100% rename from telebot/extensions/asynchronous/webhooks.py rename to telebot/ext/aio/webhooks.py diff --git a/telebot/extensions/synchronous/__init__.py b/telebot/ext/sync/__init__.py similarity index 100% rename from telebot/extensions/synchronous/__init__.py rename to telebot/ext/sync/__init__.py diff --git a/telebot/extensions/synchronous/webhooks.py b/telebot/ext/sync/webhooks.py similarity index 100% rename from telebot/extensions/synchronous/webhooks.py rename to telebot/ext/sync/webhooks.py