Lightweight Telegram Keyword Alert Bot for Personal Use

Preface

I previously used a Telegram keyword alert bot project.

After using it, I felt the functionality was a bit heavy. The original project was designed for multi-user use, where different users follow different keywords and different groups. Therefore, the original project required a database.

I plan to develop a lightweight Telegram keyword alert bot suitable for personal use.

I'll write the code with the help of the powerful GPT.

The first version of the code was generated using the free Claude.

Subsequent code debugging was done using the free M$ Copilot.


Designed Features

The bot only accepts control commands from users or groups with specified IDs, 
After the bot detects a keyword, it sends a notification message to the user, group, or channel with the specified ID,
The list of followed keywords supports regular expressions
The list of excluded keywords supports regular expressions
Both the list of followed keywords and the list of excluded keywords are updated and saved in the configuration file

Designed Configuration File

The configuration file is in YAML format, with the following content:
# Account information
account: 
  # User that listens for messages
  api_id: '1400003'
  api_hash: 'd11xxxxx112a7e059e831'
  user_phone: '+86190000010'

  # Bot that sends messages
  bot_token: '1000007:AAHNh8axxxxxxxxxxxxxxxxHA'
  bot_username: 'keyxxxxxrt_bot'

# LOG
logger:
  path: null # e.g. /root/absolute-path/   default null: {_current_path}/logs/
  level: INFO # FATAL,ERROR,WARN,INFO,DEBUG,NOTSET

# Proxy
proxy:
  type: SOCKS5 # e.g. SOCKS4, SOCKS5, HTTP
  address: null  # e.g. 127.0.0.1
  port: null # e.g. 1088

# Non-public service
# The bot only accepts commands from the following IDs; can be set to user or group IDs
command_id_list: 
  - 123456789
  - 987654321
# The bot's notification messages are sent to the following IDs; can be set to user, group, or channel IDs
result_id_list: 
  - 123456789
  - 987654321

# Do not process messages from bots
# For example, some groups have auto-reply bots that reply with duplicate messages; or some spam bots join the group and start flooding
ignore_bot_msg: true

# Temporarily disable some data sources without requiring the user to leave the group or channel
source_filter: false # Switch, default false
source_filter_ignore_list:
  - 123456789
  - 987654321

# List of followed keywords, supports regular expressions
keyword_list:
  - /keyword1|keyword2|keyword3/ig
  - /keyword4|keyword5|keyword6/ig

# List of excluded keywords, supports regular expressions
keyword_exclude_list:
  - /exkeyword1|exkeyword2|exkeyword3/ig
  - /exkeyword4|exkeyword5|exkeyword6/ig

Throwing the Feature Description and Configuration File to Claude


Setting Up the Environment

apt install -y python3-pip
pip3 install -r requirements.txt

Debugging

1. Verify the code for starting the user and bot

The correct startup result is, 

On the first run, the command line will prompt to enter a verification code.

Subsequent runs do not prompt for a verification code.



2. Disable the user's function of processing received new messages,
disable the bot's function of checking the command source,
and verify the code for the bot handling the /start command

The correct result is,

Send the /start command to the bot from any Telegram user, and the bot can receive the /start message and respond


3. Verify the command /list_keywords for querying the keyword list

The correct result is,

It can correctly display the followed keyword list and excluded keyword list from the configuration file


4. Verify the commands for adding and removing keyword lists 

/add_keyword - Add a followed keyword
/remove_keyword - Remove a followed keyword
/add_exclude - Add an excluded keyword
/remove_exclude - Remove an excluded keyword

The correct result is,

The modifications can be saved to the configuration file


5. Enable the function for the user to handle received new messages

Print debug information on the command line

The correct result is,

New messages can be received, and there is output on the command line


6. Verify the follow keyword list function and the function of sending notification messages to specified users

The correct result is,

Messages containing the keyword will send notifications to the specified user.

Messages not containing the keyword will not send notifications to the specified user.


7. Verify the function of sending notification messages to specified groups

The correct result is,

Messages containing the keyword will send notifications to the specified group.


8. Verify the function of sending notification messages to specified channels

The correct result is,

Messages containing the keyword will send notifications in the specified channel.


9. Verify the exclude keyword list function

The correct result is,

Messages containing the follow keyword and also containing the exclude keyword will not send notifications.

Messages containing the follow keyword and not containing the exclude keyword will send notifications.


10. Verify the block source function

The correct result is,

Receiving messages containing the follow keyword from normal sources will send notifications.

Receiving messages containing the follow keyword from blocked sources will not send notifications.


11. Enable the function for the bot to determine the command source, and verify the function

The correct result is,

Sending a message from a legitimate command source, the bot responds.

Otherwise the bot responds no permission to use.


That's it for the basic functions.

There are some self-righteous optimizations, no need to be verbose here.

Uploaded to GitHub

https://github.com/crazypeace/tg-keyword-monitor-bot


Actual effect

https://t.me/FuckGFWNewbieNeedHelp


The setup tutorial will be available soon. You can follow my blog. :)

https://zelikk.blogspot.com/p/icdyct.html

========

Comments