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

Py tg For New TG #109

Open
JonahSabri opened this issue Jan 17, 2017 · 19 comments
Open

Py tg For New TG #109

JonahSabri opened this issue Jan 17, 2017 · 19 comments

Comments

@JonahSabri
Copy link

Hi.
please create new pytg for new tg >> https://valtman.name/telegram-cli

thanks

@StrangePers
Copy link

It would be awsome!

@elisaado
Copy link

I agree :D

@luckydonald
Copy link
Owner

It might be possible to use it already, sending the required json (TL schema) as string in raw function.

Note: I haven't checked.

@StrangePers
Copy link

StrangePers commented Jan 23, 2017

I checked and it doesn't work. Telegram-cli consider it as improper command, triggers exception and exit. I tried to send json right into socket by using sendall(b(json)) - it returned, but then hang on receiving answer from server. Server didn't answer. Looks like server was waiting for something else... Can you help with that? I just need working raw command..
That json succeed if sent in Telegram-cli console. I used the same one in pytg with no luck:
{"ID":"SendMessage", "chat_id_":1007779878, "reply_to_message_id_":0, "disable_notification_":0, "from_background_":0, "input_message_content_":{"ID":"InputMessageText", "text_":"Test text here", "disable_web_preview_":0, "clear_draft_":0, "entities_":[]}}

@luckydonald
Copy link
Owner

You could also connect with netcat to the CLI (assuming localhost:4458)

$ nc localhost 4458

and enter the commands there (obviously sending with enter \n) and see if that works.

@StrangePers
Copy link

It works through the netcat!
What should I fix in code to do the same thing in pytg?

{"ID":"SendMessage", "chat_id_":-191132333, "reply_to_message_id_":0, "disable_notification_":0, "from_background_":0, "input_message_content_":{"ID":"InputMessageText", "text_":"Test text here", "disable_web_preview_":0, "clear_draft_":0, "entities_":[]}}
ANSWER 494
{"ID": "Message", "can_be_edited_": false, "id_": 1572864000000001, "send_state_": {"ID": "MessageIsBeingSent"}, "sender_user_id_": 206502179, "via_bot_user_id_": 0, "chat_id_": -191132333, "is_post_": false, "can_be_deleted_": true, "edit_date_": 0, "date_": 1485196757, "forward_info_": false, "reply_to_message_id_": 0, "content_": {"ID": "MessageText", "text_": "Test text here", "entities_": [], "web_page_": false}, "ttl_": 0, "views_": 0, "ttl_expires_in_": 0.0, "reply_markup_": false}

@luckydonald
Copy link
Owner

I'm puzzled.
Could you send the full log in debug mode (see readme) of what happens when you send that with raw?

@StrangePers
Copy link

StrangePers commented Jan 24, 2017

in telegram-cli failes this code


void in_command_decref (struct in_command *cmd) {
  if (!--cmd->refcnt) {
    free (cmd->line); 
    if (cmd->ev && !--cmd->ev->refcnt) {
      free (cmd->ev);
    }
    free (cmd);
    allocated_commands --;
    if (!allocated_commands && safe_quit) {
      do_halt (0);
    }
  } else {
    assert (cmd->refcnt > 0);      //<---------- this one failes
  }
}

its /tdcli/interface.c:719: in_command_decref: Assertion `cmd->refcnt > 0' failed.

I'll try once again from scratch to see if this is caused by some docker settings maybe.
UPD. All stayed the same. double checked docker CMD. And it really works with netcat, but gives assert (cmd->refcnt > 0); if sent from socket.sendall(b(json))

@StrangePers
Copy link

StrangePers commented Jan 24, 2017

That's what pytg tells as I mentioned above - the problem is in assert from telegram-cli. :

DEBUG:pytg.sender:Parsing raw: Argument {"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}} - <command> (needed)
DEBUG:pytg.sender:Sending command >[disable_preview]  {"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}
<
DEBUG:pytg.sender:Socket Connected.
DEBUG:pytg.sender:All Sent.
Traceback (most recent call last):
  File "R:\py-cli\pytg_home\pytg\examples\command_who_am_i.py", line 14, in <module>
    res = s.raw('{"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}')
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 630, in command_alias
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 318, in execute_function
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 507, in _do_send
pytg.exceptions.ConnectionError: Remote end closed
INFO:pytg.sender:Quit Sending. Not allowing sending anymore.
WARNING:pytg.sender:Terminating currently sending request.
INFO:pytg.sender:Currently not Sending.

debug of telegram-cli:


 *** 1485274919.000000 Accepting incoming connection

 *** 1485274919.000000 Read from incoming connection

telegram-cli: /root/tdcli/interface.c:719: in_command_decref: Assertion `cmd->refcnt > 0' failed.
SIGNAL received
 h/bin/telegram-cli(print_backtrace+0x1c)[0x80e72c]
/bin/telegram-cli(termination_signal_handler+0x56)[0x80e7a6]
/lib/x86_64-linux-gnu/libc.so.6(+0x350e0)[0x7fb5056920e0]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0x37)[0x7fb505692067]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x148)[0x7fb505693448]
/lib/x86_64-linux-gnu/libc.so.6(+0x2e266)[0x7fb50568b266]
/lib/x86_64-linux-gnu/libc.so.6(+0x2e312)[0x7fb50568b312]
/bin/telegram-cli[0x814364]
/bin/telegram-cli[0x821176]
/usr/lib/x86_64-linux-gnu/libevent-2.0.so.5(+0x1b2aa)[0x7fb50731f2aa]
/usr/lib/x86_64-linux-gnu/libevent-2.0.so.5(event_base_loop+0x7fc)[0x7fb5073143dc]
/bin/telegram-cli(net_loop+0x61)[0x821a31]
/bin/telegram-cli(loop+0xde)[0x821d0e]
/bin/telegram-cli(main+0x2aa)[0x80c92a]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fb50567eb45]
/bin/telegram-cli[0x80cdec]

@StrangePers
Copy link

StrangePers commented Jan 24, 2017

I thought, that [disable_preview] is not needed and tried to send just json as string right into socket.sendall. pytg log here:

DEBUG:pytg.sender:Parsing raw: Argument {"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}} - <command> (needed)
DEBUG:pytg.sender:Sending command >[disable_preview]  {"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}
<
DEBUG:pytg.sender:Sending command edited >{"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}<
DEBUG:pytg.sender:Socket Connected.
DEBUG:pytg.sender:All Sent.

DEBUG:pytg.sender:Sending command edited is the real command that I send.
After DEBUG:pytg.sender:All Sent. pytg just hangs. (Netcat receives the answer in this case. You can see it in my upper comments.). Telegram-cli doesn't send anything. Just logs:

*** 1485276035.000000 Accepting incoming connection

*** 1485276035.000000 Read from incoming connection

P.S. I have debug activeted in pytg and telegram-cli.
P.P.S. If after All Sent message wait for 2 minutes socket timeout occur:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 506, in _do_send
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "R:\py-cli\pytg_home\pytg\examples\command_who_am_i.py", line 14, in <module>
    res = s.raw('{"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}')
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 632, in command_alias
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 318, in execute_function
  File "C:\Python36\lib\site-packages\pytg-0.4.10-py3.6.egg\pytg\sender.py", line 529, in _do_send
pytg.exceptions.NoResponse: {"ID": "SendMessage", "chat_id_": -1001064119083, "reply_to_message_id_": 0, "disable_notification_": 0, "from_background_": 0, "input_message_content_": {"ID": "InputMessageText", "text_": "tst", "disable_web_preview_": 0, "clear_draft_": 0, "entities_": []}}
INFO:pytg.sender:Quit Sending. Not allowing sending anymore.
WARNING:pytg.sender:Terminating currently sending request.
INFO:pytg.sender:Currently not Sending.

@StrangePers
Copy link

StrangePers commented Jan 25, 2017

Ok. Now I solved the problem by using Unix sockets and removing [disable_preview] from sending string. So I can send commands using raw command. But now I run with another problem - how to authorize Receiver? If telegram-cli set to --json it refuses to receive such registration messages from pytg as "main_session". It obviously is waiting for some json, but what content should be there?

 *** 1485365298.000000 Accepting incoming connection

 *** 1485365298.000000 Closing incoming connection

 *** 1485365298.000000 Accepting incoming connection

 *** 1485365298.000000 Read from incoming connection

telegram-cli: /root/tdcli/interface.c:719: in_command_decref: Assertion `cmd->refcnt > 0' failed.
SIGNAL received
 h/bin/telegram-cli(print_backtrace+0x1c)[0x80e72c]
/bin/telegram-cli(termination_signal_handler+0x56)[0x80e7a6]

If i'm not using json, then main_session works and telegram_cli answers with : ANSWER 8:SUCCESS\n but that's all. No more triggering of recv(1) occure. So it's not possible to get any received message or event from telegram-cli. Any ideas?

@strayge
Copy link

strayge commented Feb 9, 2017

if start telegram-cli from terminal with --json, we can see more specific error until stacktrace:

~/tg$ /home/user/tg/telegram-cli-1222 --json -v 8
...
[Ready] 0> help
FAIL: 38: invalid json: '[' or '{' expected near 'help'
telegram-cli-1222: /root/tdcli/interface.c:719: in_command_decref: Assertion 'cmd->refcnt > 0' failed.
SIGNAL received

so, 'cmd->refcnt > 0' is a error in json parsing.

with valid json on input we got another stacktrace:

[Ready] 0> ["help"]
SIGNAL received
h/home/user/tg/telegram-cli-1222(print_backtrace+0x1c)[0x87eb7c]
/home/user/tg/telegram-cli-1222(termination_signal_handler+0x56)[0x87ebf6]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0)[0x7fab7637f4b0]
/lib/x86_64-linux-gnu/libc.so.6(strlen+0x26)[0x7fab763d4b96]
/lib/x86_64-linux-gnu/libc.so.6(__strdup+0xe)[0x7fab763d48ce]
/home/user/tg/telegram-cli-1222(TdStackFetcherFunction+0x18)[0x8fdd68]
/home/user/tg/telegram-cli-1222(json_interpreter_ex+0xb0)[0x893810]
/home/user/tg/telegram-cli-1222(interpreter+0xd0)[0x889de0]
/lib/x86_64-linux-gnu/libreadline.so.6(rl_callback_read_char+0x145)[0x7fab785576f5]
/home/user/tg/telegram-cli-1222[0x89138a]
/usr/lib/x86_64-linux-gnu/libevent-2.0.so.5(event_base_loop+0x7fc)[0x7fab780e8a0c]
/home/user/tg/telegram-cli-1222(net_loop+0x61)[0x891f71]
/home/user/tg/telegram-cli-1222(loop+0xde)[0x89224e]
/home/user/tg/telegram-cli-1222(main+0x29b)[0x87cceb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fab7636a830]
/home/user/tg/telegram-cli-1222[0x87d1ac]

telegram-cli awaits some json structure as input with --json option

One of the right json we can see in example:

{"ID":"SendMessage", "chat_id_":1007779878, "reply_to_message_id_":0, "disable_notification_":0, "from_background_":0, "input_message_content_":{"ID":"InputMessageText", "text_":"Test text here", "disable_web_preview_":0, "clear_draft_":0, "entities_":[]}}

But full specs not provided, and this part only in closed-source piece of telegram-cli.

I found available values for first level "ID":

Click to expand

AddChatMember
AddChatMembers
AddRecentSticker
AddRecentlyFoundChat
AddSavedAnimation
AnswerCallbackQuery
AnswerInlineQuery
BlockUser
CancelDownloadFile
ChangeAbout
ChangeAccountTtl
ChangeChannelAbout
ChangeChannelUsername
ChangeChatDraftMessage
ChangeChatMemberStatus
ChangeChatPhoto
ChangeChatReportSpamState
ChangeChatTitle
ChangeName
ChangePhoneNumber
ChangeUsername
CheckAuthBotToken
CheckAuthCode
CheckAuthPassword
CheckChangePhoneNumberCode
CheckChatInviteLink
ClearRecentStickers
CloseChat
CloseSecretChat
CreateChannelChat
CreateGroupChat
CreateNewChannelChat
CreateNewGroupChat
CreateNewSecretChat
CreatePrivateChat
CreateSecretChat
DeleteAccount
DeleteChannel
DeleteChatHistory
DeleteChatReplyMarkup
DeleteContacts
DeleteMessages
DeleteMessagesFromUser
DeleteProfilePhoto
DeleteRecentSticker
DeleteRecentlyFoundChat
DeleteRecentlyFoundChats
DeleteSavedAnimation
DownloadFile
EditInlineMessageCaption
EditInlineMessageReplyMarkup
EditInlineMessageText
EditMessageCaption
EditMessageReplyMarkup
EditMessageText
ExportChatInviteLink
FinishFileGeneration
ForwardMessages
GetAccountTtl
GetActiveSessions
GetArchivedStickerSets
GetAttachedStickerSets
GetAuthState
GetBlockedUsers
GetCallbackQueryAnswer
GetChannel
GetChannelFull
GetChannelMembers
GetChat
GetChatHistory
GetChatMember
GetChatReportSpamState
GetChats
GetCommonChats
GetCreatedPublicChannels
GetDeviceTokens
GetFile
GetFilePersistent
GetGameHighScores
GetGroup
GetGroupFull
GetInlineGameHighScores
GetInlineQueryResults
GetMe
GetMessage
GetMessages
GetNotificationSettings
GetOption
GetPasswordState
GetPrivacy
GetRecentInlineBots
GetRecentStickers
GetRecoveryEmail
GetSavedAnimations
GetSecretChat
GetStickerEmojis
GetStickerSet
GetStickerSets
GetStickers
GetSupportUser
GetTrendingStickerSets
GetUser
GetUserFull
GetUserProfilePhotos
GetWallpapers
GetWebPagePreview
ImportChatInviteLink
ImportContacts
MigrateGroupChatToChannelChat
OpenChat
OpenMessageContent
PinChannelMessage
RecoverAuthPassword
RecoverPassword
RegisterDevice
ReorderStickerSets
ReportChannelSpam
RequestAuthPasswordRecovery
RequestPasswordRecovery
ResendAuthCode
ResendChangePhoneNumberCode
ResetAllNotificationSettings
ResetAuth
SearchChatMessages
SearchChats
SearchContacts
SearchMessages
SearchPublicChat
SearchPublicChats
SearchStickerSet
SendBotStartMessage
SendChatAction
SendChatScreenshotTakenNotification
SendChatSetTtlMessage
SendInlineQueryResultMessage
SendMessage
SetAlarm
SetAuthPhoneNumber
SetBotUpdatesStatus
SetFileGenerationProgress
SetGameScore
SetInlineGameScore
SetNotificationSettings
SetOption
SetPassword
SetPrivacy
SetProfilePhoto
SetRecoveryEmail
TerminateAllOtherSessions
TerminateSession
TestCallBytes
TestCallEmpty
TestCallString
TestCallVectorInt
TestCallVectorIntObject
TestCallVectorString
TestCallVectorStringObject
TestForceGetDifference
TestSquareInt
TestTestNet
ToggleChannelInvites
ToggleChannelSignMessages
ToggleGroupEditors
UnblockUser
UnpinChannelMessage
UpdateStickerSet
ViewMessages
ViewTrendingStickerSets

 
But this is not enough and each command have different params.
Also telegram-cli falls if some of requierd keys not presents without any helpfull information.

In all, we can't use json mode today without full specs from author of telegram-cli.

UPD: Founded description in TL-scheme
But it still required full rewriting of sender and receiver.

@luckydonald
Copy link
Owner

luckydonald commented Feb 11, 2017

What I was hoping to have is a TL parser, which would crate code automatically.
Mostly like my Bot API pytgbot project does it.
luckydonald/pytgbot/code_generation
Would making updates a breeze.
But there is no such thing in Python yet, so it needs to be written first.
And frankly, I don't completely understood how TL schema yet.
Did someone understood it?

@strayge
Copy link

strayge commented Feb 12, 2017

I found one alternative for telegram-cli: Telethon
It has tl-parser and simple client as example.

@alirezaafzalaghaei
Copy link

I found one alternative for telegram-cli: Telethon
It has tl-parser and simple client as example.

It's very good but it hasn't on_msg_receive event for super groups!

@elisaado
Copy link

elisaado commented Apr 1, 2017 via email

@mv-go
Copy link

mv-go commented May 9, 2017

Anyone tried using telethon? Or what would you call current "industry standard" for python telegram implementations?
Telethon looks promising in a sense that it's being constantly updated.

@elisaado
Copy link

I tried it out, looked pretty nice, but I couldn't get it to send a message on my own (without the interactive client made by them)

@isblaze
Copy link

isblaze commented Oct 31, 2017

any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants