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";conststringSession=''; // leave this empty for nowconstBOT_TOKEN= ; // put your bot token here(async () => {constclient=newTelegramClient(newStringSession(stringSession), apiId, apiHash, {connectionRetries:5});awaitclient.start({ botAuthToken:BOT_TOKEN });console.log(client.session.save())})();
const {TelegramClient} =require("telegram");const {StringSession} =require ("telegram/sessions");conststringSession=''; // leave this empty for nowconstBOT_TOKEN= ; // put your bot token here(async () => {constclient=newTelegramClient(newStringSession(stringSession), apiId, apiHash, {connectionRetries:5});awaitclient.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')constinput=require('input') // npm i inputconstapiId=123456constapiHash='123456abcdfg'conststringSession=newStringSession(''); // fill this later with the value from session.save()(async () => {console.log('Loading interactive example...')constclient=newTelegramClient(stringSession, apiId, apiHash, { connectionRetries:5 })awaitclient.start({phoneNumber:async () =>awaitinput.text('number ?'),password:async () =>awaitinput.text('password?'),phoneCode:async () =>awaitinput.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 againawaitclient.sendMessage('me', { message:'Hello!' });})()
import { TelegramClient } from'telegram'import { StringSession } from'telegram/sessions'import input from'input'; // npm i inputconstapiId=123456constapiHash='123456abcdfg'conststringSession=newStringSession(''); // fill this later with the value from session.save()(async () => {console.log('Loading interactive example...');constclient=newTelegramClient(stringSession, apiId, apiHash, { connectionRetries:5 });awaitclient.start({phoneNumber:async () =>awaitinput.text('number ?'),password:async () =>awaitinput.text('password?'),phoneCode:async () =>awaitinput.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 againawaitclient.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