pytg icon indicating copy to clipboard operation
pytg copied to clipboard

Initialize Telegram Account (enter phone number & code)

Open drr3d opened this issue 8 years ago • 22 comments

Hi there again Donald :+1:

First, i'm sorry this is not really an issues, if you mind, i can move this post into another page(please give me a page to ask this question :smile: )

i still enjoying your works, thanks again.. :smile:

i have a some questions, regarding additional new account,

Whenever i try to initiate a new account/number/profile directly using Telegram-CLI, it always give some instruction, that require us to provide some information's, like phone number(usually after this Telegram would send us password to fill in Telegram-CLI), etc.

like the image below: pytg

is there any function that pytg can handle such task?

if no, is there a way i can do such task by modifying your module? without directly using Telegram-CLI?

sorry again for my bad English :smile:

Regards, drr3d

drr3d avatar Mar 07 '16 05:03 drr3d

Pytg is not yet capable of that. However, I would welcome any solutions you get!

The start stuff of the CLI is in pytg/__init__.py:54, and stopping happens in line 93 Especially stopping is a tricky part of code, as it should not leave a CLI running causing the port beeing blocked forever. (It took quite some time to get it working :D)

The reason why I did not implement yet, is mostly the detection of the input, (includes reading it), and the direct "typing the number" to the process. Checking for the code probably needs active polling, which is quite CPU intense.

luckydonald avatar Mar 07 '16 15:03 luckydonald

Long time ago (somewhere before v0.3.0) active polling was exactly what pytg did, to get any messages.

Probably you should make the check optional, and stop polling (or shut down?) after it verified the CLI is working correctly.

luckydonald avatar Mar 07 '16 16:03 luckydonald

Hi Donald

thx for your explanation, ok then i'll try to make it work, and as soon as it run, i'll let you know mate :smile:

thx again :+1:

drr3d avatar Mar 08 '16 06:03 drr3d

hmmm, i think the easiest way is modifying Telegram-CLI it self, i've done it by creating additional argument(still need more tesstingg :sob:), and (maybe then) parse that argument in pytg(this not done yet :smile:),, still more work to do :sob:

drr3d avatar Mar 08 '16 11:03 drr3d

Argument? You mean a parameter? Like telegram-cli --register --phone=+0012345 --code=1234?

It'll usually take a while for @vysheng until he get around to merging patches.

luckydonald avatar Mar 08 '16 15:03 luckydonald

I believe parsing the phone number: part shouldn't be that difficult, and the code (or call) part too. I don't expect that string to change anytime soon.

Actually yesterday I sounded like parsing that is something impossible, but it shouldn't be that hard. Hopefully I didn't discourage you.

luckydonald avatar Mar 08 '16 15:03 luckydonald

#52 could benefit from this.

luckydonald avatar Mar 08 '16 15:03 luckydonald

telegram-cli --register --phone=+0012345 --code=1234 ahhh you right, i forget about the code that must we provide, because the code only came after we enter the phonenumber :sob: :sob:

drr3d avatar Mar 09 '16 01:03 drr3d

but how to wait for the SMS with the code?

luckydonald avatar Mar 09 '16 11:03 luckydonald

maybe allow them seperatly? --phone=+001234 --register and --phone=+001234 --code=4458?

luckydonald avatar Mar 09 '16 11:03 luckydonald

Subscribed. This will be awesome.

natsu90 avatar Mar 29 '16 03:03 natsu90

@drr3d any progress? Do you have your code modifications here on github?

luckydonald avatar Mar 29 '16 18:03 luckydonald

This would be indeed very good. I think using Pexpect is the easiest way to do that (moreover the --phone parameter is already working in telegram-cli).

Nanoseb avatar Mar 31 '16 11:03 Nanoseb

Here is a minimal code that works with pexpect:

import pexpect

phone = input('your phone number: ')
child = pexpect.spawn('telegram-cli')

child.expect('phone number:')
child.sendline(phone)

code = input('the verification code: ')
child.expect('code.*')
child.sendline(code)

child.sendline('quit')

I am going to use that for now in my client.

Nanoseb avatar Mar 31 '16 17:03 Nanoseb

I'll have a look into that. Maybe we can Include that. How do you check whether the cli is registered yet?

I can think of looking in ~/.telegram-cli/..., but that would be very hacky, and might break...

luckydonald avatar Apr 01 '16 04:04 luckydonald

The only thing I can think of right now is looking if .telegram-cli/auth is empty or not, it is not perfect but it is the best we can do without modifying telegram-cli. The other one would be to see if telegram-cli is asking for a phone number, but it would be even worse (every run you have to wait to see if 'phone number:' is prompted or not).

Nanoseb avatar Apr 01 '16 10:04 Nanoseb

Also .telegram-cli/auth might not be present if you are using profiles (instead you have .telegram-cli/profile123/auth)

luckydonald avatar Apr 01 '16 17:04 luckydonald

Hum, I have never used this, is it working with the current telegram-cli version? It seems that the -p parameter is just ignored.

Nanoseb avatar Apr 01 '16 17:04 Nanoseb

Ok, I can't make profiles work. Anyway it is not a big deal, if you start telegram-cli you have to know the profile name also, so the only thing needed is to put the profile name in the path for this verification.

But I agree with you, it is not very clean...

Nanoseb avatar Apr 01 '16 17:04 Nanoseb

You need to write the config file first. Mine contains:

default_profile = "bot2";
bot = {
        config_directory = ".telegram-cli/bot";
        msg_num = true;
        test = true;
};
bot2 = {
        config_directory = ".telegram-cli/bot2";
        msg_num = true;
        test = false    ;
};
luckydonald = {
        config_directory = ".telegram-cli/luckydonald";
        msg_num = true;
        test = false    ;
};

I am not sure if config_directory is mandatory, also note the default_profile setting

luckydonald avatar Apr 02 '16 12:04 luckydonald

@Gintasz The traceback says, that phone is an integer. If you used exactly the code above, this would mean input("...") returns an integer.

This indicates you use python 2 instead of python 3.

You should use python 3 instead to run the code, as python 2 is a bad choice.

If you really need to stick to python 2, use raw_input(), but using python 2 will most certainly give you a hard time when dealing with unicode strings later.

luckydonald avatar Feb 01 '18 14:02 luckydonald

wrote some code... `telecli = [path_to_cli, '-c',path_to_config,'-p',phone_number,'-q'] t = time.time()

p = subprocess.Popen(telecli, bufsize=0, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=False)

numberRequest = False
while not numberRequest and time.time() - t < 5:
	if(p.stdout.readline().find("phone")):
	numberRequest = True
if numberRequest:
	p.stdin.write(number_of_profile)
	print("Number sent")
else:
	raise Exception('Time out, try run telegram-cli in bash to check error')
t = time.time()

codeRequest = False
while not codeRequest and time.time() - t < 5:
	if(p.stdout.readline().find("code")):
	codeRequest = True
if codeRequest:
	code = input("Enter code:")
	p.stdin.write(int(code))
	print("Code sent")
else:
	raise Exception('Time out, try run telegram-cli in bash to check error')

passRequest = False
while not passRequest and time.time() - t < 5:
	if(p.stdout.readline().find("pass")):
	passRequest = True
if passRequest:
	passwd = input("Enter password of two-step verification:")
	p.stdin.write(int(passwd))
	print("Password sent")`

bioxakep avatar Jul 22 '18 08:07 bioxakep