Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UIT Announcement Bot

This bot monitors the Announcements page of the Academic Affairs & Science Office at the University of Information Technology (UIT) and posts new announcements to Telegram.

How it works

  • Every 5 minutes by default (configurable via POLL_INTERVAL), the bot performs two tasks:
    1. Fetches announcements from https://sdh.uit.edu.vn/thong-bao-chung
    2. Reads unread emails from Gmail (Inbox + Spam) — if configured
  • It compares fetched items with a stored list of already-sent items and only posts new announcements.
  • Emails are marked as read after being posted to avoid duplicates.

Requirements

  • Go 1.21+ — see Go downloads
  • A Telegram Bot Token (free via BotFather)
  • A Chat ID (group or personal chat)

Step-by-step guide

Step 1: Create a Telegram Bot

  1. Open Telegram and search for @BotFather.
  2. Send /newbot and follow the prompts.
  3. Give your bot a name (e.g. UIT Notify Bot).
  4. Choose a username that ends with bot (e.g. uit_notify_bot).
  5. BotFather will provide a token after creation — keep it safe. Example:
    8676555434:AAHYMxTY5_dQipO75wg4QzEjiaJj2weFGcI
    

Step 2: Obtain the Chat ID

Option 1: Use @userinfobot (for a personal chat)

  1. Search for @userinfobot.
  2. Send /start — it will reply with your chat ID.

Option 2: Create a group and get its ID (recommended)

  1. Create a new group in Telegram.
  2. Add your bot to the group and give it Admin rights.
  3. Send any message in the group (e.g. "test").
  4. Run the following to fetch updates (replace BOT_TOKEN):
    curl -s "https://api.telegram.org/botBOT_TOKEN/getUpdates" | python3 -m json.tool
  5. Look for a line like "chat": {"id": -100xxxxxxxxx, "title": "...", "type": "supergroup"} — the group ID is the number (often negative), e.g. -1003986448291.

Note: If the result array is empty, send another message in the group and try again.

Step 3: Install Go

macOS (Homebrew):

brew install go

Ubuntu/Debian:

sudo apt install golang-go

Windows: Download the installer from go.dev/dl.

Verify:

go version
# e.g. go version go1.21.x

Step 4: Get the source and build

# Change into the project directory
cd auto-get-mails

# Download dependencies
go mod tidy

# Build the binary
go build -o uit-bot ./cmd/bot/

This produces an executable named uit-bot in the current directory.

Step 5: Run the bot

# Run from source
TELEGRAM_BOT_TOKEN=xxxx:yyyy \
TELEGRAM_CHAT_ID=-1003986448291 \
go run ./cmd/bot/

# Or run the built binary
TELEGRAM_BOT_TOKEN=xxxx:yyyy \
TELEGRAM_CHAT_ID=-1003986448291 \
./uit-bot

On the first run the bot will post all current announcements to Telegram; subsequent runs only post new announcements.

Step 6: (Optional) Monitor Gmail

The bot can read unread emails from Gmail (Inbox and Spam) and post them to Telegram with subject, sender, and content.

How it works:

  • In each cycle (default 5 minutes) the bot connects to Gmail via IMAP.
  • It finds unread emails in Inbox and Spam.
  • It posts them to the Telegram group (spam emails are prefixed with 💰 [SPAM]) and includes a link to view the email in Gmail.
  • After posting, an email is marked as read to prevent reposting.

Requirement: the Gmail account must have two-factor authentication (2FA) enabled.

Creating an App Password:

  1. Go to https://myaccount.google.com/security and enable 2-Step Verification if not enabled.
  2. Open https://myaccount.google.com/apppasswords
  3. Select Mail and Other (Custom name), enter UIT Bot.
  4. Google will generate a 16-character password (e.g. abcd efgh ijkl mnop) — copy it.
  5. Add to your .env:
    GMAIL_USER=email@gmail.com
    GMAIL_APP_PASSWORD=xxxx

Note: After posting, emails are marked as read in Gmail to avoid duplicate posts.

Step 7: Run as a background service (daemon)

nohup (simple — Linux/macOS)

# Start in background
nohup ./uit-bot > bot.log 2>&1 &

# View logs
tail -f bot.log

# Stop
pkill uit-bot

If nohup ... & stops when you close the terminal, add disown:

nohup ./uit-bot > bot.log 2>&1 & disown

tmux (recommended — keeps process running after terminal closes)

brew install tmux
tmux new -s bot
./uit-bot
# Detach while leaving the bot running: Ctrl+B then D
# Reattach: tmux attach -t bot
# Stop inside session: Ctrl+C
# List sessions: tmux ls

systemd (Linux — auto-start on reboot)

Create /etc/systemd/system/uit-bot.service:

[Unit]
Description=UIT Announcement Bot
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/auto-get-mails
ExecStart=/home/ubuntu/auto-get-mails/uit-bot
Environment="TELEGRAM_BOT_TOKEN=your_token_here"
Environment="TELEGRAM_CHAT_ID=your_chat_id_here"
Environment="POLL_INTERVAL=5m"
Environment="STORE_PATH=data/seen.json"
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable uit-bot
sudo systemctl start uit-bot
sudo systemctl status uit-bot

launchd (macOS — equivalent to systemd)

Create ~/Library/LaunchAgents/com.uit-bot.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.uit-bot</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/your_user/auto-get-mails/uit-bot</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/Users/your_user/auto-get-mails</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Users/your_user/auto-get-mails/bot.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/your_user/auto-get-mails/bot.log</string>
</dict>
</plist>
# Load and run
launchctl load ~/Library/LaunchAgents/com.uit-bot.plist

# Unload
launchctl unload ~/Library/LaunchAgents/com.uit-bot.plist

# Check status
launchctl list | grep uit-bot

Configuration (Environment variables)

Variable Required Default Description
TELEGRAM_BOT_TOKEN ✅ — Bot token from BotFather
TELEGRAM_CHAT_ID ✅ — Target group or chat ID
POLL_INTERVAL ❌ 5m Interval between checks (e.g. 10m, 30s, 1h)
STORE_PATH ❌ data/seen.json Path to store IDs of already-sent items
GMAIL_USER ❌ — Gmail address (to enable email reading)
GMAIL_APP_PASSWORD ❌ — Gmail App Password for IMAP

Or use a .env file:

cp .env.example .env
# Edit .env with your token and chat ID
source .env && ./uit-bot

Project structure

auto-get-mails/
├── cmd/bot/main.go           # Entry point, main loop
├── internal/
│   ├── scraper/scraper.go    # Fetch & parse HTML
│   ├── email/reader.go       # Read Gmail via IMAP
│   ├── notifier/telegram.go  # Send Telegram messages
│   └── store/store.go        # Manage sent-state storage
├── data/
│   └── seen.json             # (auto-created) IDs of announcements already sent
├── .env.example              # Example environment file
├── go.mod / go.sum
├── uit-bot                   # Built binary
└── README.md

Troubleshooting

Bot does not send messages:

  • Verify the token is correct: curl -s "https://api.telegram.org/botTOKEN/getMe"
  • Ensure the bot has been added to the group.
  • Confirm the chat ID is correct (pay attention to a leading - for groups).

Cannot fetch the announcements page:

  • Check internet access: curl -s https://sdh.uit.edu.vn/thong-bao-chung | head -5
  • The page HTML structure may have changed — update internal/scraper/scraper.go accordingly.

Emails are not posted:

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages