pytg icon indicating copy to clipboard operation
pytg copied to clipboard

msg: parameter #1 <text> is not type UnicodeString.

Open timhok opened this issue 8 years ago • 14 comments

Just installed this package, get my telegram-cli running by

/bin/telegram-cli --json -W -d -P 4458

And when i run this example: https://github.com/luckydonald/pytg/blob/master/examples/command_send_message.py I got this: (i changed only my username)

Traceback (most recent call last):
  File "command_send_message.py", line 19, in <module>
    main()
  File "command_send_message.py", line 14, in main
    res = sender.msg("@timhok", "Hello!")
  File "/usr/local/lib/python2.7/dist-packages/pytg/sender.py", line 630, in command_alias
    return self.execute_function(command_name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pytg/sender.py", line 274, in execute_function
    command_name, new_args = self._validate_input(function_name, arguments)
  File "/usr/local/lib/python2.7/dist-packages/pytg/sender.py", line 414, in _validate_input
    function_name=function_name, number=i, type=func_type.__class__.__name__, param=str(func_type), error=str(err)))
ValueError: Error in function msg: parameter #1 <text> is not type UnicodeString. (Not a string.)

I also tried unicode strings with no success:

pytg.exceptions.FailException: Error 38: u'can not parse arg #1' (command u"[disable_preview] msg timhok 'Hello'\n")

timhok avatar Jul 14 '16 22:07 timhok

I realize that maybe pytg sends msg command without "@" symbol in name, and last version of telegram-cli (version 1.4.1, tgl version 2.1.0) does not support that:

timhok@srv04:~# echo msg timhok hi | nc 127.0.0.1 4458
ANSWER 70
{"result": "FAIL", "error_code": 38, "error": "can not parse arg #1"}

^C
timhok@srv04:~# echo msg @timhok hi | nc 127.0.0.1 4458
ANSWER 22
{"result": "SUCCESS"}

^C

timhok avatar Jul 14 '16 22:07 timhok

FYI: pytg usually starts the CLI with this arguments. Does that json output differ?

luckydonald avatar Jul 17 '16 07:07 luckydonald

Hey, I have the exact same problem, tried the arguments, which doesn't help. Anything else?

blablablupp avatar Aug 30 '16 22:08 blablablupp

You have to decode the text before sending as it expects unicode text:

res = sender.msg("@nick", "hi".decode("utf-8"))

will make it

iwkse avatar Nov 18 '16 01:11 iwkse

What is your python version? 2.7?

luckydonald avatar Nov 19 '16 23:11 luckydonald

Yes in that example I used the 2.7

iwkse avatar Nov 20 '16 10:11 iwkse

You can probably also use u"hi" insead of "hi".decode(...)

luckydonald avatar Nov 20 '16 11:11 luckydonald

using u"msg' or "msg".decode("utf-8") fixed the error for me, but now I get # TODO remove me NoResponse: [disable_preview] msg @contact 'msg :)'

Update:

ok, I started telegram-cli manually with --json -P 4458 and it fixed my problem.

Alirezaies avatar Jan 11 '17 03:01 Alirezaies

Sorry, you didn't write with version of python on what OS you are running.

luckydonald avatar Jan 13 '17 11:01 luckydonald

If using Python 2, be sure you are using Unicode strings (unicode), not the normal (binary) str. Or just use Python 3.

luckydonald avatar Jan 13 '17 11:01 luckydonald

I'm Using Ubuntu 16.04 LTS And Python 2.7

Alirezaies avatar Jan 14 '17 00:01 Alirezaies

I'm getting the same error with python 3.5.2 on Linux-4.4.0-75-generic-x86_64-with-Ubuntu-16.04-xenial,

Traceback (most recent call last):
  File "cli.py", line 5, in <module>
    res = sender.send_msg('@bitwisepi', u"Testing, does this thing work?")
  File "/usr/local/lib/python3.5/dist-packages/pytg/sender.py", line 630, in command_alias
    return self.execute_function(command_name, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pytg/sender.py", line 354, in execute_function
    raise e
  File "/usr/local/lib/python3.5/dist-packages/pytg/sender.py", line 351, in execute_function
    return result_parser(message)
  File "/usr/local/lib/python3.5/dist-packages/pytg/result_parser.py", line 55, in success_fail
    raise FailException(json.error_code, json.error)
pytg.exceptions.FailException: Error 38: 'can not parse arg #1' (command "[disable_preview] msg @bitwisepi 'Testing, does this thing work?'\n")

Tried passing both arguments as unicode strings, still throws an error.

rescindable avatar May 01 '17 17:05 rescindable

Which cli version (The commit hash?) are you using?

luckydonald avatar May 02 '17 18:05 luckydonald

Should be 6547c0b21b977b327b3c5e8142963f4bc246187a, the most recent one

I actually fixed this error by adding quotes before and after the peer value in my msg() function, but it still returns the same error as described in #81, with the "can't find user telegramd to switch to" value

rescindable avatar May 02 '17 23:05 rescindable