luckydonald / pytg

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

Retrieving history of a single-message conversation improperly throws an exception #89

Closed joell closed 1 year ago

joell commented 8 years ago

If you attempt to retrieve the history of a conversation which has only a single message, pytg will fail with an IllegalResponseException("Should return something.") in the something method in result_parser.py.

What's happening is that telegram-cli is returning an array with a single JSON object for the single message in the conversation, exactly as it should. However, something(value) requires that len(value) > 1, which is not the case since the length of the JSON array is exactly 1. This causes pytg to improperly fail.

The fix should be as simple as replacing len(value) > 1 with len(value) >= 1 or len(value) > 0 (which is what I would have expected "something" to imply).

luckydonald commented 8 years ago

I think the thought behind that was, that "{}" already needs 2 characters. But indeed, as sender.py:341 shows, it will most certainly (if json parsing doesn't fail) be a dict.

And Something > 0 totally makes sense.

luckydonald commented 8 years ago

If you like you can submit a PR, I am not sure when I get around to edit that in.

joell commented 8 years ago

Hmm... so when I enter the program with a debugger, something's value isn't a str or dict... it's a list. And that made sense to me, since telegram-cli returns a JSON array for its response to the history command.

My eventual local solution to this was to change the history check from res.something to res.anything, as even [] is a valid response from history for an empty conversation (or one with an offset that precludes any messages that did exist).

However, if the expectation is that the result is either always a string or always a dictionary, then something much more serious might be going on.

luckydonald commented 8 years ago

Most of the command return values are not properly implemented, as I never needed them... heh. As I currently have no running cli, could you check if it is always a list? If yes, you could edit in a check for that, like mentioned in comment of #90? I'd much appreciate it.