It connects to Telegram's API. It generates JSON files containing channel's data, including channel's information and posts.

Overview

Telegram API


GitHub stars GitHub watchers Twitter estebanpdl License Open Source Made-with-python


Overview

It connects to Telegram's API. It generates JSON files containing channel's data, including channel's information and posts. You can search for a specific channel, or a set of channels provided in a text file (one channel per line.)

Files are saved by default in a folder called output/data. These folders are created by the script.

β”œβ”€β”€πŸ—‚ Telegram-api
|   └──main.py
|   β””β”€β”€πŸ—‚ config
|   	└──config.ini
|   β””β”€β”€πŸ—‚ output
|   	└──collected_chats.csv
|   	β””β”€β”€πŸ—‚ data
|   		└──file_messages.json
|   		└──channel.json
|   		└──etc.

Software required

Python required libraries

Installing

  • Via git clone
git clone https://github.com/estebanpdl/telegram-api.git

This will create a directory called tg-api which contains the Python scripts. Cloning allows you to easily upgrade and switch between available releases.

  • From the github download button

Download the ZIP file from github and use your favorite zip utility to unpack the file tg-api.zip on your preferred location.

After cloning or downloding the repository, install the libraries from requirements.txt.

pip install -r requirements.txt

or

pip3 install -r requirements.txt

Once you obtain an API ID and API hash on my.telegram.org, populate the config/config.ini file with the described values.

[Telegram API credentials]
api_id = api_id
api_hash = api_hash
phone = phone

Example usage

main.py

This Python script will connect to Telegram's API and handle your API request.

Options

  • --telegram-channel Specifies Telegram Channel to download data from.
  • --batch-file File containing Telegram Channels to download data from, one channel per line.
  • --limit-download-to-channel-metadata Will collect channels metadata only, not channel's messages. (default = False)
  • --min-id Specifies the offset id. This will update Telegram data with new posts.

Examples

Basic request

python main.py --telegram-channel channelname`

Expected output

  • Excel file of collected channels
  • JSON file containing channel's profile metadata
  • JSON file containing posts from the requested channel

Request using a text file containing a set of channels

python main.py --batch-file './path/to/channels_text_file.txt'

Expected output

  • Excel file of collected channels
  • JSON files containing channels' profile metadata
  • JSON files containing posts from each requested channel

These examples will retrieve all posts available through the API from the requested channel. If you want to collect channel's information only, without posts, you can run:

Limit download to channel's metadata only

python main.py --telegram-channel channelname --limit-download-to-channel-metadata

or, using a set of telegram channels via a text file:

python main.py --batch-file './path/to/channels_text_file.txt' --limit-download-to-channel-metadata

Updating channel's data

If you want to collect new messages from one channel, you need to identify the message ID from the last post. Once you identify the id, run:

python main.py --telegram-channel channelname --min-id 12345

Expected output

  • Excel file of collected channels - based on new messages
  • JSON file containing channel's profile metadata
  • JSON file containing new messages posted after the requested ID (min ID)

build-datasets.py

python build-datasets.py

This Python script reads the collected files and creates a new dataset containing messages from the requested channels. By default, the created dataset in CSV format will be located in the output folder.

β”œβ”€β”€πŸ—‚ Telegram-api
|   └──main.py
|   β””β”€β”€πŸ—‚ output
|   	└──msgs_dataset.csv

channels-to-network.py

python channels-to-network.py

This Python script builds a network graph. By default, the file will be located in the output folder. The script also shows a preliminary graph using the modules matplotlib, networkx, and python-louvain, which implements community detection. You can use import the graph file in different softwares, including Gephi.

β”œβ”€β”€πŸ—‚ Telegram-api
|   └──main.py
|   β””β”€β”€πŸ—‚ output
|   	└──Graph.gexf

You might also like...
Telegram Bot to store Posts and Documents and it can Access by Special Links.

File-sharing-Bot Telegram Bot to store Posts and Documents and it can Access by Special Links. I Guess This Will Be Usefull For Many People..... πŸ˜‡ .

File-sharing-Bot: Telegram Bot to store Posts and Documents and it can Access by Special Links.

File-sharing-Bot Telegram Bot to store Posts and Documents and it can Access by Special Links. I Guess This Will Be Usefull For Many People..... πŸ˜‡ .

Telegram Bot to store Posts and Documents and it can Access by Special Links.
Telegram Bot to store Posts and Documents and it can Access by Special Links.

Telegram Bot to store Posts and Documents and it can Access by Special Links. I Guess This Will Be Usefull For Many People..... πŸ˜‡ . Features Fully cu

An script where it logs in your instagram account and follows people and likes their posts

InstaFollower An script where it logs in your instagram account and follows people and likes their posts (uses the tags to fetch people) Requirements:

WaifuGen - A program made in waifuGen that generates SFW and NSFW waifus from the waifu.pics API
WaifuGen - A program made in waifuGen that generates SFW and NSFW waifus from the waifu.pics API

waifuGen A program made in waifuGen that generates SFW and NSFW waifus from the

Picot - A discord bot made to fetch images from Pexels and unsplash API and provide raw images directly in channels

Picot A discord bot made to fetch images from Pexels and unsplash API and provid

Exports saved posts and comments on Reddit to a csv file.

reddit-saved-to-csv Exports saved posts and comments on Reddit to a csv file. Columns: ID, Name, Subreddit, Type, URL, NoSFW ID: Starts from 1 and inc

A Python Instagram Scraper for Downloading Profile's Posts, stories, ProfilePic and See the Details of Particular Instagram Profile.
A Python Instagram Scraper for Downloading Profile's Posts, stories, ProfilePic and See the Details of Particular Instagram Profile.

βœ” βœ” InstAstra ⚑ ⚑ ⁜ Description ~ A Python Instagram Scraper for Downloading Profile's Posts, stories, ProfilePic and See the Details of Particular In

Takes upcoming items from a Google Calendar and posts them to Slack.
Takes upcoming items from a Google Calendar and posts them to Slack.

Google Calendar to Slack by Jason Snell - [email protected] This Python script scrapes upcoming items from Google Calendar HTML and posts them to S

Comments
  • Cannot cast InputPeerChat to any kind of InputChannel

    Cannot cast InputPeerChat to any kind of InputChannel

    OS : Ubuntu 21 Python : 3.10.4

    • I'm using : python main.py --batch-file 'channels_text_file.txt' as source of channels, with one channel per line.
    • I'm able to see the JSON/CSV files getting created

    After a successful installation of the requirements, I'm able to run it image

    but after a while, a few minutes, it crashes with this error :

    `Traceback (most recent call last): File "/home/peer/tg-api/main.py", line 176, in counter = write_collected_chats( File "/home/peer/tg-api/utils/init.py", line 142, in write_collected_chats ch['participants_count'] = process_participants_count(client, ch_id) File "/home/peer/tg-api/utils/init.py", line 73, in process_participants_count channel_request = loop.run_until_complete( File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete return future.result() File "/home/peer/tg-api/api/init.py", line 85, in full_channel_req return await client( File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/site-packages/telethon/client/users.py", line 30, in call return await self._call(self._sender, request, ordered=ordered) File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/site-packages/telethon/client/users.py", line 39, in _call await r.resolve(self, utils) File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/site-packages/telethon/tl/functions/channels.py", line 720, in resolve self.channel = utils.get_input_channel(await client.get_input_entity(self.channel)) File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/site-packages/telethon/utils.py", line 263, in get_input_channel _raise_cast_fail(entity, 'InputChannel') File "/home/peer/.pyenv/versions/3.10.4/lib/python3.10/site-packages/telethon/utils.py", line 138, in _raise_cast_fail raise TypeError('Cannot cast {} to any kind of {}.'.format(

    TypeError: Cannot cast InputPeerChat to any kind of InputChannel.`

    I'm not a Python dev in itself so any hint to help me in the right direction to debug this would be very appreciated, I'm not 100% but this error could entirely be only on my end.

    opened by benborges 5
  • Channels with spaces in their name?

    Channels with spaces in their name?

    I'm trying to connect to channels with spaces in their name and constantly get errors.

    $ python ./main.py --telegram-channel Channel\ Name
    
    Init program at Tue Nov  8 19:54:38 2022
    
    
    > Authorized!
    
    > Collecting data from Telegram Channel -> Channel Name
    > ...
    
    Traceback (most recent call last):
      File "/home/xxx/telegram/telegram-api/./main.py", line 161, in <module>
        entity_attrs = loop.run_until_complete(
      File "/home/xxx/miniconda3/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
        return future.result()
      File "/home/xxx/telegram/telegram-api/api/__init__.py", line 57, in get_entity_attrs
        return await client.get_entity(source)
      File "/home/xxx/miniconda3/lib/python3.9/site-packages/telethon/client/users.py", line 335, in get_entity
        result.append(await self._get_entity_from_string(x))
      File "/home/xxx/miniconda3/lib/python3.9/site-packages/telethon/client/users.py", line 574, in _get_entity_from_string
        raise ValueError(
    ValueError: Cannot find any entity corresponding to "Channel Name"
    

    I've tried with quotes and with \, same error.

    I also created a text file with the channel names using --batch-file, and it fails in the same manner.

    opened by Mycah 4
  • Setup.py?

    Setup.py?

    Setup.py is not found, keep getting error while running pip install -r requirements.txt that says python setup.py bdist_wheel did not run successfully. Using Windows Powershell as command line, virtual env already setup. Any input would be greatly appreciated, thanks.

    opened by KevKurian 2
  • How to get data from private telegram chanel?

    How to get data from private telegram chanel?

    Link of private channel is like this - "https://t.me/+fv4dNzN3i_tlZjBh" if I use this command - "python main.py --telegram-channel https://t.me/+fv4dNzN3i_tlZjBh" or this command - "python main.py --telegram-channel fv4dNzN3i_tlZjBh" or this command - "or this command - "python main.py --telegram-channel +fv4dNzN3i_tlZjBh"

    This generates error messages.

    Please find some way to use this repo for private channel too.

    opened by mahfuztdx61 0
Owner
Esteban Ponce de Leon
Esteban Ponce de Leon
Techie Sneh 19 Dec 3, 2021
A bot that connects your guild chat to a Discord channel, written in Python.

Guild Chat Bot A bot that connects your guild chat to a discord channel. Uses discord.py and pyCraft Deploy on Railway Railway is a cloud development

Evernote 10 Sep 25, 2022
An automated tool that fetches information about your crypto stake and generates historical data in time.

Introduction Yield explorer is a WIP! I needed a tool that would show me historical data and performance of my staked crypto but was unable to find a

Sedat Can Yalçın 42 Nov 26, 2022
A program to convert YouTube channel registration information into Json files for ThirdTube.

ThirdTubeImporter A program to convert YouTube channel registration information into Json files for ThirdTube. Usage Japanese https://takeout.google.c

Hidegon 2 Dec 18, 2021
Telegram Bot to save Posts or Files that can be Accessed via Special Links

OKAERI-FILE Bot Telegram untuk menyimpan Posting atau File yang dapat Diakses melalui Link Khusus. Jika Anda memerlukan tambahan module lagi dalam rep

Wahyusaputra 5 Aug 4, 2022
Repository containing the project files for CEN4020's Team Utah.

inCollege-Team-Utah Repository containing the project files for CEN4020's Team Utah. Contributors: Deepak Putta Jose Ramirez Fuentes Jaason Raudales C

Keylin Sanchez 3 Jul 12, 2022
Read API docs offline, CLI, supports DevDocs.io compatible JSON files

Read API docs offline, CLI, supports DevDocs.io compatible JSON files

Tero Karvinen 3 Oct 18, 2022
Scrapping malaysianpaygap & Extracting data from the Instagram posts

Scrapping malaysianpaygap & Extracting data from the posts Recently @malaysianpaygap has gotten quite famous as a platform that enables workers throug

Yudhiesh Ravindranath 65 Nov 9, 2022
Google Drive, OneDrive and Youtube as covert-channels - Control systems remotely by uploading files to Google Drive, OneDrive, Youtube or Telegram

covert-control Control systems remotely by uploading files to Google Drive, OneDrive, Youtube or Telegram using Python to create the files and the lis

Ricardo Ruiz 52 Dec 6, 2022
πŸ“’ Video Chat Stream Telegram Bot. Can ⏳ Stream Live Videos, Radios, YouTube Videos & Telegram Video Files On Your Video Chat Of Channels & Groups !

Telegram Video Chat Bot (Beta) ?? Video Chat Stream Telegram Bot ?? Can Stream Live Videos, Radios, YouTube Videos & Telegram Video Files On Your Vide

brut✘⁢⁹ // ユスフ 15 Dec 24, 2022