telegram: Support to connect to test environment
patch 219dba6a312f4a8d40668f28f81baa8a8a535bfe
Author: E. Bosch <presidev@AT@gmail.com>
Date: Tue Feb 22 02:37:33 CET 2022
* telegram: Support to connect to test environment
Add options to configure this
diff -rN -u old-irgramd/irgramd new-irgramd/irgramd
--- old-irgramd/irgramd 2024-11-22 21:53:42.525169794 +0100
+++ new-irgramd/irgramd 2024-11-22 21:53:42.525169794 +0100
@@ -77,7 +77,11 @@
tornado.options.define('pam', default=False, help='Use PAM for IRC authentication, if not set you should set `irc_password`')
tornado.options.define('pam_group', default=None, metavar='GROUP', help='Unix group allowed if `pam` enabled, if empty any user is allowed')
tornado.options.define('phone', default=None, metavar='PHONE_NUMBER', help='Phone number associated with the Telegram account to receive the authorization codes if necessary')
- tornado.options.define('port', default=None, metavar='PORT', help='Port to listen on. (default 6667, default with TLS 6697)')
+ tornado.options.define('port', default=None, metavar='PORT', help='Port to listen on for IRC. (default 6667, default with TLS 6697)')
+ tornado.options.define('test', default=False, help='Connect to Telegram test environment')
+ tornado.options.define('test_datacenter', default=2, metavar='DATACENTER_NUMBER', help='Datacenter to connect to Telegram test environment')
+ tornado.options.define('test_host', default=None, metavar='HOST_IP', help='Host to connect to Telegram test environment (default: use a internal table depending on datacenter)')
+ tornado.options.define('test_port', default=443, metavar='PORT', help='Port to connect to Telegram test environment')
tornado.options.define('tls', default=False, help='Use TLS/SSL encrypted connection for IRC server')
tornado.options.define('tls_cert', default=None, metavar='CERTFILE', help='IRC server certificate chain for TLS/SSL, also can contain private key if not defined with `tls_key`')
tornado.options.define('tls_key', default=None, metavar='KEYFILE', help='IRC server private key for TLS/SSL')
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2024-11-22 21:53:42.525169794 +0100
+++ new-irgramd/telegram.py 2024-11-22 21:53:42.529169788 +0100
@@ -21,6 +21,13 @@
from irc import IRCUser
from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration
+# Test IP table
+
+TEST_IPS = { 1: '149.154.175.10',
+ 2: '149.154.167.40',
+ 3: '149.154.175.117'
+ }
+
# Telegram
class TelegramHandler(object):
@@ -31,6 +38,10 @@
self.api_id = settings['api_id']
self.api_hash = settings['api_hash']
self.phone = settings['phone']
+ self.test = settings['test']
+ self.test_dc = settings['test_datacenter']
+ self.test_ip = settings['test_host'] if settings['test_host'] else TEST_IPS[self.test_dc]
+ self.test_port = settings['test_port']
self.media_cn = 0
self.irc = irc
self.authorized = False
@@ -52,8 +63,12 @@
os.makedirs(self.telegram_session_dir)
# Construct Telegram client
- telegram_session = os.path.join(self.telegram_session_dir, 'telegram')
- self.telegram_client = telethon.TelegramClient(telegram_session, self.api_id, self.api_hash)
+ if self.test:
+ self.telegram_client = telethon.TelegramClient(None, self.api_id, self.api_hash)
+ self.telegram_client.session.set_dc(self.test_dc, self.test_ip, self.test_port)
+ else:
+ telegram_session = os.path.join(self.telegram_session_dir, 'telegram')
+ self.telegram_client = telethon.TelegramClient(telegram_session, self.api_id, self.api_hash)
# Initialize Telegram ID to IRC nick mapping
self.tid_to_iid = {}
@@ -68,7 +83,10 @@
self.telegram_client.add_event_handler(handler, event)
# Start Telegram client
- await self.telegram_client.connect()
+ if self.test:
+ await self.telegram_client.start(self.phone, code_callback=lambda: str(self.test_dc) * 5)
+ else:
+ await self.telegram_client.connect()
while not await self.telegram_client.is_user_authorized():
self.logger.info('Telegram account not authorized, you must provide the Login code '