pytg
pytg copied to clipboard
Initialize Telegram Account (enter phone number & code)
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:
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
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.
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.
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:
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:
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.
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.
#52 could benefit from this.
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:
but how to wait for the SMS with the code?
maybe allow them seperatly?
--phone=+001234 --register
and --phone=+001234 --code=4458
?
Subscribed. This will be awesome.
@drr3d any progress? Do you have your code modifications here on github?
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).
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.
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...
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).
Also .telegram-cli/auth
might not be present if you are using profiles
(instead you have .telegram-cli/profile123/auth
)
Hum, I have never used this, is it working with the current telegram-cli version?
It seems that the -p
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...
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
@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.
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")`