luckydonald / pytg

Python package that wraps around Telegram messenger CLI. Send and receive messages, and more.
MIT License
369 stars 76 forks source link

Result parser for secret chats raises exception due to expected result in JSON #138

Open DopeforHope opened 4 years ago

DopeforHope commented 4 years ago

The problem

Result parser for secret chats raises exception due to expected result in JSON Therefore secret chats cant be used. When doing by hand the success msg comes one later. I added a print of the JSON for debugging purposes.

Your environment

Fill out your used versions:

Component Version
python Python 2.7.15+
pytg   0.4.10  
OS Linux pop-os 5.3.0-22-generic
telegram-cli 1.4.1

Things to check before submitting a report

Have you

Steps to reproduce

Use sender.create_secret_chat with the username as argument.

Expected behaviour

Don't raise an execption.

Actual behaviour

Programm:

from pytg.sender import Sender
from pytg.receiver import Receiver
from pytg.utils import coroutine
from pytg.exceptions import ConnectionError
import threading
import sys
import logging

logging.basicConfig(level=logging.DEBUG)

# Connect to telegram
print('Connecting to telegram...')

receiver = Receiver(host="localhost", port=4458)
sender = Sender(host="localhost", port=4458)

# Retrieve contact list

try:
    contacts = [c for c in sender.dialog_list()]
    for i, user in enumerate(contacts):
        print(unicode(i) + ': \t' + unicode(user['print_name']))
except ConnectionError:
    print('Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.')
    sys.exit(1)

# Ask user to choose contact
i = int(input('Telegram online, please enter contact to connect to (by number): '))

# Print username
try:
    username = contacts[i]['print_name']
    peer_id = contacts[i]['peer_id']
    print('Connecting to partner: ' + username)
except IndexError:
    print('Please enter a number in the above range!')
    sys.exit(1)

# Helper function that can be executed in a thread
def main_loop_starter():
    receiver.start()
    # Start the receive loop
    receiver.message(main_loop())

@coroutine
def main_loop():
    while True:
        # Receive message from telegram, this includes ALL messages
        msg = (yield)
        print(msg)

# Create the receive thread via our helper method
thread = threading.Thread(target=main_loop_starter)

print('Trying to establish secret channel...')
sender.create_secret_chat(unicode(contacts[i]['print_name']))
print('Waiting for ')

# Start the thread for receiving
print('Connecting to peer...')
thread.start()
print('Connected! Sending Invitation!')

while True:
    pass
Connecting to telegram...
DEBUG:pytg.sender:Skipping missing optional parameter #0 [limit] (type NonNegativeNumber) in function dialog_list.
DEBUG:pytg.sender:Skipping missing optional parameter #0 [offset] (type NonNegativeNumber) in function dialog_list.
DEBUG:pytg.sender:Sending command >[disable_preview] dialog_list 
<
DEBUG:pytg.sender:Socket Connected.
DEBUG:pytg.sender:All Sent.
0:  Telegram
1:  Evelyn_Hanker
Telegram online, please enter contact to connect to (by number): 1
Connecting to partner: Evelyn_Hanker
Trying to establish secret channel...
DEBUG:pytg.sender:Parsing create_secret_chat: Argument Evelyn_Hanker - <user> (needed)
DEBUG:pytg.sender:Sending command >[disable_preview] create_secret_chat Evelyn_Hanker
<
DEBUG:pytg.sender:Socket Connected.
DEBUG:pytg.sender:All Sent.
DEBUG:pytg.result_parser:{u'print_name': u'!_Evelyn_Hanker#2', u'flags': 1, u'user': {u'first_name': u'Evelyn', u'print_name': u'Evelyn_Hanker', u'peer_type': u'user', u'when': u'2020-01-03 17:53:21', u'phone': u'4915129595268', u'last_name': u'Hanker', u'flags': 196609, u'peer_id': 776203683, u'id': u'$01000000a3ed432e0000000000000000'}, u'peer_type': u'encr_chat', u'peer_id': 752392754, u'id': u'$04000000329ad82cff226a5138015bb9'}
Traceback (most recent call last):
  File "secret_chat_test_sender.py", line 75, in <module>
    sender.create_secret_chat(unicode(contacts[i]['print_name']))
  File "/home/dopeforhope/virtualEnv/teletun/local/lib/python2.7/site-packages/pytg/sender.py", line 630, in command_alias
    return self.execute_function(command_name, *args, **kwargs)
  File "/home/dopeforhope/virtualEnv/teletun/local/lib/python2.7/site-packages/pytg/sender.py", line 351, in execute_function
    return result_parser(message)
  File "/home/dopeforhope/virtualEnv/teletun/local/lib/python2.7/site-packages/pytg/result_parser.py", line 53, in success_fail
    if json.result == u("SUCCESS"):
  File "/home/dopeforhope/virtualEnv/teletun/local/lib/python2.7/site-packages/DictObject/__init__.py", line 717, in __getattr__
    raise _exception
AttributeError: result
INFO:pytg.sender:Quit Sending. Not allowing sending anymore.
WARNING:pytg.sender:Terminating currently sending request.
INFO:pytg.sender:Currently not Sending.
DopeforHope commented 4 years ago

So investigated again the bug. The point is the following: I have the telegram-cli open for both and currently executing the commands manually. Clients wo want to establish a secret chat:

> create_secret_chat Yomam_Whkart
{"event": "updates", "peer": {"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}, "updates": []}
{"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}
{"event": "updates", "peer": {"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}, "updates": ["working"]}
{"event": "service", "id": "04000000cdd05343cfbda28e150a412ff5d10609edb50096", "flags": 10499, "from": {"id": "$01000000a3ed432e97cab7e200c94ff4", "peer_type": "user", "peer_id": 776203683, "print_name": "Evelyn", "flags": 524289, "first_name": "Evelyn", "when": "2020-01-03 18:12:17", "last_name": "", "phone": "4915129595268"}, "to": {"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}, "out": true, "unread": true, "service": true, "date": 1578157822, "action": {"type": "notify_layer", "layer": 17}}
 *** 1578157822.561615 qts = 48081268, qts_count = 1
{"event": "service", "id": "04000000cdd05343b6dccdce7aa42714f5d10609edb50096", "flags": 10496, "from": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}, "to": {"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}, "out": false, "unread": false, "service": true, "date": 1578157822, "action": {"type": "notify_layer", "layer": 17}}
{"user": {"id": "$010000009e5c453134338a86754f1792", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}, "online": false, "state": -1, "when": "2020-01-04 18:14:32", "event": "online-status"}
> msg !_Yomam_Whkart "Secret chat works"
{"event": "message", "id": "04000000cdd05343d6d7b6990d756744f5d10609edb50096", "flags": 18691, "from": {"id": "$01000000a3ed432e97cab7e200c94ff4", "peer_type": "user", "peer_id": 776203683, "print_name": "Evelyn", "flags": 524289, "first_name": "Evelyn", "when": "2020-01-03 18:12:17", "last_name": "", "phone": "4915129595268"}, "to": {"id": "$04000000cdd05343f5d10609edb50096", "peer_type": "encr_chat", "peer_id": 1129566413, "print_name": "!_Yomam_Whkart", "flags": 1, "user": {"id": "$010000009e5c45310000000000000000", "peer_type": "user", "peer_id": 826629278, "print_name": "Yomam_Whkart", "flags": 196609, "first_name": "Yomam", "when": "2020-01-04 18:14:32", "last_name": "Whkart", "phone": "4915129595269"}}, "out": true, "unread": true, "service": false, "date": 1578158234, "text": "Secret chat works"}
{"result": "SUCCESS"}

And the other side who accepts the chat automatically:

{"result": "SUCCESS"}
 *** 1578157822.563992 qts = 210302461, qts_count = 1
 *** 1578158234.395456 qts = 210302462, qts_count = 1

So only the other side receives the success message. Nevertheless it seems that the secret doesn't even work reliably with telegram-cli.