tgsnake
Version:
Telegram MTProto framework for nodejs.
180 lines (179 loc) • 6.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginWithCLI = LoginWithCLI;
const Logger_js_1 = require("../../Context/Logger.js");
const platform_node_js_1 = require("../../platform.node.js");
const onCancel = () => {
Logger_js_1.Logger.info('Aborting prompt!!');
platform_node_js_1.sysprc.exit(1);
return false;
};
async function LoginWithCLI(snake) {
Logger_js_1.Logger.info('Initial login with CLI.');
if (!snake._options.apiId) {
Logger_js_1.Logger.debug('Api Id is missing.');
await AskApiId(snake);
}
if (!snake._options.apiHash) {
Logger_js_1.Logger.debug('Api Hash is missing.');
await AskApiHash(snake);
}
Logger_js_1.Logger.debug('Creating client.');
snake._client = new platform_node_js_1.Client(snake._options.login.session, snake._options.apiHash, snake._options.apiId, snake._options.clientOptions);
await snake._options.login.session.load();
if (!snake._options.login.session.authKey) {
if (snake._options.login.botToken) {
Logger_js_1.Logger.debug('Login using bot token.');
const user = await snake._client.start({
botToken: snake._options.login.botToken,
});
Logger_js_1.Logger.log(`${snake._options.login.forceDotSession
? `(${snake._options.login.sessionName}.session)`
: ``} Loggined as: `);
await snake._options.login.session.save();
return user;
}
const loginAs = snake._options.login.loginAs || (await AskLoginAs());
if (loginAs === 'undefined')
return;
Logger_js_1.Logger.debug(`Login as: ${loginAs}`);
if (loginAs === 'bot') {
await AskBotToken(snake);
const user = await snake._client.start({
botToken: snake._options.login.botToken,
});
Logger_js_1.Logger.log(`${snake._options.login.forceDotSession
? `(${snake._options.login.sessionName}.session)`
: ``} Loggined as: `);
await snake._options.login.session.save();
return user;
}
const user = await snake._client.start({
phoneNumber: AskPhoneNumber,
password: AskPassword,
recoveryCode: AskRecoveryCode,
code: AskOTPCode,
firstname: AskFirstName,
lastname: AskLastName,
});
Logger_js_1.Logger.log(`${snake._options.login.forceDotSession ? `(${snake._options.login.sessionName}.session)` : ``} Loggined as: `);
await snake._options.login.session.save();
return user;
}
else {
await snake.connect();
return await snake.getMe();
}
}
async function AskApiId(snake) {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'number',
name: 'value',
message: 'Input your api id!',
validate: (value) => (value ? true : false),
}, { onCancel });
snake._options.apiId = value;
Logger_js_1.Logger.debug(`Setting up Api Id with: ${snake._options.apiId}.`);
return String(value);
}
async function AskApiHash(snake) {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'text',
name: 'value',
message: 'Input your api hash!',
validate: (value) => (value ? true : false),
}, { onCancel });
snake._options.apiHash = value;
Logger_js_1.Logger.debug(`Setting up Api Hash with: ${snake._options.apiHash}.`);
return String(value);
}
async function AskLoginAs() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'select',
name: 'value',
message: 'Login as?',
initial: 0,
choices: [
{
title: 'Bot',
description: 'Login as bot using bot token from bot father.',
value: 'bot',
},
{
title: 'User',
description: 'Login as user using phone number.',
value: 'user',
},
],
}, { onCancel });
return String(value);
}
async function AskBotToken(snake) {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'text',
name: 'value',
message: 'Input your bot token!',
validate: (value) => (value ? true : false),
}, { onCancel });
snake._options.login.botToken = value;
const splitedBotToken = String(snake._options.login.botToken).split(':');
const securedBotToken = `${splitedBotToken[0]}:${splitedBotToken[1]
.slice(30)
.padStart(splitedBotToken[1].length, '*')}`;
Logger_js_1.Logger.debug(`Setting up Bot Token with: ${securedBotToken}.`);
return String(value);
}
async function AskPhoneNumber() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'number',
name: 'value',
message: 'Input your international phone number!',
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}
async function AskPassword(hint) {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'password',
name: 'value',
message: `Input your two factor authentication password!\n\nHint : ${hint}`,
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}
async function AskRecoveryCode() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'number',
name: 'value',
message: 'Input your recovery code from your email address!',
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}
async function AskOTPCode() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'number',
name: 'value',
message: 'Input your otp code from telegram application or sms!',
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}
async function AskFirstName() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'text',
name: 'value',
message: 'Input your first name!',
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}
async function AskLastName() {
const { value } = await (0, platform_node_js_1.prompts)({
type: 'text',
name: 'value',
message: 'Input your last name!',
validate: (value) => (value ? true : false),
}, { onCancel });
return String(value);
}