Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,22 @@ def __init__(
f,
)

self.application = (
# Build Telegram application with optional custom base URL for testing
builder = (
ApplicationBuilder()
.persistence(PicklePersistence(filepath="persistent_storage.pickle"))
.token(tg_config["token"])
# .post_shutdown(signal_handler)
.concurrent_updates(True)
.build()
)

# Allow overriding Telegram API base URL (for mocking in tests)
telegram_base_url = os.environ.get("TELEGRAM_BASE_URL")
if telegram_base_url:
logger.info(f"Using custom Telegram base URL: {telegram_base_url}")
builder = builder.base_url(telegram_base_url)

self.application = builder.build()
self.telegram_sender = sender.TelegramSender(
bot=self.application.bot, tg_config=tg_config
)
Expand Down
8 changes: 6 additions & 2 deletions src/tg/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import json
import logging
import os
import re
import time
from typing import Callable, List
Expand All @@ -17,6 +18,9 @@

logger = logging.getLogger(__name__)

# Allow overriding Telegram API base URL (for mocking in tests)
TELEGRAM_API_BASE = os.environ.get("TELEGRAM_BASE_URL", "https://api.telegram.org")


class TelegramSender(Singleton):
def __init__(
Expand Down Expand Up @@ -79,7 +83,7 @@ def send_to_chat_id(self, message_text: str, chat_id: int, **kwargs) -> bool:
# Handle poll sending
poll_options = kwargs["poll_options"]
resp = requests.get(
url=f"https://api.telegram.org/bot{self._tg_config['token']}/sendPoll",
url=f"{TELEGRAM_API_BASE}/bot{self._tg_config['token']}/sendPoll",
params={
"chat_id": chat_id,
"question": poll_options["question"],
Expand Down Expand Up @@ -120,7 +124,7 @@ def send_to_chat_id(self, message_text: str, chat_id: int, **kwargs) -> bool:
if "reply_markup" in kwargs:
payload["reply_markup"] = kwargs["reply_markup"].to_json()
resp = requests.get(
url=f"https://api.telegram.org/bot{self._tg_config['token']}/sendMessage",
url=f"{TELEGRAM_API_BASE}/bot{self._tg_config['token']}/sendMessage",
json=payload,
)
resp.raise_for_status()
Expand Down