Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posix Error : Network is unreachable; How to send a file #152

Closed
rohittp0 opened this issue Feb 5, 2021 · 8 comments
Closed

Posix Error : Network is unreachable; How to send a file #152

rohittp0 opened this issue Feb 5, 2021 · 8 comments

Comments

@rohittp0
Copy link

rohittp0 commented Feb 5, 2021

I have a reliable network connection but still, I am getting this error,

[ConnectionCreator.cpp:896][#1][!ConnectionCreator] [[2001:b28:f23f:f005::a]:443] to DcId{5}: [PosixError : Network is unreachable : 101 : Failed to connect to [[2001:b28:f23f:f005::a]:443]]

Method To Reproduce

Login using tg.login() and entering code.
Wait for login to end then wait for running to finish.

Now restart the program and call tg.login().
Call,
param= { '@type': 'sendMessage', 'chat_id': chat_id, 'input_message_content': { '@type':'inputMessageDocument', 'document': { '@type':'inputFileLocal', 'path': file_path } } tg.call_method("sendMessage",param).wait()

The complete output :
[ 2][t 4][1612541454.562263250][Timer.cpp:67][#1][!Td][&duration > max_duration_] SLOW: [name:change key][duration:2973.3us] [ 2][t 4][1612541454.562390089][TdDb.cpp:323][#1][!Td] Got PRAGMA user_version = 13 [ 2][t 4][1612541454.564440011][AuthDataShared.cpp:109][#1][!Td] DcId{5} [auth_key_id:12701291553040611538][state:OK][created_at:1612541131.000000] [ 2][t 4][1612541454.565639734][Session.cpp:167][#1][!SessionProxy:5:main] Generate new session_id 7671406197548583323 for auth key 12701291553040611538 for main DC5 [ 2][t 4][1612541454.565755128][ConnectionCreator.cpp:896][#1][!ConnectionCreator] [[2001:b28:f23f:f005::a]:443] to DcId{5}: [PosixError : Network is unreachable : 101 : Failed to connect to [[2001:b28:f23f:f005::a]:443]]

@alexander-akhmetov
Copy link
Owner

Hi,

tdlib tries to connect to IPv6 network. Can you try to call tdlib's setOption with prefer_ipv6=False? python-telegram does not support setOption, so you need to call it directly:

    tg = Telegram(...)

    tg.call_method(
        'setOption',
        {
            'name': 'prefer_ipv6',
            'value': {'@type': 'optionValueBoolean', 'value': False},
        },
    )

    tg.login()

@rohittp0
Copy link
Author

rohittp0 commented Feb 6, 2021

Hi,

tdlib tries to connect to IPv6 network. Can you try to call tdlib's setOption with prefer_ipv6=False? python-telegram does not support setOption, so you need to call it directly:

    tg = Telegram(...)

    tg.call_method(
        'setOption',
        {
            'name': 'prefer_ipv6',
            'value': {'@type': 'optionValueBoolean', 'value': False},
        },
    )

    tg.login()

YesI tried this and nothing changed. I can still send text messages but can't send files.
This is how I am trying to send files :

def sendFile(tg,chat_id,file_path,parent_folder="/") :
        param= {
            '@type': 'sendMessageAlbum',
            'chat_id': chat_id,
            'input_message_content': {
                '@type':'inputMessageDocument',
                'document': {
                    '@type':'inputFileLocal',
                    'path': file_path
                },
                'caption' : {
                    'text': {
                        '@type': 'formattedText', 
                        'text': str(path.relpath(file_path,parent_folder))
                    }
                }
            },
            '@extra': {
            	'path': file_path
            }
        }
        return tg.call_method("sendMessage",param)

sendFile(tg,123,"/some/real/path.png","/some").wait()  # This returns none

Calling wait on tg.send_message() also returns none ( even when it had succeeded )
I am pretty new to Python so sorry in advance if it is something I am doing wrong 🙏

@alexander-akhmetov
Copy link
Owner

According to this issue PosixError : Network is unreachable : 101 is not an error but a warning.

Calling wait on tg.send_message() also returns none ( even when it had succeeded )

Yes, wait() does not return anything. call_mehod returns instance of telegram.utils.AsyncResult. AsyncResult.wait() is a blocking call which waits for the response. To get the actual response (update attribute) you need to change your code:

result = sendFile(tg,123,"/some/real/path.png","/some")
result.wait()
print(result.update)  
# or
# print(result.error_info)

@rohittp0
Copy link
Author

rohittp0 commented Feb 6, 2021

Yes, wait() does not return anything. call_mehod returns instance of telegram.utils.AsyncResult. AsyncResult.wait() is a blocking call which waits for the response. To get the actual response (update attribute) you need to change your code:

result = sendFile(tg,123,"/some/real/path.png","/some")
result.wait()
print(result.update)  
# or
# print(result.error_info)

First of all thanks for this 🤝

But still this doesn't solve my issue. If this is just a warning then what is the real problem or how do find it ?

The complete output :
[ 2][t 4][1612541454.562263250][Timer.cpp:67][#1][!Td][&duration > max_duration_] SLOW: [name:change key][duration:2973.3us] [ 2][t 4][1612541454.562390089][TdDb.cpp:323][#1][!Td] Got PRAGMA user_version = 13 [ 2][t 4][1612541454.564440011][AuthDataShared.cpp:109][#1][!Td] DcId{5} [auth_key_id:12701291553040611538][state:OK][created_at:1612541131.000000] [ 2][t 4][1612541454.565639734][Session.cpp:167][#1][!SessionProxy:5:main] Generate new session_id 7671406197548583323 for auth key 12701291553040611538 for main DC5 [ 2][t 4][1612541454.565755128][ConnectionCreator.cpp:896][#1][!ConnectionCreator] [[2001:b28:f23f:f005::a]:443] to DcId{5}: [PosixError : Network is unreachable : 101 : Failed to connect to [[2001:b28:f23f:f005::a]:443]]

This is all the output I am getting. And me not being able to handle the exception rather than just getting a printed output is not helping either.

So what steps would you suggest to debug further ? And is there any better way to send file ?

@alexander-akhmetov
Copy link
Owner

alexander-akhmetov commented Feb 6, 2021

In general, I'd suggest to print result.error_info first, it should contain an error message from tdlib. You can also increase verbosity of the tdlib library to see more logs:

tg = Telegram(
    ...,
    tdlib_verbosity=7,
)

sendMessageAlbum is a method, and sendMessage is another method, which one do you want to use? Also, don't specify @type field in the param dictionary if you use Telegram.call_method. It automatically puts the method name to as a value of the @type key.

If you want just to send a file, you can use sendMessage:

file_path = './file.txt'

params = {
    'chat_id': chat_id,
    'input_message_content': {
        '@type': 'inputMessageDocument',
        'document': {'@type': 'inputFileLocal', 'path': file_path},
    },
}

result = tg.call_method('sendMessage', params)
result.wait()
print('Error: %s' % result.error_info)
print('tdlib response: %s' % result.update)

If you want to send a picture, use sendMessageAlbum:

file_path = './file.png'

params = {
    'chat_id': chat_id,
    'input_message_contents': [
        {
            '@type': 'inputMessagePhoto',
            'photo': {'@type': 'inputFileLocal', 'path': file_path},
            'caption': {
                '@type': 'formattedText',
                'text': 'hello',
            },
        }
    ],
}

result = tg.call_method('sendMessageAlbum', params)
result.wait()
print('Error: %s' % result.error_info)
print('tdlib response: %s' % result.update)

@rohittp0
Copy link
Author

rohittp0 commented Feb 6, 2021

In general, I'd suggest to print result.error_info first, it should contain an error message from tdlib. You can also increase verbosity of the tdlib library to see more logs

I will surely try this.

If you want to send a picture, use sendMessageAlbum

I want to send images and videos but don't want telegram to compress them. So are there any options I can set or should I just use send message ?

@alexander-akhmetov
Copy link
Owner

If you want to send a picture, use sendMessageAlbum

I want to send images and videos but don't want telegram to compress them. So are there any options I can set or should I just use send message ?

I think, the example with sendMessage is what you need :) You can also have a look at sendMessage and inputMessageDocument, but the code in the example should already work and send files.

@rohittp0
Copy link
Author

rohittp0 commented Feb 7, 2021

@alexander-akhmetov With your help, I finally solved the problem

def sendFile(tg,chat_id,file_path,parent_folder="/") :
        param= {
            'chat_id': chat_id,
            'input_message_content': {
                '@type':'inputMessageDocument',
                'document': {
                    '@type':'inputFileLocal',
                    'path': file_path
                },
                'caption' : {
                    '@type': 'formattedText', 
                    'text': str(path.relpath(file_path,parent_folder))
                }
            },
            '@extra': {
            	'path': file_path
            }
        }
        return tg.call_method("sendMessage",param)

This is the working code. The problem was I used :

'caption' : {
     'text': {
         '@type': 'formattedText', 
         'text': str(path.relpath(file_path,parent_folder))
      }
}

@alexander-akhmetov alexander-akhmetov changed the title Posix Error : Network is unreachable. Posix Error : Network is unreachable; How to send a file Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants