[Dev] Allow invoking Telegram API from console (#1346)

This commit is contained in:
Alexander Zinchuk 2021-08-03 01:31:05 +03:00
parent 435362e448
commit e69ea676f3
8 changed files with 43 additions and 10 deletions

View File

@ -16,4 +16,18 @@ Obtain API ID and API hash on [my.telegram.org](https://my.telegram.org) and pop
npm run dev
```
### Invoking API from console
Start your dev server and locate GramJS worker in console context.
All constructors and functions available in global `GramJs` variable.
Run `npm run gramjs:tl full` to get access to all available Telegram requests.
Example usage:
``` javascript
await invoke(new GramJs.help.GetAppConfig())
```
## Bug reports and Suggestions
If you find an issue with this app, let Telegram know using the [Suggestions Platform](https://bugs.telegram.org/c/4002).

View File

@ -95,6 +95,11 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
console.log('>>> FINISH INIT API');
// eslint-disable-next-line no-console
console.log('[GramJs/client] CONNECTED');
// eslint-disable-next-line no-restricted-globals
(self as any).invoke = invokeRequest;
// eslint-disable-next-line no-restricted-globals
(self as any).GramJs = GramJs;
}
onAuthReady();

View File

@ -2,3 +2,5 @@ src/lib/gramjs/tl/types-generator/template.js
src/lib/gramjs/tl/api.d.ts
src/lib/gramjs/tl/apiTl.js
src/lib/gramjs/tl/schemaTl.js
src/lib/gramjs/tl/apiTl.full.js
src/lib/gramjs/tl/schemaTl.full.js

View File

@ -914,7 +914,6 @@ account.resetPasswordFailedWait#e3779861 retry_date:int = account.ResetPasswordR
account.resetPasswordRequestedWait#e9effc7d until_date:int = account.ResetPasswordResult;
account.resetPasswordOk#e926d63e = account.ResetPasswordResult;
---functions---
// Only needed functions from \`api.tl\` are manually copied here to reduce package size
initConnection#c1cd5ea9 {X:Type} flags:# api_id:int device_model:string system_version:string app_version:string system_lang_code:string lang_pack:string lang_code:string proxy:flags.0?InputClientProxy params:flags.1?JSONValue query:!X = X;
invokeWithLayer#da9b0d0d {X:Type} layer:int query:!X = X;
auth.sendCode#a677244f phone_number:string api_id:int api_hash:string settings:CodeSettings = auth.SentCode;
@ -1086,5 +1085,4 @@ langpack.getLangPack#f2f2330a lang_pack:string lang_code:string = LangPackDiffer
langpack.getStrings#efea3803 lang_pack:string lang_code:string keys:Vector<string> = Vector<LangPackString>;
langpack.getLanguages#42c6978f lang_pack:string = Vector<LangPackLanguage>;
folders.editPeerFolders#6847d0ab folder_peers:Vector<InputFolderPeer> = Updates;
// LAYER 128
`;

View File

@ -4,17 +4,32 @@ const path = require('path');
require('./types-generator/generate');
function main() {
const apiTl = fs.readFileSync(path.resolve(__dirname, './static/api.reduced.tl'), 'utf-8');
const args = process.argv.slice(2);
const FULL_SCHEMA = args.length && args[0] === 'full';
const apiTl = fs.readFileSync(
path.resolve(__dirname, `./static/api${!FULL_SCHEMA ? '.reduced' : ''}.tl`),
'utf-8',
);
fs.writeFileSync(
path.resolve(__dirname, './apiTl.js'),
`module.exports = \`${apiTl.replace(/`/g, '\\`')}\`;`,
`module.exports = \`${stripTl(apiTl)}\`;`,
);
const schemaTl = fs.readFileSync(path.resolve(__dirname, './static/schema.reduced.tl'), 'utf-8');
const schemaTl = fs.readFileSync(
path.resolve(__dirname, `./static/schema${!FULL_SCHEMA ? '.reduced' : ''}.tl`),
'utf-8',
);
fs.writeFileSync(
path.resolve(__dirname, './schemaTl.js'),
`module.exports = \`${schemaTl.replace(/`/g, '\\`')}\`;`,
`module.exports = \`${stripTl(schemaTl)}\`;`,
);
}
function stripTl(tl) {
return tl.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '')
.replace(/\n\s*\n/g, '\n')
.replace(/`/g, '\\`');
}
main();

View File

@ -36,5 +36,4 @@ destroy_session_none#62d350c9 session_id:long = DestroySessionRes;
new_session_created#9ec20908 first_msg_id:long unique_id:long server_salt:long = NewSession;
---functions---
ping#7abe77ec ping_id:long = Pong;
ping_delay_disconnect#f3427b8c ping_id:long disconnect_delay:int = Pong;
`;
ping_delay_disconnect#f3427b8c ping_id:long disconnect_delay:int = Pong;`;