A Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.
Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!
Logging in as a Bot
Using GramJS you can use a bot token to log in. doing this is simple
import {TelegramClient} from "telegram";
import {StringSession} from "telegram/sessions";
const stringSession = ''; // leave this empty for now
const BOT_TOKEN = ; // put your bot token here
(async () => {
const client = new TelegramClient(new StringSession(stringSession),
apiId, apiHash, {connectionRetries: 5});
await client.start({
botAuthToken: BOT_TOKEN
});
console.log(client.session.save())
})();
const {TelegramClient} = require("telegram");
const {StringSession} =require ("telegram/sessions");
const stringSession = ''; // leave this empty for now
const BOT_TOKEN = ; // put your bot token here
(async () => {
const client = new TelegramClient(new StringSession(stringSession),
apiId, apiHash, {connectionRetries: 5});
await client.start({
botAuthToken: BOT_TOKEN
});
console.log(client.session.save())
})();
Underneath this is just calling the RAW function ImportBotAuthorization. you can leave the string session empty for now.
Logging in as a User
Logging in as a user is a bit more complex because you'll need to provide callbacks for when you receive the code from telegram. you can use the input package to manage that on Node or prompt on the browser
const { TelegramClient } = require('telegram')
const { StringSession } = require('telegram/sessions')
const input = require('input') // npm i input
const apiId = 123456
const apiHash = '123456abcdfg'
const stringSession = new StringSession(''); // fill this later with the value from session.save()
(async () => {
console.log('Loading interactive example...')
const client = new TelegramClient(stringSession, apiId, apiHash, { connectionRetries: 5 })
await client.start({
phoneNumber: async () => await input.text('number ?'),
password: async () => await input.text('password?'),
phoneCode: async () => await input.text('Code ?'),
onError: (err) => console.log(err),
});
console.log('You should now be connected.')
console.log(client.session.save()) // Save this string to avoid logging in again
await client.sendMessage('me', { message: 'Hello!' });
})()
import { TelegramClient } from 'telegram'
import { StringSession } from 'telegram/sessions'
import input from 'input'; // npm i input
const apiId = 123456
const apiHash = '123456abcdfg'
const stringSession = new StringSession(''); // fill this later with the value from session.save()
(async () => {
console.log('Loading interactive example...');
const client = new TelegramClient(stringSession, apiId, apiHash, { connectionRetries: 5 });
await client.start({
phoneNumber: async () => await input.text('number ?'),
password: async () => await input.text('password?'),
phoneCode: async () => await input.text('Code ?'),
onError: (err) => console.log(err),
});
console.log('You should now be connected.');
console.log(client.session.save()) // Save this string to avoid logging in again
await client.sendMessage('me', { message: 'Hello!' });
})()
Persistent Session
To avoid having to logging each time you'll need to save the session after logging in.
There are multiple types of sessions with the easiest being StringSession that will provide an Authorization string for you to use again. You can create your own Session by subclassing the MemorySession class.
If you have async logic in your custom session put it in the load() function that's called before loading a session
Store session uses store2 with the help of node-localstorage to save the session automatically in files. it's useful to save entities so you can access them later with just their ID and lowers the amount of requests needed to the telegram server. You just need to provide a session name to save it with
import { StoreSession } from 'telegram/sessions'
const storeSession = new StoreSession("my_session");
const client = new TelegramClient(storeSession , apiId, apiHash,
{connectionRetries: 5});