Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created Aug 20, 2022
Embed
What would you like to do?
create telegram notification by channels and supergroups
const { Api,TelegramClient } = require("telegram");
const { StringSession, StoreSession} = require("telegram/sessions");
const input = require("input");
const { NewMessage } = require('telegram/events')
const util = require('util')
const apiId = 0;
const apiHash = "0";
const stringSession = new StoreSession("telegram_session");
(async () => {
console.log("Loading interactive example...");
const client = new TelegramClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
});
await client.start({
phoneNumber: async () => "+9",
password: async () => await input.text("Please enter your password: "),
phoneCode: async () =>
await input.text("Please enter the code you received: "),
onError: (err) => console.log(err),
});
console.log("You should now be connected.");
client.session.save()
async function eventPrint(event) {
const message = event.message;
const chat = await message.getChat()
const user = await message.getSender()
console.log(`${chat.title}: (${user.firstName} ${user.lastName}) ${message.text}`)
}
const dialogs = await client.getDialogs();
dialogList = []
dialogs.forEach(
dialog=>{
if(dialog.isChannel || dialog.isGroup || dialog.isUser){
dialogList.push(dialog.id)
}
}
)
client.addEventHandler(eventPrint, new NewMessage({chats: dialogList}));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment