Hi everyone, I need help to connect to Telegram by Telethon. Just learn python for several days and have don't undestand how to male correct.
My code:
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerEmpty, InputGeoPoint
import pandas as pd
from telethon import functions
import telethon
from telethon import events
from config import api_id, api_hash, phone_number, CHANNELS, GROUP_CHAT_ID, allowed_characters
session_name = 'session'
client = TelegramClient(session_name, api_id, api_hash, proxy=('http', 'proxy.server', 3128))
client.connect()
CHANNELS = [str(i) for i in CHANNELS]
if not client.is_user_authorized():
client.send_code_request(phone_number)
print('Enter the code sent to your number: ')
code = input()
try:
client.sign_in(phone_number, code)
except telethon.errors.SessionPasswordNeededError:
password = input("Enter password: ")
client.sign_in(phone = phone_number, code = code, password = password)
def check_message(text):
text = text.split()
filter_words = []
for word in text:
if len(word) == 8:
check = 1
for j in word:
if j in allowed_characters:
continue
else:
check = 0
break
if check == 1:
filter_words.append(word)
return filter_words
@client.on(events.NewMessage())
async def normal_handler(event):
try:
message = event.message.to_dict()
print(message)
if '-100' + str(message['peer_id']['channel_id']) in CHANNELS:
text = message['message']
filter_words = check_message(text)
print(filter_words)
if filter_words != []:
chat = await client.get_entity(GROUP_CHAT_ID)
for i in filter_words:
await client.send_message(entity = chat, message = i)
except Exception as e:
print(e)
pass
print(client, 'started')
client.run_until_disconnected()
I receive
Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)
Traceback (most recent call last):
File "/home/DienUa/BotParser/main.py", line 13, in <module>
client.connect()
File "/home/DienUa/.local/lib/python3.10/site-packages/telethon/sync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/home/DienUa/.local/lib/python3.10/site-packages/telethon/client/telegrambaseclient.py", line 525, in connect
if not await self._sender.connect(self._connection(
File "/home/DienUa/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 127, in connect
await self._connect()
File "/home/DienUa/.local/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 253, in _connect
raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)
paid plan: Hacker