Filtering

In Junction Bot you can filter incoming Telegram messages by applying filtering rules. You decide which messages you receive and which will be filtered out so you will not see them (like advertisements, for example).

Filters can be applied to channels separately or to all channels at once. You can combine any quantity of filters of any kind.

Where is it 
Menu → Forwarding settings → Choose source and destination → Filters

Please notice that there is a very useful debugging feature, which may save you many hours.

⏵Alternative configuration using commands
/filter - create a filter (for 1 source) OR remove a filter if entered second time.
Combines with: 'mustHave', 'mustNotHave', 'every', type of attachement 

/filterrx - filtering by regex (see examples here)

/filterall, /filterrxall - create a filter for all sources

/filter source_channel_reference - see all current filters applied to a channel 

If you have multiple forwarding rules from the same source to different destinations, you can set different settings, filters and replacements for different destinations. Add the following construction to the source_channel_reference: ->-1001251642436
Where -1001251642436 is the identifier of the destination channel in the Telegram system plus prefix "-100" (it's the way how Telegram distinguishes numbers if channels from numbers of other objects). To find this number is very simple, search the Internet for instructions. For example, forward a message from the channel to the bot @junction_helper0_bot. There you will see the channel number in forward_from_chat.id or forward_from.id.
So, for example, the configuration command might look like this:
/filter source_channel_reference->-1001251642436 mustHave cats

🚦Permissive and denying filters

On the filter settings page, you can choose what to do with posts that fit the filter criteria.

🟢 A permissive filter (also known as a mustHave filter) will let through only those messages that fit the criteria. If you have one or more permissive filters, but a post doesn’t fit any of them, the post will definitely be stopped. In other words, multiple mustHave filters will be combined on an “or” basis.

🔴 A denying filter (also known as a mustNotHave filter) will stop posts that fit the criteria. If you have one or more denying filters and a post fits at least one of them, it will definitely be stopped. In other words, multiple mustNotHave filters will be combined on an “and” basis.

That is, if you have multiple permissive text filters and multiple denying text filters, a post will only be published if it fits at least one permissive text filter and does not fit any denying text filter.

These combination rules work within a set of filters of the same type. That is, for example, if you have permissive text match and permissive regular expression filters, a message will only be published if it matches at least one permissive text match and at least one permissive regular expression filter.

There are different types of filters: by text match, by regular expression, by date, etc. Filtering is performed for a set of filters of each type separately. That is, text filters are applied first, then regular expression filters, and so on. If as a result of applying, for example, text filters, the message should be stopped, it will be stopped regardless of the result of applying the other filters.

⏵Alternative configuration using commands
mustHave - you will receive only messages with a specific text
Format: /filter source_channel_reference mustHave text to receive
Ex. /filter https://t.me/junction_bot_news mustHave cool cats
Please remember that the text of a filter applies as a whole phrase, not as separate words. So in this example you will receive all messages containing an exact phrase "cool cats". You don't need to use any escape characters like   here.

mustNotHave - filters out messages with a specific text
Format: /filter source_channel_reference mustNotHave text to filter out
Ex. /filter https://t.me/junction_bot_news mustNotHave old hats

Examples:
/filter source_channel_reference mustHave cats
/filter source_channel_reference mustHave kittens
You will receive all messages containing either the word 'cats' or the word 'kittens'

/filter source_channel_reference mustNotHave dogs
/filter source_channel_reference mustNotHave puppies 
You will not receive any messages containing words 'dogs' or 'puppies'

🎯Filtering by an exact text match

Where is it  
Menu → Forwarding settings → Choose source and destination → Filters

This is the simplest type of filtering. The criterion works on the principle of exact text match, i.e. the filter condition is triggered only if the exact text of the criterion is found in the message text. Note that case is taken into account. That is, if the criterion consists of the word “bitcoin”, it will not work if the text contains only “Bitcoin”.

Remember that if the text of the criterion consists of several words, then the entire string of the criterion must appear in the text of the message, all the words, in that order and with the same delimiters. That is, if the criterion consists of the string “Buy bitcoin”, it will not work if the text contains only “Buy” or only “bitcoin” or, for example, “Buy or sell bitcoin”.

Filtering by media type

Use one of the following text filters to filter by media type: , <audio>, <video>, <sticker>, <voice>, <location>, <animation>, <videonote>, <poll>.

⏵Alternative configuration using commands
Examples:
/filter source_channel_reference mustHave <audio> - you will receive only messages containing audio attachments from the channel
/filter source_channel_reference mustNotHave <photo> - you will not receive any messages containing pictures from the channel

🔍 Filtering with regular expressions

Where is it   
Menu → Forwarding settings → Choose source and destination → Filters

Also, you can use regular expressions (RegEx) to do your filtering. For that use the command /filterrx. It has the same format.

⏵Alternative configuration using commands
To set this type of filters, use the /filterrx command. It works in the same way as the /filter command.
Here are some popular examples. They use commands. When working via a menu, just use the last argument as the criteria. For example, for the first example the criteria would be .*word.*<photo>.*

1. Stop posts having a word word in a caption for an image
/filterrx source_channel_reference mustNotHave .*word.*<photo>.*
2. Two words - word1 and word2 - in a certain order
/filterrx source_channel_reference mustNotHave .*word1.*word2.*
3. Two words in any order
/filterrx source_channel_reference mustNotHave (?=.*\bword1\b)(?=.*\bword2\b).*
5. Two words in any order, ignore the register
/filterrx source_channel_reference mustNotHave (?i:.*wOrd1.*woRd2.*)
6. Any word from a set
/filterrx source_channel_reference mustNotHave .*word1.*|.*word2.*|.*word3.*
7. You can combine them. For example, any of the words, case insensitive
/filterrx source_channel_reference mustNotHave (?i:.*wOrd1.*|.*worD2.*|.*woRd3.*)
8. Any word containing a Russian alphabet
/filterrx source_channel_reference mustNotHave .*([а-яА-Я]+).*
9. Any HTTP link
/filterrx source_channel_reference mustNotHave .*(http:\/\/|https:\/\/).*
10. Any number
/filterrx source_channel_reference mustNotHave .*([0-9]+).*
11. Any @username
/filterrx source_channel_reference mustNotHave .*@([a-zA-Z0-9=&_\-]+).*

It’s important to understand what RegEx is before using these filters. For example, the filter with the regular expression word2 DOES NOT MATCH the message word1 word2 word3, as it contains words other than word2. Remember to add .* On both sides of the word: .*word2.*

Note an important nuance: when filtering, the bot can add a space and/or a media content code at the end of the text. Therefore, we advise you to always put .* at the end of your RegEx filters.

Another important point: RegEx for filters usually has a .* at the beginning and at the end, because you want the whole message to fit the expression. If you want to use a similar expression for replacements, you probably want to remove .* (otherwise the replacement will replace the whole text of the message).

There is a very convenient instrument to test regular expressions: https://regex101.com. Set the Flavor to “Java 8” and set the flag “Single line: Dot matches the new line”. This will make it work in the same way as @junction_bot’s matcher.

Help with regular expressions

Regular expressions are a vast topic. Help in this area is beyond the scope of the bot administration. Please do not write to admins in private asking them to make you a replacement rule or a filter. Also, the bot administration does not provide this kind of help in the support group.

The easiest way to have a regular expression created for your needs is to ask ChatGPT, it’s quite good in it.

Also, you can look for help in appropriate forums. For example StackOverflow or CodeProject.

Setting a filter for all your sources

To add a filter to all your current sources, use the /filterall or /filterrxall (for regular expressions) command.

Please note that new forwarding rules are always created with an empty filter list. You can copy filters to them from an existing source.

For instance:
/filterrxall add mustNotHave .*(http:\/\/|https:\/\/).*

This command will add to all your sources a filter, which will stop any message containing a link.

Copy filters

In Junction Bot you can copy all filters from one Telegram channel to another. To do this use the following command format:

/filter channel_to_copy_filters_to addAllFrom channel_to_copy_filters_from

Editing or removing filters

The most convenient way to edit and delete filters is via the menu here: Menu → Forwarding settings → Choose source and destination → Filters. If you need to remove filters from all sources at once, use the commands.

⏵Alternative configuration using commands

To see all current filters applied to a channel use command

/filter source_channel_reference

To delete all filters from all your channels use the command

/filterall DELETE ALL

For RegEx filters use /filterrx and /filterrxall correspondingly.

Receiving only every N-th message

In Junction bot you can filter Telegram messages by frequency, so only every n-th message will be copied (in the following example – every second message). To do this, use the following command format:

/filter source_channel_reference every 2

🕓Filtering by date and time

Where is it   
Menu → Forwarding settings → Choose source and destination → Filters

This type of filter allows you to set rules for stopping messages based on when they arrive.

⏵Alternative configuration using commands
/filtertime source_channel_reference mustHave time interval

The filter criterion is the time interval. It specifies:

  • The period when messages should arrive for 🟢 permissive filters (mustHave).
  • The period when messages should not come for 🔴 denying filters (mustNotHave).

Time interval may contain day of the week, time and timezone.

Day of the week. It always starts with a letter w and then follow the numbers of the days of the week (starting from Mon).
Example:

w135
Corresponds to the 1st, 3rd and 5th days of the week (Mon, Wed, Fri)

w67
Corresponds to the 6th and 7th days of the week (Sat, Sun)

Time. Time is always in 24h format, an interval from – to (inclusive), hours:minutes.
Examples:
9:30-17:30
0:00-23:59

Timezone. Please see the list of available timezones here. If you do not explicitly mention the timezone then it will be set to CET – Central European Time (Amsterdam/Berlin/Prague/Vienna)

Examples:

/filtertime source_channel_reference mustNotHave 0:00-7:00
Messages will not come from midnight till 7 AM at CET.

/filtertime source_channel_reference mustHave w12345 8:00-20:00 America/New_York
Messages will come only on working days (Mon-Fri) starting from 8 AM and till 8 PM at New York time.

To add a temporal filter to all your sources you can use the command /filtertimeall. For example:
/filtertimeall add mustNotHave 0:00-7:00
To remove:
/filtertimeall remove mustNotHave 0:00-7:00

💬Filter by topic in a group

Where is it   
Menu → Forwarding settings → Choose source and destination → Filters

This type of filter allows you to set rules for stopping posts depending on which thread (topic) they were posted in.

⏵Alternative configuration using commands
/filtertopic source_channel_reference mustHave topicID

The criterion is the topic number. To find out the group’s topic number, take a link to any post in the topic. The number in the middle of the link is the topic number. For example, if the link is https://t.me/publicchat/1723/36753, the topic number is 1723.

Examples:

/filtertopic source_channel_reference mustHave 1723
Allow only posts from topic number 1723.

/filtertopic source_channel_reference mustNotHave 7231
Filter out posts from topic number 7231.

Important exception:
If the link has topic 1, then the filter should have topic 0. For instance, if your link is https://t.me/publicchat/1/36753, then the command should be
/filtertopic source_channel_reference mustHave 0

To copy messages from different topics in the source to different destination channels, first create the necessary forwarding rules from source to destination. Then, for each specific forwarding rule, set the desired topic filter.

To copy messages from topics in the source to the same topics in the destination, you need to create destination topics manually. Then you need to create an intermediate channel for each topic. For each topic, you first set up copying to the intermediate channel with the desired topic filter. Then from the intermediate channel to the destination with the desired topic setting.

Removing duplicates

Where is it   
Menu → Forwarding settings → Choose source and destination → Duplicates

In some cases one message can come to you several times. For example, if you are subscribed to two channels that both have forwarded the same post. To filter out all identical Telegram messages in Junction Bot use the command /setting duplicate. It check for identical messages within a history of last 300 messages.

⏵Alternative configuration using commands
/setting duplicate + one of the following options:

pass - disabled, allow duplicates
Ex. /setting duplicate pass

filter - filter duplicated messages out
Ex. /setting duplicate filter

To apply this setting to one channel only, replace the command /setting with /settingchannel source_channel_reference (read more).

Please notice, that the filter of duplicates compares original messages. It means that if you want to avoid duplicates in the destination channel, appeared as a result of your replacement rules, you need to use a slightly more complex approach – through an intermediate channel.

Suppose we need to redirect messages from A to B. Create channel C and set up a redirection from A (source) to C (intermediate). Apply all your replacement rules to channel A. You don’t need to create a duplicate filter for channel A.

Next, set up a redirect from C (intermediate) to B (destination). We set a filtering rule on this redirection (with the /settingchannel C duplicate filter command), which will remove all the duplicated, even those that appeared as a result of replacement rules.

Done! If you would set a duplicates filter for the first redirect (from A to C), then it would not affect the duplicates that appeared as a result of replacement rules.

Note that the duplicate filter does not work with the delivery forward_client setting.

Bulk modification of filters

/filterall – this command will add this filter to all your subscriptions and forwardings. Please notice that there is no such thing as a common set of filters – a set of filters applied to all the rules does not exist. There is a set of filters per concrete subscription / forwarding rule.

/filterall works with the same combinations as /filter.

Instead of source_channel you set an action: add or remove.

/filterall add - adds a new filter to all channels.
Format: /filterall add [rule] text to receive
Ex. /filterall add mustHave bitcoin  OR /filterall add mustNotHave litecoin

/filterall remove - removes the filter from all channels.
Format: /filterall remove [rule] text to filter out
Ex. /filterall remove mustHave bitcoin  OR /filterall remove mustNotHave litecoin

Change the command to /filterrxall to use regular expressions.

👤Filtering by message author

Where is it   
Menu → Forwarding settings → Choose source and destination → Filters

This type of filter allows you to set rules for stopping messages depending on the number (ID) of the message author. For anonymous messages, the author number is the number of the corresponding group or channel. Note that it is the number, not the author’s name, that is the criterion.

⏵Alternative configuration using commands
/filtertopic source_channel_reference mustHave topicID

Filtering by author number is the most efficient and convenient way, we recommend using it. But sometimes you may want to filter by author name. To filter by author name rather than author number, the procedure is a bit more complicated. The rest of this article is devoted to this more complicated method.

  1. First you need to enable the display of the author’s name in the forwarding settings: Menu → Forwarding Settings → Select Source and Destination → Author
  2. Then you need to add a text filter containing the author’s name as a criterion
⏵Alternative configuration using commands
  1. Create a new forwarding rule with the command /new
  2. Turn on the copying of authors’ names with the command
    /settingchannel source_channel_reference author full
  3. Set up the filter for the name you need with the command
    /filter source_channel_reference mustHave user_reference
    If you need the message to be from a specific user and at the same time contain a specific text, then you need a slightly different command:
    /filterrx source_channel_reference mustHave .*user_reference.*specific text.*

In most cases as a user_reference you can use the @username of the user. In some cases it will not work (for example if the user doesn’t have a public username or the bot doesn’t have full information about it). In this case, you need to enable an explanation report and see how the user is referenced in the message and then use this string.

With the author setting the resulting messages will contain the name of the author of the message. Sometimes it is necessary that the author’s name is not present. This will require a slightly more complex process – through an intermediate channel.

⏵ Configuration details with commands via the intermediate channel

Suppose we need to redirect messages from A to B. Create channel C and set up a redirection from A (source) to C (intermediate). Apply the settings from pp 2 and 3 to it:
/settingchannel A author full
/filter A mustHave @user

Next, set up a redirect from C (intermediate) to B (destination). We set a replacement rule on this redirection (with the /replace command), which will remove the names of the authors.
/replace B @user&nbsp;(User's&nbsp;name)

Done! If you set this replacement rule for the first redirect (from A to C), then it will not affect the author’s name. The author’s name, like the name of the source channel, is a service text and is not affected by the /replace command, and can be adjusted in other ways.

Filtering forwarded posts

You can make Junction Bot filter the Telegram messages, that were forwarded to a source channel. Let’s say @source is the source channel. To copy only original messages @source, but not the messages forwarded to @source from other channels, do the following:

  1. Create a new forwarding rule with the command /new
  2. Turn on the copying of authors’ names with the command
    /settingchannel source_channel_reference header from_only
    It will add at the beginning of the forwarded message a piece of text: “from ” + the name of the channel.
  3. Set up the filter for the name of the channel you want to filter out. For example, if forwarded posts come from a channel named “Source of forwards”
    /filter source_channel_reference mustNotHave from Source of forwards
    If you want to get rid of posts forwarded from any other channel, then you need a slightly different command:
    /filter source_channel_reference mustNotHave from

AI filtering

You can describe any filtering criteria in free form and let the AI automatically filter your messages. Interestingly, this way you can filter not only by the text of the message as such, but also by its meaning and sentiment. For example, you can ask the AI to only let positive news in your channel ☺️

How to set this up, see here.

Manual filtering

If you want to have manual control over whether posts should be copied or skipped, use the review mode.

Finding out what is not working

Check what are the current filters and that they are correct:

/filter source_channel_reference

If you set up a lot of filters it can be difficult to figure out why a certain message is passing or why it is not. To help you in that there is a special setting called explain:

Menu → Forwarding settings → Choose source and destination → Explain-report

If it’s enabled you will receive a report for every new message coming from this source_channel. The report will explain why this message is filtered out or allowed to pass.