Skip to content

Commit d1f831b

Browse files
author
0rtemk
committed
hotfix(tgbot): add api connect with proxy
1 parent b2d1c52 commit d1f831b

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

tgbot/uni_eduplatform_bot.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
# Load .env
1111
load_dotenv()
1212

13+
SOCKS5_PROXY = os.getenv('SOCKS5_PROXY')
14+
15+
proxies = None
16+
if SOCKS5_PROXY:
17+
proxies = {
18+
'http': SOCKS5_PROXY,
19+
'https': SOCKS5_PROXY,
20+
}
21+
1322
TOKEN = os.getenv('TG_BOT_TOKEN')
1423
TEACHER_CODE = os.getenv('TEACHER_CODE')
1524
PLATFORM_ADDRESS_SERVER = os.getenv('PLATFORM_ADDRESS_SERVER')
@@ -21,7 +30,13 @@
2130
if not PLATFORM_ADDRESS_SERVER:
2231
raise RuntimeError('PLATFORM_ADDRESS_SERVER not found in environment (.env: PLATFORM_ADDRESS_SERVER)')
2332

24-
bot = telebot.TeleBot(TOKEN)
33+
if SOCKS5_PROXY:
34+
bot = telebot.TeleBot(
35+
TOKEN,
36+
proxy=SOCKS5_PROXY
37+
)
38+
else:
39+
bot = telebot.TeleBot(TOKEN)
2540

2641
# In-memory registration/change-password state per chat
2742
states: Dict[int, Dict[str, Any]] = {}
@@ -115,7 +130,7 @@ def graphql_query(query: str, variables: dict = None) -> dict:
115130
if variables is not None:
116131
payload['variables'] = variables
117132
try:
118-
r = requests.post(url, json=payload, headers=headers, timeout=10)
133+
r = requests.post(url, json=payload, headers=headers, timeout=10, proxies=proxies)
119134
try:
120135
r.raise_for_status()
121136
except requests.HTTPError as http_err:
@@ -389,8 +404,4 @@ def registration_flow(message):
389404

390405

391406
if __name__ == '__main__':
392-
while True:
393-
try:
394-
bot.polling(none_stop = True)
395-
except Exception as e:
396-
time.sleep(2)
407+
bot.polling()

0 commit comments

Comments
 (0)