micl2e2 / tg-focus

A comprehensive Telegram message filter/forwarder that helps you focusπŸ’‘.
GNU General Public License v3.0
95 stars 3 forks source link
c-plus-plus c-plus-plus-20 cplusplus tdlib telegram telegram-bot telegram-bot-api telegram-chat telegram-filter telegram-forwarder telegram-message-sender telegram-userbot userbot

TG-FocusπŸ’‘

GitHub License Static Badge GitHub commits difference between two branches/tags/commits

Table of Contents

Overview

tg-focus is a comprehensive Telegram message filter that helps you focus. A gentle User Bot for message forwarding.

Most of the message types are supported:

  1. Chat Message
  2. Channel Update
  3. ...and more

By customizing Focus Filters, tg-focus can forward any specific message from any chat to a dedicated chat named TG-FOCUS, that way users have full control of the messages they see.

Notable features:

Quick Start

Below is a simple 9-step demonstration of a common use case: forwarding the messages that contains the keywords we want, ignoring the message that sent from the users we don't want.

Back to start

Back to start

Back to start

Back to start

Back to start

Back to start

Back to start

Supported Platforms

Platforms

Never(❌). Maybe In Future(❓). Currently Supported(βœ…).

Operating System Support(x86_64)
macOS ❌
Windows ❓
Windows with WSL 1 βœ…
Windows with WSL 2 βœ…
AlmaLinux 9 βœ…
AlmaLinux 8 βœ…
Amazon Linux 2023 βœ…
Amazon Linux 2 ❌
Amazon Linux 1 ❌
Arch Linux βœ…
Debian 12 βœ…
Debian 11 βœ…
Debian 10 βœ…
Fedora 39 βœ…
Fedora 38 βœ…
Fedora 37 βœ…
openSUSE Leap 15.5 βœ…
openSUSE Leap 15.4 βœ…
openSUSE Tumbleweed βœ…
Oracle Linux 9 βœ…
Oracle Linux 8 βœ…
Oracle Linux 7 ❌
Rocky Linux 9 βœ…
Rocky Linux 8 βœ…
Slackware 15.0 βœ…
Slackware 14.2 ❌
Solus βœ…
Ubuntu 23.10 βœ…
Ubuntu 23.04 βœ…
Ubuntu 22.04 βœ…
Ubuntu 20.04 βœ…
Void Linux βœ…

Note: this is not an exhaustive list, missing platforms can be added to the list by opening an issue or a pull request.

Languages:

Language Status
English (United States) βœ…
Others Add Your Language

Note: More languages can be supported! Follow the internationalization instructions(docs/i18n.org) to add your language.

How To Use

Before anything make sure you have:

  1. A Telegram account

There are two different ways to deploy tg-focus, through Docker/Podman, or using prebuilt binary:

Binaries

Step 1:

Download the binaries at GitHub releases page. Please check whether your platform is supported or not.

Step 2:

Login your Telegram account firstly, it needs your phone number (in international format, e.g. +1234567 if your phone number is 234567 with region code +1) and a login code:

/path/to/tgf-conf auth

If you've logged in successfully, the last line should be:

[tgf INFO] Logged in!

Step 3: (Optional)

Using different language other than default(English) by:

/path/to/tgf-conf lang XX-YY

Note: XX is the language code, YY is the region code, e.g. "en-US" represents "English (United States)", "en-GB" represents "English (United Kingdom)", etc. All valid codes can be found at http://www.lingoes.net/en/translator/langcode.htm

Step 4:

Launch tg-focus daemon:

# run in foreground
/path/to/tgf-focusd

# or run in background
nohup /path/to/tgf-focusd &

Step 5:

If logined successfullly, a new chat TG-FOCUS will be created, the subsequent messages will be filtered and forwarded to that chat.

Step 6:

Note that by default, tg-focus will forward all messages you receive. You can change the Focus Filters(the filter configuration) any time you like, to custom the messages you'd like to receive on that chat. See examples.

/path/to/tgf-conf filters

(NOTE: This will open the GNU nano editor for configuration modification. After modification, press Ctrl-O, Enter, Ctrl-X to finish the work. As long as the configuration is valid, the last line of output shall be "Saving filters...")

Step 7:

Done.

Docker/Podman

Make sure have Docker or Podman installed on your machine:

Step 1:

Pull the image and run it in the background, assuming the newly created container's name is CONTAINER-NAME:

docker pull micl2e2/tg-focus
docker run -d micl2e2/tg-focus

Step 2:

Login your Telegram account firstly, it needs your phone number (in international format, e.g. +1 234567 if your phone number is 234567 with region code +1) and a login code:

docker exec -it CONTAINER-NAME tgf-conf auth

If you've logged in successfully, the last line should be:

[tgf INFO] Logged in!

Step 3: (Optional)

Using different language other than default(English) by:

docker exec -it CONTAINER-NAME tgf-conf lang XX-YY

Note: XX is the language code, YY is the region code, e.g. "en-US" represents "English (United States)", "en-GB" represents "English (United Kingdom)", etc. All valid codes can be found at http://www.lingoes.net/en/translator/langcode.htm

Step 4:

If logined successfullly, a new chat TG-FOCUS will be created, the subsequent messages will be filtered and forwarded to that chat.

Step 5:

Note that by default, tg-focus will forward all messages you receive. You can change the Focus Filters(the filter configuration) any time you like, to custom the messages you'd like to receive on that chat. See examples.

docker exec -it CONTAINER-NAME tgf-conf filters

(NOTE: This will open the embeded GNU nano editor for configuration modification. After modification, press Ctrl-O, Enter, Ctrl-X to finish the work. If the modified one is valid, the last line of output shall be "Saving filters...")

Step 6:

Done.

Step 7: (Optional)

Use following command if you want to destroy the instance and its Telegram-related credentials and resource:

docker rm --force CONTAINER-NAME

Filtering Rules

A Focus Filter is a filter used by tg-focus to match against the message's text content(text, emoji, or media caption). They are:

Users can add as many filters as they like, they accept simple text or regular expression.

When a message comes, they are tried one by one, if there is any filter than can match the message and not skip the message , it will be forwarded.

Currently Focus Filter can match:

can skip:

can reject:

A message is forwarded if and only if:

(Note that the first three rules can be regarded as whitelist, the second two rules can be regarded as weak blacklist, the third one can be regarded as strong blacklist)

Filter Examples

Say we want to forward all messages in a chat named "πŸ‘AnAwesomeChatπŸ‘":

works, the chat title is fully matched:

[[focus-filter]]
title = "πŸ‘AnAwesomeChatπŸ‘"

works, the chat title is partly matched:

[[focus-filter]]
title = "AnAwesomeChat"

works, the chat title is partly matched:

[[focus-filter]]
title = "Awesome"

works, the chat title is partly matched:

[[focus-filter]]
title = "πŸ‘"

works, the regular expression matches the chat title:

[[focus-filter]]
title = ".*Awesome.*"

Say we want to forward the messages that contain "football", but not "basketball", from a chat named "Sports User Group". We can write our filters as

[[focus-filter]]
title = "Sports User Group"
keywords = ["football", "soccer"]
no-keywords = ["basketball"]

Say we want the messages containing "football", or any other messages except the ones containing "basketball", and the ones sent by the user whose full name is "Basketball Lover", or the ones sent by the user whose user ID is "alice_love_basketball", from a chat named "SportsUserGroup". We can write the filter as:

# 1st
[[focus-filter]]
title = "Sports User Group"
keywords = ["football", "soccer"]

# 2nd
[[focus-filter]]
title = "Sports User Group"
no-keywords = ["basketball"]
no-senders = ["Basketball Love", "@alice_love_basketball"]

So these message will be forwarded:

these will NOT be forwarded:

Troubleshooting

My TG-FOCUS stops formarding message

Try to restart tgf-focusd program or container.

Q & A

Can I trust tg-focus?

Yes.

First of all, consider that tg-focus is a GPL-licensed free software.

tg-focus's source as well as its dependencies' source are completely open. Additionally, all its binaries and OCI images will not be built by any individual, but by Github-hosted machines. All build details can be found in the dedicated repository tg-focus-cicd.

It only saves Telegram server credentials on the user's machine because they are necessary during initialization. And they are completely yours-irrelavent.

However, since tg-focus is largely built on top of TDLib, how much you can trust tg-focus largely depends on how much you trust TDLib and Telegram's governance. Hence use at your own risk.

What makes tg-focus a gentle User Bot?

TDLib states that it is a library for building Telegram clients. By embedding TDLib, tg-focus, like any other User Bot, is able to use all TDLib's public APIs. While it opens the possibility for many powerful features, it also means User Bots have far more privileges than a general-purpose Bot. This can be a double-edged sword.

There are already some examples where some of the good-purpose User Bots disappoint their users because of their misuse of Telegram APIs, such as repeatedly sending random messages to users' random chats, or other unmanaged behaviors. This is probably one of the reasons why Telegram User Bots have a bad reputation.

However, tg-focus will not follow their paths, we have one strict principle: all message collected by tg-focus will only be forwarded to a newly created chat.

With this principle, it is firmly guaranteed that no messages generated by tg-focus will be ever sent to the wrong chat. Additionally, there is no reason for tg-focus users to worry about whether any existing or new features will be harmful or not. By the time of updates, tg-focus will remain gentle.

Development

See docs/development.org.

Contributing

tg-focus is still far from perfect, any form of contribution is welcomed!

License

tg-focus is licensed under GNU General Public License Version 3.0.