npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

teledown

v0.0.8

Published

Telegram app to download files from a group

Downloads

9

Readme

Teledown

Teledown is a tool designed to download files from Telegram groups using the Telegram API.

Installation

To install as a CLI:

npm install -g teledown
# or
yarn global add teledown
# or
pnpm install -g teledown

To install as a library:

npm install teledown
# or
yarn add teledown
# or
pnpm install teledown

As a CLI

First you need to generate a config file. You can do this by running:

teledown --gen-config

Then open the file and fill it with your data. A sample config file looks like this:

{
  "API_ID": 0,
  "API_HASH": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "CHANNEL_NAME": "xxxxxxxxx",
  "MAX_MESSAGES_PER_RUN": 80,
  "SECONDS_BETWEEN_RUNS": 60,
  "OUT": "./downloads"
}

After you have filled the config file, you can start downloading files by running:

teledown --config ./config.json

On the first run, you will be asked to authenticate with your phone number and a code sent by Telegram. The session will be saved in a file called session.txt in the same directory as the config file.

If you don't want to create a config file, you can pass the parameters directly. To see all available parameters, run:

teledown --help

As a library

import TeleDown from 'teledown';

const teledown = new TeleDown({
  API_ID: 'your_api_id', // get it on https://my.telegram.org/apps
  API_HASH: 'your_api_hash', // get it on https://my.telegram.org/apps
  CHANNEL_NAME: 'your_channel_name', // this comes from the URL of the channel (https://t.me/your_channel_name)
  MAX_MESSAGES_PER_RUN: 100, // how many messages to download per run
  SECONDS_BETWEEN_RUNS: 60, // how many seconds to wait between runs
  OUT: './downloads' // where to save the files (relative to the current directory)
});

// if no session is found, it will ask for the phone number and then the code sent by Telegram
await teledown.authenticate();
teledown.start();

// later you can stop the process
// if teledown is already processing a batch, it will finish it before stopping
teleDown.stop();

Events

import TeleDown from 'teledown';

const teledown = new TeleDown({
  ... // config
});

// all the available events
teledown.on('error', (err) => console.log(err));
teledown.on('got-messages', (messages, offsetId) =>
  console.log(`Found ${messages.length} messages with offset ${offsetId}`)
);
teledown.on('downloaded-file', ({ path, fileName, originalFileName, extension, md5, telegramFile }) =>
  console.log(`Downloaded ${originalFileName} (${md5})`)
);
teledown.on('start-batch', (offsetId) =>
  console.log(`Starting batch with offset ${offsetId}`)
);
teledown.on('end-batch', (offsetId) =>
  console.log(`Ending batch with offset ${offsetId}`)
);
teledown.on('end', (offsetId) =>
  console.log(`No more messages to process. Ending with offset ${offsetId}`)
);

// you can also remove an event listener
const listener = teledown.on('downloaded-file', ({ originalFileName, md5 }) =>
  console.log(`Downloaded ${originalFileName} (${md5})`)
);

teledown.off(listener);

// or remove all listeners
teledown.offAll();

Passing a filter function

You can pass a filter function to the constructor to filter files that will be downloaded. The function recieves a File object and should return a boolean.

The API is as follows:

const fileFilter = (telegramFile) => {
  // telegramFile: the full file object from Telegram, read the Telegram API docs for more info

  return true; // will download the file
};

Example:

const fileFilter = () => {
  return true; // will download all files
};

const teledown = new TeleDown(someConfig, fileFilter);

// rest of the code

Only download files with a specific extension:

const fileFilter = (telegramFile) => {
  const fileName = telegramFile?.attributes?.[0]?.fileName;
  const extension = fileName?.split('.')?.pop();

  return extension === 'pdf'; // will only download .pdf files
};

const teledown = new TeleDown(someConfig, fileFilter);

// rest of the code

How it works

Teledown uses the Telegram API to fetch messages from a specified channel or group. It processes the messages in batches, downloading files that match the specified criteria. The tool saves the offset of the last processed message, allowing it to resume from where it left off in subsequent runs.

Limits

The Telegram API has rate limits, so Teledown includes a configurable delay SECONDS_BETWEEN_RUNS to avoid hitting these limits. The default delay is 60 seconds after processing each batch of 100 messages. The session TTL is also something to be mindful of, as re-authentication may be required after a certain period.

Fair use

Use this tool responsibly and respect the terms of service of the Telegram API. Do not abuse the API or use this tool for malicious purposes.