diff --git a/example-config.yaml b/example-config.yaml index 9840d2cd..86f64372 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -251,6 +251,40 @@ telegram: api_hash: tjyd5yge35lbodk1xwzw2jstp90k55qz # (Optional) Create your own bot at https://t.me/BotFather bot_token: disabled + + # Telethon connection options. + connection: + # The timeout in seconds to be used when connecting. + timeout: 120 + # How many times the reconnection should retry, either on the initial connection or when + # Telegram disconnects us. May be set to a negative or null value for infinite retries, but + # this is not recommended, since the program can get stuck in an infinite loop. + retries: 5 + # The delay in seconds to sleep between automatic reconnections. + retry_delay: 1 + # The threshold below which the library should automatically sleep on flood wait errors + # (inclusive). For instance, if a FloodWaitError for 17s occurs and flood_sleep_threshold + # is 20s, the library will sleep automatically. If the error was for 21s, it would raise + # the error instead. Values larger than a day (86400) will be changed to a day. + flood_sleep_threshold: 60 + # How many times a request should be retried. Request are retried when Telegram is having + # internal issues, when there is a FloodWaitError less than flood_sleep_threshold, or when + # there's a migrate error. May take a negative or null value for infinite retries, but this + # is not recommended, since some requests can always trigger a call fail (such as searching + # for messages). + request_retries: 5 + + # Device info sent to Telegram. + device_info: + # "auto" = OS name+version. + device_model: auto + # "auto" = Telethon version. + system_version: auto + # "auto" = mautrix-telegram version. + app_version: auto + lang_code: en + system_lang_code: en + # Custom server to connect to. server: # Set to true to use these server settings. If false, will automatically @@ -262,6 +296,7 @@ telegram: ip: 149.154.167.40 # The port to connect to. 443 may not work, 80 is better and both are equally secure. port: 80 + # Telethon proxy configuration. # You must install PySocks from pip for proxies to work. proxy: diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 60dc004f..a4c94a61 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -95,27 +95,43 @@ class AbstractUser(ABC): def _init_client(self) -> None: self.log.debug(f"Initializing client for {self.name}") - device = f"{platform.system()} {platform.release()}" - sysversion = MautrixTelegramClient.__version__ + self.session = self.session_container.new_session(self.name) if config["telegram.server.enabled"]: self.session.set_dc(config["telegram.server.dc"], config["telegram.server.ip"], config["telegram.server.port"]) + if self.is_relaybot: base_logger = logging.getLogger("telethon.relaybot") else: base_logger = logging.getLogger(f"telethon.{self.tgid or -hash(self.mxid)}") - self.client = MautrixTelegramClient(session=self.session, - api_id=config["telegram.api_id"], - api_hash=config["telegram.api_hash"], - loop=self.loop, - app_version=__version__, - system_version=sysversion, - device_model=device, - timeout=120, - base_logger=base_logger, - proxy=self._proxy_settings) + + device = config["telegram.device_info.device_model"] + sysversion = config["telegram.device_info.system_version"] + appversion = config["telegram.device_info.app_version"] + + self.client = MautrixTelegramClient( + session=self.session, + + api_id=config["telegram.api_id"], + api_hash=config["telegram.api_hash"], + + app_version=__version__ if appversion == "auto" else appversion, + system_version=MautrixTelegramClient.__version__ if sysversion == "auto" else sysversion, + device_model=f"{platform.system()} {platform.release()}" if device == "auto" else device, + + timeout=config["telegram.connection.timeout"], + connection_retries=config["telegram.connection.retries"], + retry_delay=config["telegram.connection.retry_delay"], + flood_sleep_threshold=config["telegram.connection.flood_sleep_threshold"], + request_retries=config["telegram.connection.request_retries"], + + proxy=self._proxy_settings, + + loop=self.loop, + base_logger=base_logger + ) self.client.add_event_handler(self._update_catch) @abstractmethod diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index d75de4a8..4cd27018 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -257,10 +257,24 @@ class Config(DictWithRecursion): copy("telegram.api_id") copy("telegram.api_hash") copy("telegram.bot_token") + + copy("telegram.connection.timeout") + copy("telegram.connection.retries") + copy("telegram.connection.retry_delay") + copy("telegram.connection.flood_sleep_threshold") + copy("telegram.connection.request_retries") + + copy("telegram.device_info.device_model") + copy("telegram.device_info.system_version") + copy("telegram.device_info.app_version") + copy("telegram.device_info.lang_code") + copy("telegram.device_info.system_lang_code") + copy("telegram.server.enabled") copy("telegram.server.dc") copy("telegram.server.ip") copy("telegram.server.port") + copy("telegram.proxy.type") copy("telegram.proxy.address") copy("telegram.proxy.port")