This project is a Telegram bot developed using Golang and PostgreSQL. The bot saves all messages sent in a chat and stores messages that contain a specific filter word in a separate table.
-
Add your telegram bot token:
- Go to docker-compose.yml and on 52nd line you will see the following: "TELEGRAM_BOT_TOKEN=TELEGRAM_TOKEN"
- Replace "TELEGRAM_TOKEN" with your token.
-
Start all services:
docker-compose up
This command will pull the necessary images from Docker Hub and start all services.
The database is at port 5435.
-
Send commands to the bot:
- The bot has the following username: @message_filtering2_bot (https://t.me/message_filtering2_bot)
- /start: The bot will respond with a welcome message.
- /filter + word: The bot will save the word and filter messages by it.
- Any other message will be saved to one of the tables, depending on whether it contains the filter word or not.
- Connect to the
db-psqlcontainer:docker exec -it telegram_bot-db-psql-1 psql -U postgres- Check available databases and connect to the
telegram_recordsdatabase:\l \c telegram_records - Select all unfiltered and filtered messages and exit psql shell:
SELECT * FROM messages; SELECT * FROM filtered_messages; \q
- Check available databases and connect to the
docker-compose down-
Project Structure: The project is split into three main directories:
initDirectory: Contains themain.gofile which connects to a database and initialisesMessageHandlerandMessagesDBstructures as well as the Telegram bot.handlersDirectory: Contains functions for processing incoming messages and commands.storageDirectory: Works with the database.
-
Handler and Storage Functions:
- Handler functions are encapsulated within the
MessageHandlerstructure. This allows shared variables such asmessagesDB(for database operations) andfilterWord(for filtering messages) to be easily accessed and managed. - Storage functions belong to the
MessagesDBstructure, to ensure that sql database can be accessed from the functions.
- Handler functions are encapsulated within the
-
Docker Compose:
- Docker compose is used and separate container is created to make migrations to the database