Merge pull request #1535 from contributors/fix-telegram-proxy-crash
This commit is contained in:
@@ -187,10 +187,14 @@ class TelegramChannel(BaseChannel):
|
|||||||
self._running = True
|
self._running = True
|
||||||
|
|
||||||
# Build the application with larger connection pool to avoid pool-timeout on long runs
|
# Build the application with larger connection pool to avoid pool-timeout on long runs
|
||||||
req = HTTPXRequest(connection_pool_size=16, pool_timeout=5.0, connect_timeout=30.0, read_timeout=30.0)
|
req = HTTPXRequest(
|
||||||
|
connection_pool_size=16,
|
||||||
|
pool_timeout=5.0,
|
||||||
|
connect_timeout=30.0,
|
||||||
|
read_timeout=30.0,
|
||||||
|
proxy=self.config.proxy if self.config.proxy else None,
|
||||||
|
)
|
||||||
builder = Application.builder().token(self.config.token).request(req).get_updates_request(req)
|
builder = Application.builder().token(self.config.token).request(req).get_updates_request(req)
|
||||||
if self.config.proxy:
|
|
||||||
builder = builder.proxy(self.config.proxy).get_updates_proxy(self.config.proxy)
|
|
||||||
self._app = builder.build()
|
self._app = builder.build()
|
||||||
self._app.add_error_handler(self._on_error)
|
self._app.add_error_handler(self._on_error)
|
||||||
|
|
||||||
|
|||||||
107
tests/test_telegram_channel.py
Normal file
107
tests/test_telegram_channel.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from nanobot.bus.queue import MessageBus
|
||||||
|
from nanobot.channels.telegram import TelegramChannel
|
||||||
|
from nanobot.config.schema import TelegramConfig
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeHTTPXRequest:
|
||||||
|
instances: list["_FakeHTTPXRequest"] = []
|
||||||
|
|
||||||
|
def __init__(self, **kwargs) -> None:
|
||||||
|
self.kwargs = kwargs
|
||||||
|
self.__class__.instances.append(self)
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeUpdater:
|
||||||
|
def __init__(self, on_start_polling) -> None:
|
||||||
|
self._on_start_polling = on_start_polling
|
||||||
|
|
||||||
|
async def start_polling(self, **kwargs) -> None:
|
||||||
|
self._on_start_polling()
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeBot:
|
||||||
|
async def get_me(self):
|
||||||
|
return SimpleNamespace(username="nanobot_test")
|
||||||
|
|
||||||
|
async def set_my_commands(self, commands) -> None:
|
||||||
|
self.commands = commands
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeApp:
|
||||||
|
def __init__(self, on_start_polling) -> None:
|
||||||
|
self.bot = _FakeBot()
|
||||||
|
self.updater = _FakeUpdater(on_start_polling)
|
||||||
|
self.handlers = []
|
||||||
|
self.error_handlers = []
|
||||||
|
|
||||||
|
def add_error_handler(self, handler) -> None:
|
||||||
|
self.error_handlers.append(handler)
|
||||||
|
|
||||||
|
def add_handler(self, handler) -> None:
|
||||||
|
self.handlers.append(handler)
|
||||||
|
|
||||||
|
async def initialize(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def start(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeBuilder:
|
||||||
|
def __init__(self, app: _FakeApp) -> None:
|
||||||
|
self.app = app
|
||||||
|
self.token_value = None
|
||||||
|
self.request_value = None
|
||||||
|
self.get_updates_request_value = None
|
||||||
|
|
||||||
|
def token(self, token: str):
|
||||||
|
self.token_value = token
|
||||||
|
return self
|
||||||
|
|
||||||
|
def request(self, request):
|
||||||
|
self.request_value = request
|
||||||
|
return self
|
||||||
|
|
||||||
|
def get_updates_request(self, request):
|
||||||
|
self.get_updates_request_value = request
|
||||||
|
return self
|
||||||
|
|
||||||
|
def proxy(self, _proxy):
|
||||||
|
raise AssertionError("builder.proxy should not be called when request is set")
|
||||||
|
|
||||||
|
def get_updates_proxy(self, _proxy):
|
||||||
|
raise AssertionError("builder.get_updates_proxy should not be called when request is set")
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
return self.app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_start_uses_request_proxy_without_builder_proxy(monkeypatch) -> None:
|
||||||
|
config = TelegramConfig(
|
||||||
|
enabled=True,
|
||||||
|
token="123:abc",
|
||||||
|
allow_from=["*"],
|
||||||
|
proxy="http://127.0.0.1:7890",
|
||||||
|
)
|
||||||
|
bus = MessageBus()
|
||||||
|
channel = TelegramChannel(config, bus)
|
||||||
|
app = _FakeApp(lambda: setattr(channel, "_running", False))
|
||||||
|
builder = _FakeBuilder(app)
|
||||||
|
|
||||||
|
monkeypatch.setattr("nanobot.channels.telegram.HTTPXRequest", _FakeHTTPXRequest)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"nanobot.channels.telegram.Application",
|
||||||
|
SimpleNamespace(builder=lambda: builder),
|
||||||
|
)
|
||||||
|
|
||||||
|
await channel.start()
|
||||||
|
|
||||||
|
assert len(_FakeHTTPXRequest.instances) == 1
|
||||||
|
assert _FakeHTTPXRequest.instances[0].kwargs["proxy"] == config.proxy
|
||||||
|
assert builder.request_value is _FakeHTTPXRequest.instances[0]
|
||||||
|
assert builder.get_updates_request_value is _FakeHTTPXRequest.instances[0]
|
||||||
Reference in New Issue
Block a user