Tool

Load Telegram Messages into Documents

LlamaIndex reader that fetches Telegram channel or chat content into documents.

Works with telegram

74
Spark score
out of 100
Updated 2 days ago
Version 0.14.23
Models

Add to Favorites

Why it matters

Ingest and structure data from Telegram channels and chats for analysis and integration with AI models. This loader facilitates the extraction of messages, posts, and comments, making them readily available for further processing.

Outcomes

What it gets done

01

Fetch posts, chat messages, and comments from Telegram.

02

Authenticate securely with Telegram API using provided credentials.

03

Load Telegram data into LlamaIndex Document format.

04

Limit the number of fetched messages for efficient data handling.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/li-reader-readers-telegram | bash

Overview

Telegram Loader

The Telegram Loader fetches posts, chat messages, and comments from Telegram channels or chats into LlamaIndex documents, authenticated via a Telegram API ID and hash plus a one-time interactive login. Use it when you need Telegram channel or chat content loaded into LlamaIndex. It requires your own Telegram API ID and hash, plus a one-time interactive auth step.

What it does

The Telegram Loader fetches posts, chat messages, and comments from Telegram channels or chats into LlamaIndex Document objects. You initialize TelegramReader with a session name, your Telegram API ID and hash, and your phone number, then call load_data with the target channel or chat's entity name.

When to use - and when NOT to

Use it when you need Telegram channel or chat content pulled into LlamaIndex documents, for example a public channel's posts by URL. It requires your own Telegram API ID and hash (obtained by logging into my.telegram.org and creating an application under API Development tools -- Telegram treats the hash as secret and irrevocable, so it should never be posted anywhere), plus completing a one-time interactive login (entering a security code and password sent to your account), so it is not usable without both the API credentials and that initial auth step. The API ID and hash belong to your application, not your phone number, and can be reused with any phone number.

Inputs and outputs

Install with:

pip install llama-index-readers-telegram

Authenticate and load messages from an entity:

from llama_index.readers.telegram import TelegramReader

loader = TelegramReader(
    session_name="[YOUR_SESSION_NAME]",
    api_id="[YOUR_API_ID]",
    api_hash="[YOUR_API_HASH]",
    phone_number="[YOUR_PHONE_NUMBER]",
)
documents = loader.load_data(
    entity_name="https://t.me/[ENTITY_NAME]", limit=100
)

The session_name you pass becomes the name of a .session file created in your working directory on the first load_data() run; if that file already exists, the loader skips re-authentication, so moving or renaming it will force a fresh login. The first run's interactive auth asks for a security code and your account password. load_data takes an entity_name (a channel or chat URL) and a limit on how many items to fetch.

Who it's for

Developers building LlamaIndex pipelines that need Telegram channel or chat content -- posts, messages, comments -- loaded as documents.

Source README

Telegram Loader

pip install llama-index-readers-telegram

This loader fetches posts/chat messages/comments from Telegram channels or chats into Documents.

Before working with Telegram’s API, you need to get your own API ID and hash:

  1. Login to your Telegram account with the phone number of the developer account to use.
  2. Click under API Development tools.
  3. 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.
  4. 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!

This API ID and hash is the one used by your application, not your phone number. You can use this API ID and hash with any phone number.

Usage

The first parameter you pass to the constructor of the TelegramReader is the session_name, and defaults to be the session name (or full path). That is, if you create a TelegramReader(session_name='anon', ...) instance and run load_data(), an anon.session file will be created in the working directory where you run this loader.

The Auth procedure asks for:

  • Security Code
  • Password
Please enter the code you received: 12345
Please enter your password: *******
(You are now logged in)

If the .session file already existed, it will not login again, so be aware of this if you move or rename the file! See here for more instructions.

To use this loader, you simply need to pass in a entity name.

from llama_index.readers.telegram import TelegramReader

loader = TelegramReader(
    session_name="[YOUR_SESSION_NAME]",
    api_id="[YOUR_API_ID]",
    api_hash="[YOUR_API_HASH]",
    phone_number="[YOUR_PHONE_NUMBER]",
)
documents = loader.load_data(
    entity_name="https://t.me/[ENTITY_NAME]", limit=100
)

Examples

This loader is designed to be used as a way to load data into LlamaIndex.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.