vysheng / tg

telegram-cli
GNU General Public License v2.0
6.48k stars 1.53k forks source link

how to send secret chat message from command line #918

Open j-ed opened 8 years ago

j-ed commented 8 years ago

I've compiled the telegram-cli binary, based on todays source code. If I try to send a secure chat message from the command line, it usually fails and an error message is displayed. (Using telegram-cli in interactive mode works like a charme)

telegram-cli -W -e 'msg !_user Hello world'
FAIL: 38: can not parse arg #1

Based on what I've found on the Internet, in the documentation or on GitHub, I've tried to send a message using several different ways, but without success:

1. telegram-cli -W -e 'msg !_TOM Hello world'
   -> this doesn't work.

2. telegram-cli -W -e "create_secret_chat\nmsg !_TOM Hello world"
   -> also \n is most likely interpreted correctly, this doesn't work because '!'
      is not mask (-bash: !_user: event not found)

3. telegram-cli -W -e "create_secret_chat\nmsg \!_TOM Hello world"
   -> also \n is most likely interpreted correctly and ! is also mask correctly
      this doesn't work because \!_user couldn't most likely be found.

4. telegram-cli -W -e 'create_secret_chat\nmsg !_TOM Hello world'
   -> this doesn't work because '\n' is most likely not recognized as new-line.

5. telegram-cli -W -e "create_secret_chat\nmsg !_user@id Hello world"
   -> this doesn't work either, because the  '!' is not mask correctly
      (-bash: !_user: event not found)

I've also tried to separate the two commands using a semi-colon or using '-e' multiple times, but without success. So if someone could shed some light on how the command need to be called, I would appreciate it.

beMoD commented 8 years ago

Using daemon mode works:

bemod@wohnpi:~$ /usr/bin/telegram-cli -W -k /etc/telegram-cli/server.pub -vvvRC -d
bemod@wohnpi:~$ echo "create_secret_chat be_handy" | nc -q 1 localhost 1234
bemod@wohnpi:~$ echo 'msg  !_be_handy hurz' | nc -q 1 localhost 1234
bemod@wohnpi:~$ echo "safe_quit" | nc -q 1 localhost 1234

results in:

 *** restarting query 3698697603188564
 *** outbound rpc connection from dc #1 becomed ready
 *** outbound rpc connection from dc #3 becomed ready
 *** adjusting CLOCK_MONOTONIC delta to -0.609796
 *** bad_msg_notification: msg_id = 6237134855663813632, seq = 2, error = 64
 *** bad_msg_notification: msg_id = 6237134855663813632, seq = 2, error = 64
 *** failing session -3866091081797299784
 *** adjusting CLOCK_MONOTONIC delta to -0.604828
 *** outbound rpc connection from dc #5 becomed ready
 *** adjusting CLOCK_MONOTONIC delta to 0.659910
 *** Accepting incoming connection
 *** Read from incoming connection
 *** Closing incoming connection
 *** lua: attempt to call a nil value
 *** lua: attempt to call a nil value
Secret chat !_be_handy updated access_hash
 *** Secret chat in ok state
 *** lua: attempt to call a nil value
Secret chat !_be_handy updated status
[20:49]  !_be_handy be Pi updated layer to 17
 *** qts = 991048472, qts_count = 1
[20:49]  !_be_handy be handy updated layer to 23
 *** Accepting incoming connection
 *** Read from incoming connection
 *** Closing incoming connection
[20:49]   !_be_handy <<< hurz
 *** Accepting incoming connection
 *** Read from incoming connection
 *** Closing incoming connection
[warn] Epoll MOD(4) on fd 12 failed.  Old events were 6; read change was 2 (del); write change was 0 (none): Bad file descriptor
[warn] Epoll MOD(1) on fd 12 failed.  Old events were 6; read change was 0 (none); write change was 2 (del): Bad file descriptor
All done. Exit
ยดยดยด
j-ed commented 8 years ago

I've run several tests now on my server but running telegram-cli as a deamon doesn't solve the problem for me. As soon as I send the create_secret_chat command to the deamon, it returns the following error:

ANSWER 31
FAIL: 38: can not parse arg #1

The deamon itself displays the following messages:

 *** Accepting incoming connection
 *** Read from incoming connection
 *** Closing incoming connection
beMoD commented 8 years ago

Have you used the -W argument to run the daemon? What does contact_list show?

j-ed commented 8 years ago

@beMoD You're right, -W did the trick. Thank you very much :-) Nevertheless shouldn't it be possible to send secret chat messages on the command line too?

j-ed commented 8 years ago

I just realized that using the create_secret_chatcommand in a batch file always creates new chats. One way would be to read the reply of that command and save the chat id of the created chat for the future, e.g. '!_TOM#1'. Is there any way to show a list of already created chats before a create_secret_chat command is initiated again?

beMoD commented 8 years ago

I currently am not able to test, but perhaps dialog_list?

j-ed commented 8 years ago

Unfortunately not, because it only returns

ANSWER 19
User TOM: 0 unread

I've now created a wrapper script which first eavaluates if a secret chat should be initiated. Next it tries to send a secret chat message. If that fails with the following error

# echo "msg !_TOM#1 'Hello world!'" | nc -q 1 localhost 1234
ANSWER 38
FAIL: 22: secret chat not in ok state

it sends a create_secret_chat TOM command first and resends the previous message again.

beMoD commented 8 years ago

Is it a shell script? If so, I would like to have it :-)

j-ed commented 8 years ago

Here it is: send-telegram-message.txt

beMoD commented 8 years ago

Thank you :-)

noplanman commented 7 years ago

Hi @j-ed and @beMoD

Kind of on the same topic, how can I read a message received through a secret chat? On the CLI I see this when a message is received:

*** 1497377792.640274 qts = 2353109, qts_count = 1
{"id": "04000000d01e7a01cfd95c01baf25c6b8322f490a0f06f92", "event": "message"}

But how do I actually get the contents of it? I've tried with get_message <msg-id> and view <msg-id> but both return nothing useful.

Hope you can help me out, thanks! ๐Ÿ˜‡

j-ed commented 7 years ago

@noplanman I think you're misinterpreting the given id, it's not the a message id from my point of view.

The message number display need to be enabled first by executing set msg_num 1. Then you can show all available message by executing history <user-name>. The first number in each row is the message id which can be used to select a specific message, e.g. get_message <msg-id>.

noplanman commented 7 years ago

With normal messages it works fine, but not with secret chat ones. I see the messages in history !_<user-name> but I can't access the decrypted version ๐Ÿ˜•

Have you tried and tested it with a secret chat? What version of tg-cli are you using?

> version
Telegram-cli version 1.4.1 (uses tgl version 2.1.0)
uses libopenssl for encryption
j-ed commented 7 years ago

@noplanman I don't now how it works with secret chats. BTW, I think this is not a general issue ticket to ask non-related questions. Best would be to open a new ticket for your question.

noplanman commented 7 years ago

I think this is not a general issue ticket

Yes I know, but since the topic was about sending secret chats via CLI, it fit quite well ๐Ÿ˜‰

Will do some more testing and then possibly make a new issue, thanks for your responses by the way ๐Ÿ‘