This Telegram bot sends the day's top news stories in a few messages.
Copy env file and fill it with your credentials:
cp .env.sample .env
nano .env
Run bot to save chat IDs of interacting users:
go run cmd/bot/bot.go
Periodically send news to these chats by run:
go run cmd/send_news/send_news.go
Edit server access details in inventory file ansible/inventories/staging.yml
and API secrets in ansible/group_vars/staging/bot.yml
.
After that you can run from project directory:
make deploy
To deploy a Golang program to a VDS (Virtual Dedicated Server), you can follow these steps:
- Build your golang program - Before deploying your program, you need to build it. To do that, first, make sure you have golang installed on your local machine. Then, navigate to your project directory and run the following command:
go build -o bot cmd/bot/bot.go
go build -o send_news cmd/send_news/send_news.go
This will create an executable files in your project directory.
- Transfer executable files to your VDS. You can do this using an FTP client or SCP. SCP example:
scp bot deploy@127.0.0.1:~/main-news-telegram-bot
scp send_news deploy@127.0.0.1:~/main-news-telegram-bot
- Create a new systemd file:
sudo nano /etc/systemd/system/main-news-telegram-bot.service
Here is an example systemd service file for a Go program:
[Unit]
Description=Main News Telegram bot
After=network.target
[Service]
User=deploy
WorkingDirectory=/home/deploy/main-news-telegram-bot
ExecStart=/home/deploy/main-news-telegram-bot/bot
Environment=TELEGRAM_BOT_TOKEN=secret
Environment=RAPID_API_KEY=secret
Environment=TELEGRAM_CHANNELS=channel1,channel2
Environment=POSTS_COUNT=50
Restart=on-failure
RestartSec=1
[Install]
WantedBy=multi-user.target
# See these pages for lots of options:
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
Make sure to replace the environment variables with your own values.
- Save the file and reload systemd:
sudo systemctl daemon-reload
- Start the service:
sudo systemctl start main-news-telegram-bot
- Verify that the service is running:
sudo systemctl status main-news-telegram-bot
- If everything is working correctly, enable the service to start at boot:
sudo systemctl enable main-news-telegram-bot
That's it! Your Golang program should now be running on your VDS and will automatically start up whenever your server reboots.
-
Add .env file with your credentials. For an example, see the .env.sample file.
-
Add crone task to run send_news file every hour. To do that you can follow these steps:
- Edit the crontab file using the command:
crontab -e
- In the crontab file, add a new line with the following syntax:
0 * * * * cd /home/deploy/main-news-telegram-bot && ./send_news > cron_output.txt 2>&1
- Save and exit the crontab file.
- Edit the crontab file using the command:
The cron task is now set up to execute the /home/deploy/main-news-telegram-bot/send_news
file every hour.