tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Why I cannot create this json format to send text? #2843

Closed SnapdragonLee closed 5 months ago

SnapdragonLee commented 6 months ago

Because of the simple event handler architechture in python, I'm using tdjson to send text/file/photo to myself.

td_send({
            "@type": "sendMessage",
            "chat_id": '',
            "input_message_content": {
                "@type": "inputMessageText",
                "text": {
                    "@type": "formattedText",
                    "text": "Hello, this is a test message."
                }
            }
        })

Why there's no return/sending behavior or any traceback error after excuted this command? Actually, this text has not been sent to my chat.

SnapdragonLee commented 6 months ago

If I change chat_id to any other chats, or change type to photo/file no behaviors either.

levlam commented 6 months ago

You can use only identifiers received from TDLib, therefore you must receive the chat_id from TDLib before you can use it.

Additionally, you can check TDLib logs to see requests received by TDLib and their responses.

SnapdragonLee commented 6 months ago

You can use only identifiers received from TDLib, therefore you must receive the chat_id from TDLib before you can use it.

Additionally, you can check TDLib logs to see requests received by TDLib and their responses.

Thanks for the reply. From my side, I could see the identifiers in a 10 digits format, and I have handled the json event each time I send to myself on other clients.

For the TDLib, what level of logs should I use? level 2? I may have a try to redirect stdout to a file.

levlam commented 6 months ago

You need level 3 or bigger to see all requests in the log.

SnapdragonLee commented 6 months ago

OK, I have do some tests on daemon thread.

time.sleep(3)
    td_send({
        # '@type': 'loadChats',
        # 'limit': 1

        '@type': 'getChat',
        'chat_id': {19xxxxxxxxx}
    })

    time.sleep(3)
    td_send({
        "@type": "sendMessage",
        "chat_id": 19xxxxxxxxx,
        "input_message_content": {
            "@type": "inputMessageText",
            "text": {
                "@type": "formattedText",
                "text": "Hello, this is a test message."
            }
        }
    })

The first one, loadChats and limit set to 1, I could send this message. But for the second, openChat or getChat, the log in severity 3 reports that Chat is not found with a 400 return. Why I got this thing? Just a little bit confusing.

levlam commented 6 months ago

A "Chat not found" error means that you are trying to pass a hardcoded chat identifier to TDLib. You can only use identifiers received from the same TDLIb instance programmatically.

SnapdragonLee commented 6 months ago

I'm sorry that I may not get you. I just wanna send something to 'Saved Message' by TdLib. How could I manage this before sent sendMessage to target chat_id: 19xxxxxxxxx? What function should I call for receiving the chat_id?

levlam commented 6 months ago

You need to use getMe to get the current user, then use createPrivateChat with received user_id to create the chat with self, and then use received chat identifier to send messages.