The idea is very simple: you have a Telegram channel and just want to automatically backup your posts to GitHub.
Sounds easy. But how to do this with less hassle and without registering of additional Telegram and GitHub Apps IDs? Just follow these steps.
Now it only backups text posts, post-images, text posts with images and also downloads your telegraph articles in
html
format. Update for backuping posts with other types of media will come later.
-
In Telegram find
@BotFather
, Telegram’s tool for creating and managing bots. -
Use the
/newbot
command to create a new bot. -
@BotFather
will ask you for a name and username, then generate an authentication token for your new bot.The 'name' of your bot is displayed in contact details and elsewhere.
The 'username' is a short name, used in search, mentions and t.me links. Usernames are 5-32 characters long and not case sensitive – but may only include Latin characters, numbers, and underscores. Your bot's username must end in 'bot', like 'tetris_bot' or 'TetrisBot'.
The 'token' is a string, like
110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
, which is required to authorize the bot and send requests to the Bot API. Keep your token secure and store it safely, it can be used by anyone to control your bot.Unlike the bot’s name, the username cannot be changed later – so choose it carefully.
-
Add newly created bot to your channel as subscriber or administrator.
Just follow the instructions. Be sure to grant repository read-and-write permissions for actions
and contents
. You can also restrict permissions to only this repository.
Follow the instructions and add AUTH_TOKEN
and BOT_TOKEN
secrets
- Copy code from action_workflow_example
- Replace
CHANNEL_REPO
andBRANCH
placeholders with your names (13-14 lines) - Action is triggered on schedule at 23:30 UTC every day (line 7). Look here if you want to change it.
- action is triggered on schedule at 23:30 UTC every day
- installs
python-telegram-bot
andPyGithub
- makes
_posts
folder for posts - run
backup_telegram_channel.py
with usage ofBOT_TOKEN
- run
upload_to_github.py
with usage ofAUTH_TOKEN
- gets channel updates for last 24 hours and saves as dictionary of the newly created or the last version of updated posts, uses python-telegram-bot library
python-telegram-bot
is a wrapper of the Telegram Bot API. Incoming channel updates are stored on the server no longer than 24 hours. That's why action in the orkflo is triggered once every day. If you need more functionality, then look into full Telegram API, register new Telegram App and useapi_id
and anapi_hash
for authentification. This script should be rewritten for usage of another API. - save all posts from dictionary in the
YYYY-MM-DD-postId.md
format in_posts
folder
- gets all files in your channel backup repository as a set
- commits and pushes all posts from
_posts
folder with right commit message (updated or created post)
If you already have a channel and there are already posts older than last 24 hours, then this optional script is for you. You need to have Python3 and Telegram Desktop installed. Telegraph and BeautifulSoup are used to prettify html string from Telegraph articles.
- Checkout this repository and install necessary dependancies:
$ git clone https://github.com/CatUnderTheLeaf/telegram_channel_backup.git $ pip install PyGithub $ pip install 'telegraph[aio]' $ pip install beautifulsoup4 # create folder for old posts in json format $ cd telegram_channel_backup $ mkdir json_dump
- Go to Telegram Desktop, open your channel, click on three dots sign in the upper right corner and select
export chat history
, selectJSON
as format andjson_dump
as path - Convert posts and upload them to your repository:
# convert json format to posts in the `YYYY-MM-DD-postId.md` format in `_posts` folder # json_dump is a folder with dumped history and images $ python backup_telegram_channel_history.py dump_dir=json_dump # replace placeholders for actual values $ python upload_to_github.py auth_token=$AUTH_TOKEN repo=$CHANNEL_REPO branch=$BRANCH