This is the template I use across my bots. Before using, setup .env and install @mtproto/core
, dotenv
and readline-sync
. Then execute with node index.js
. You can also move utils.js and auth.js to src folder.
-
-
Save VityaSchel/77e482daf2ac688c18d39b126583fc86 to your computer and use it in GitHub Desktop.
Telegram MTProto program template (with login, 2FA support and @mtproto/core module)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_ID= | |
APP_HASH= | |
PHONE= | |
TWO_FA_PASSWORD= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readlineSync from 'readline-sync' | |
import { getUser, sendCode, signIn, getPassword, checkPassword } from './utils.js' | |
export default async function authorize() { | |
let user | |
if(process.argv[2] === '--logout') await global.api.call('auth.logOut') | |
else user = await getUser() | |
if (user) return user | |
else { | |
const phone = process.env.PHONE | |
const { phone_code_hash } = await sendCode(phone) | |
const code = readlineSync.question('Код для авторизации: ') | |
try { | |
const signInResult = await signIn({ | |
code, | |
phone, | |
phone_code_hash, | |
}) | |
if (signInResult._ === 'auth.authorizationSignUpRequired') throw 'Аккаунт не найден' | |
} catch (error) { | |
switch (error.error_message) { | |
case 'SESSION_PASSWORD_NEEDED': | |
try { | |
await twoFA() | |
} catch(e) { | |
if(e.error_message === 'PASSWORD_HASH_INVALID') throw 'Неправильный пароль 2FA в .env файле' | |
else throw error | |
} | |
return await getUser() | |
case 'PHONE_CODE_INVALID': | |
console.log('Неправильный код! Попробуйте еще раз') | |
return await authorize() | |
default: | |
throw error | |
} | |
} | |
} | |
} | |
async function twoFA() { | |
const { srp_id, current_algo, srp_B } = await getPassword() | |
const { g, p, salt1, salt2 } = current_algo | |
const { A, M1 } = await global.api.crypto.getSRPParams({ | |
g, | |
p, | |
salt1, | |
salt2, | |
gB: srp_B, | |
password: process.env.TWO_FA_PASSWORD, | |
}) | |
await checkPassword({ srp_id, A, M1 }) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dotenv/config' | |
import MTProto from '@mtproto/core' | |
import authorize from './auth.js' | |
global.api = new MTProto({ | |
api_id: Number(process.env.APP_ID), | |
api_hash: process.env.APP_HASH, | |
storageOptions: { path: './tempdata.json' } | |
}) | |
const user = await authorize() | |
console.log(`Пользователь ${user.users[0].first_name} авторизирован, бот начинает работу`) | |
// Use https://mtproto-core.js.org/docs/ to write methods here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getUser() { | |
try { | |
const user = await global.api.call('users.getFullUser', { | |
id: { | |
_: 'inputUserSelf', | |
}, | |
}) | |
return user | |
} catch (error) { | |
return null | |
} | |
} | |
export function sendCode(phone) { | |
return global.api.call('auth.sendCode', { | |
phone_number: phone, | |
settings: { | |
_: 'codeSettings', | |
}, | |
}) | |
} | |
export function signIn({ code, phone, phone_code_hash }) { | |
return global.api.call('auth.signIn', { | |
phone_code: code, | |
phone_number: phone, | |
phone_code_hash: phone_code_hash, | |
}) | |
} | |
export function signUp({ phone, phone_code_hash }) { | |
return global.api.call('auth.signUp', { | |
phone_number: phone, | |
phone_code_hash: phone_code_hash, | |
first_name: 'MTProto', | |
last_name: 'Core', | |
}) | |
} | |
export function getPassword() { | |
return global.api.call('account.getPassword') | |
} | |
export function checkPassword({ srp_id, A, M1 }) { | |
return global.api.call('auth.checkPassword', { | |
password: { | |
_: 'inputCheckPasswordSRP', | |
srp_id, | |
A, | |
M1, | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment