TeleWeb - Telegram API implementation for JS in the browser
Based on original TeleJS library(thanks RD17) with few changes:
- Switched to websockets
- Switched to TG API layer 112
- Implemented methods for SRP two-factor authentication
- Moved config out of the library
- Parallel connections to same datacenters
- Handling TG API updates
- Few changes in methods/dependencies to optimize functionality for webapps
Installing
npm install teleweb --save
Code Usage
const telegram = require('teleweb');
const config = {
initialState: null,
test: false,
ssl: true,
websockets: true,
persistState: true,
appId: 'xxxxx',
appHash: 'xxxxx',
defaultDC: 2,
keepNotDefaultNetworkers: false,
};
let signedInUser = await telegram.init(config, saveState, restoreState, 'debug');
log('User Signed In: ');
log(JSON.stringify(signedInUser));
log('Nearest DC: ');
log(JSON.stringify(telegram.getNearestDc()));
log('auth.sendCode');
let resp = await telegram.mtpInvokeApi('auth.sendCode', {phone_number: '+380505555555', settings: {"_":"codeSettings"}});
log(JSON.stringify(resp));
log('different DC');
resp = await telegram.mtpInvokeApi('upload.getFile', { }, {dcId: 5});
log(JSON.stringify(resp));
log('Media DCs');
resp = await telegram.mtpInvokeApi('upload.getFile', { }, {dcId: 1005});
resp = await telegram.mtpInvokeApi('upload.getFile', { }, {dcId: 2005});
resp = await telegram.mtpInvokeApi('upload.getFile', { }, {dcId: 3005});
log('auth.logOut');
resp = await telegram.mtpInvokeApi('auth.logOut');
log(JSON.stringify(resp));
log('MD5');
log(telegram.MD5('test'));
log('SRPGenerator');
resp = await telegram.mtpInvokeApi('account.getPassword');
const srpGenerator = new telegram.SRPGenerator(resp.data);
const srp = await srpGenerator.getInputCheckPasswordSRP(password);
const authResp = await telegram.mtpInvokeApi('auth.checkPassword', { password: srp });
log('Subscribe to API updates');
telegram.setUpdatesCallback((updateObject)=>{
console.log(updateObject);
});
License
MIT