Forums

Telegram client using telethon

SORRY FOR MY ENGLISH!))

Trying to launch telegram client on pythonanywhere hosting using flask

Loaded all necessary libraries (see photo1)

On this site created an api-application, got it app_id and api_hash

Further, I made a flask application with this code (for test)

@app.route('/')
def hello_world():
    #------------------------If an error occurred during import------------
    try:
        from telethon import TelegramClient, sync
    except Exception as e:
        return 'IMPORT ERROR: ' + str(e)
    #------------------------we catch it-----------------------------------

    api_id = API_ID_FROM_MY_TELEGRAM_ORG #Тут подставляю свое
    api_hash = API_HASH_CODE_FROM_MY_TELEGRAM_ORG #Тут подставляю свое

    #----------If an error occurred while creating the object--------------
    try:
        client = TelegramClient('test_session', api_id, api_hash)
        return 'Succes!'
    except Exception as e:
        return 'OBJECT ERROR: ' + str(e)
    #------------------------we catch it-----------------------------------

When you start and open the application in the browser, the following error is displayed (in text form):

OBJECT ERROR: database is locked

This text corresponds to the last try/catch construction, so an error occurs when trying to create an object client = TelegramClient('test_session', api_id, api_hash)

What is this exception and how in my case to fight it?

From your screenshot of the libraries that you loaded, it looks like you've just pulled down the source code -- if you want to use a library, you should use pip3.7 (or similar) to install it. Check out this help page.

Ok I've made forced-reinstall of telethon like that

pip3.6 install --user --upgrade --force-reinstall telethon

And this library and other required have appeared in ./.local/lib/python3.6/site-packages

But there is error

IMPORT ERROR: No module named 'telethon'

What now?

I've added that:

import sys
sys.path.insert(0, '.local/lib/python3.6/site-packages')

but there is this error again

OBJECT ERROR: database is locked

And here is the solution)))

https://stackoverflow.com/questions/54369315/telethon-operationalerror-database-is-locked

But then an other error appears:

Errno Connection to Telegram failed 6 time(s)

You don't need to add .local/lib/python3.6/site-packages to your sys.path if you're using Python 3.6 -- it's added automatically. I suggest you remove the code that adds it, and use pip3.7 to install the packages, because your website is set up to use Python 3.7. I'm not sure that's the cause of the problem, but it probably isn't helping.

Ok, I've made that

pip3.7 install --user --upgrade --force-reinstall telethon

but there is an error, as in the post above

OBJECT ERROR: [Errno Connection to Telegram failed %d time(s)] 6

How to deal with this problem?

Well, I found that, about proxy)

https://toster.ru/q/642176

Is it right to do like that?

proxy=(socks.SOCKS5, 'proxy.server', 3128)
client = TelegramClient('session_name', api_id, api_hash, proxy = proxy)

If yes, there is a problem with reloading my web app

Yes, I think that's the issue :-) It sounds like you need to configure telethon to use the proxy -- the help page the person in that forum linked to has the details.

is there a solution for using telegramm proxy on pythonanywhere?

It depends on which telegram library you are using. Which one do you want to use?

the topic is about 'client using telethon', I am also trying to set up telethon through 'proxy.server',3128 or my own proxy

What code are you using when using our default proxy server? And what error messages are you getting, if any?

Using your own proxy won't work from a free PythonAnywhere account, as it would not be on our whitelist.

client = TelegramClient('session_name', config.api_id, config.api_hash, proxy=(socks.SOCKS5, 'proxy.server',3128))

results

ConnectionError: Connection to Telegram failed 5 time(s)

Could you share the full traceback of the error with us?

I am sorry, of cource :

09:24 ~/bot $ python3 tele.py 
Traceback (most recent call last):
  File "tele.py", line 15, in <module>
    client.start()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 132, in start
    else self.loop.run_until_complete(coro)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 139, in _start
    await self.connect()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 472, in connect
    if not await self._sender.connect(self._connection(
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 125, in connect
    await self._connect()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 250, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

You cannot use the mtproto connection type from a free account on PythonAnywhere. Free accounts can only connect out of PythonAnywhere using http(s). Check the documentation for telethon to see how to use an http-based connection instead.

I tried

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.tcpfull.ConnectionTcpFull)

error still the same

[edit by admin: formatting]

.

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.tcpfull.ConnectionTcpFull, proxy=(socks.HTTP, 'proxy.server',3128))
client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.tcpfull.ConnectionTcpFull, proxy=(socks.SOCKS5, 'proxy.server',3128))

with same result

[edit by admin: formatting]

This one should have worked:

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.tcpfull.ConnectionTcpFull, proxy=(socks.HTTP, 'proxy.server',3128))

Could you try it again and post the full error message that you get from it?

.

client.start()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 132, in start
    else self.loop.run_until_complete(coro)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 139, in _start
    await self.connect()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 472, in connect
    if not await self._sender.connect(self._connection(
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 125, in connect
    await self._connect()
  File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 250, in _connect
    raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)

It looks to me like it is still trying to use mtproto rather than HTTP. What happens if you remove the connection=connection.tcpfull.ConnectionTcpFull parameter?

as I wrote above, all connection types give the same error traceback. I tried 4 nonMtproto instructions:

client = TelegramClient('session_name', config.api_id,
        config.api_hash, connection=connection.tcpfull.ConnectionTcpFull,
        proxy=(socks.HTTP, 'proxy.server',3128))   
client =
        TelegramClient('session_name', config.api_id, config.api_hash,
        connection=connection.tcpfull.ConnectionTcpFull,
        proxy=(socks.SOCKS5, 'proxy.server',3128))   
client =
        TelegramClient('session_name', config.api_id, config.api_hash,
        connection=connection.tcpfull.ConnectionTcpFull)
client = TelegramClient('session_name', config.api_id, config.api_hash,  proxy=(socks.SOCKS5, 'proxy.server',3128))

I suspect that TcpConnectionFull is using mtproto,. then. Try using telethon.network.connection.http.ConnectionHttp instead.

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.http.ConnectionHttp, proxy=(socks.SOCKS5, 'proxy.server',3128))

the same error

Traceback (most recent call last): File "/home/codefather/bot/tele.py", line 15, in <module> client.start() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 132, in start else self.loop.run_until_complete(coro) File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete return future.result() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 139, in _start await self.connect() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 472, in connect if not await self._sender.connect(self._connection( File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 125, in connect await self._connect() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 250, in _connect raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) ConnectionError: Connection to Telegram failed 5 time(s)

Can we take a look at your code? We can see it from our admin interface, but we always ask for permission first.

yes. of course script is at "/home/codefather/bot/tele.py" thank you

Could you try one more time with

client = TelegramClient('session_name', config.api_id, config.api_hash,
    proxy=(socks.HTTP, 'proxy.server',3128))

....?

Hi Giles, I'm having the same issue... I tried your last recommendation: client = TelegramClient('session_name', config.api_id, config.api_hash, proxy=(socks.HTTP, 'proxy.server',3128))

but didn't work...

I tried

client = TelegramClient('session_name', config.api_id, config.api_hash,  proxy=(socks.HTTP, 'proxy.server',3128))

and

client = TelegramClient('session_name', config.api_id,connection=connection.ConnectionHttp, config.api_hash,  proxy=(socks.HTTP, 'proxy.server',3128))

but the error is the same :(

Let's see if we can get more information by increasing the logging level for Telethon; could you try adding this to the start of your script?

import logging
logging.basicConfig(level=logging.DEBUG)

done

DEBUG:asyncio:Using selector: EpollSelector INFO:telethon.network.mtprotosender:Connecting to 149.154.167.51:443/TcpFull... DEBUG:telethon.network.mtprotosender:Connection attempt 1... WARNING:telethon.network.mtprotosender:Attempt 1 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) DEBUG:telethon.network.mtprotosender:Connection attempt 2... WARNING:telethon.network.mtprotosender:Attempt 2 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) DEBUG:telethon.network.mtprotosender:Connection attempt 3... WARNING:telethon.network.mtprotosender:Attempt 3 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) DEBUG:telethon.network.mtprotosender:Connection attempt 4... WARNING:telethon.network.mtprotosender:Attempt 4 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) DEBUG:telethon.network.mtprotosender:Connection attempt 5... WARNING:telethon.network.mtprotosender:Attempt 5 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) DEBUG:telethon.network.mtprotosender:Connection attempt 6... WARNING:telethon.network.mtprotosender:Attempt 6 at connecting failed: GeneralProxyError: Socket error: 403: Forbidden

[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy) Traceback (most recent call last): File "/home/codefather/bot/tele.py", line 17, in <module> client.start() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 132, in start else self.loop.run_until_complete(coro) File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete return future.result() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 139, in _start await self.connect() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 472, in connect if not await self._sender.connect(self._connection( File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 125, in connect await self._connect() File "/home/codefather/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 250, in _connect raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries)) ConnectionError: Connection to Telegram failed 5 time(s)

Which code are you using to construct the client when you get that error?

as you recomended above:

client = TelegramClient('session_name', config.api_id, config.api_hash,  proxy=(socks.HTTP, 'proxy.server',3128))

That will not work because it is still using mtproto. Use the version that has the HttpConnection specified.

as you an see in the posts before, i used many configurations as

connection=connection.ConnectionHttp

Ok. The last time you posted a traceback that was clearly using the HTTP connection type, you said you'd used this code:

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.http.ConnectionHttp, proxy=(socks.SOCKS5, 'proxy.server',3128))

try

client = TelegramClient('session_name', config.api_id, config.api_hash, connection=connection.http.ConnectionHttp, proxy=(socks.HTTP, 'proxy.server',3128))

as I told early, I tried and socks.HTTP and SOCKS5 - no changes

hello, Telethon uses MTProto under the hood, so there is no way of using it in free pythonanywhere account.

It has to be http connection using our proxy to work on free account.

from telethon.sync import TelegramClient

api_id = id api_hash = hash

client = TelegramClient('session_name', api_id, api_hash)

client.connect()

Почему видеть это ошибка?

enter image description here

The library that you are using has created an SQLite database -- it looks like it uses this to store sessions -- and the database is locked. If you're running two instances of your code at the same time, then that might happen if they both try to access the database at the same time. Alternatively, it might have locked the database and then crashed at some time in the past, leaving a lock file on disk. If you don't care about the library's session cache, then you can probably just find the database file and delete it.

Hi, I am getting a telegram error can anybody help..? Below is error:

Attempt 1 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 2 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 3 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 4 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 5 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Attempt 6 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443) Traceback (most recent call last): File "/home/harkunwarkhera/tele.py", line 5, in <module> with TelegramClient('eth_kunwar', api_id, api_hash) as client: File "/home/harkunwarkhera/.local/lib/python3.8/site-packages/telethon/helpers.py", line 184, in _sync_enter return loop.run_until_complete(self.aenter()) File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete return future.result() File "/home/harkunwarkhera/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 713, in aenter return await self.start() File "/home/harkunwarkhera/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 140, in _start await self.connect() File "/home/harkunwarkhera/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 513, in connect if not await self._sender.connect(self._connection( File "/home/harkunwarkhera/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 127, in connect await self._connect() File "/home/harkunwarkhera/.local/lib/python3.8/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)

How is your bot configured? Are you sure that you are actually running the code that configures the connection to use the proxy?

Hi I am using api_id and api_hash this is my code:

from telethon.sync import TelegramClient import time api_id = xx api_hash = xxx with TelegramClient('eth_kunwar', api_id, api_hash) as client: print('enter number') destination_user_username = '@xx' entity = client.get_entity(destination_user_username) while True: try: client.send_message(entity = entity,message='hello') time.sleep(30) except: print('error') client.run_until_disconnected() client.run_until_disconnected()

telegram client line:

with TelegramClient('eth_kunwar', api_id, api_hash) as client:

Your client is using mtprotosender, which will not work on PythonAnywhere in a free account. Check the documentation of the client you're using to see whether you can get it to use http instead and, if you can, then also make sure that you have configured it to use http through the PythonAnywhere proxy.