A Telegram bot built with Go (Golang) to assist developers with daily tasks. This project is part of the GoBot Challenge - a 30-day learning journey to master Go programming.
- β Basic Commands: Start, help, ping functionality
- β Entertainment: Random jokes and motivational quotes
- β Utility: Current time, bot information
- β JSON Configuration: Easily customizable messages and content
- β Polling & Webhook: Support for both development and production modes
- β Logging: Comprehensive activity tracking
- β Error Handling: Robust error management
Command | Description |
---|---|
/start |
Initialize the bot and get welcome message |
/help |
Display all available commands |
/ping |
Check if bot is online |
/hazil |
Get a random programming joke |
/iqtibos |
Get a motivational programming quote |
/haqida |
Get information about the bot |
/vaqt |
Get current timestamp |
Before you begin, ensure you have the following installed:
- Go 1.21+: Download Go
- Git: Download Git
- ngrok: Download ngrok (for webhook setup)
- Postman: Download Postman (for API testing)
- Telegram Account: For creating and testing the bot
git clone https://github.com/yourusername/yordamchi-dev-bot.git
cd yordamchi-dev-bot
go mod init yordamchi-dev-bot
go mod tidy
- Open Telegram and find @BotFather
- Send
/newbot
command - Choose a name:
Yordamchi Dev Bot
- Choose a username:
your_bot_username_bot
- Copy the bot token (format:
123456789:ABCdef-YourTokenHere
)
# Copy the example file
cp .env.example .env
# .env
BOT_TOKEN=your_bot_token_from_botfather
BOT_MODE=polling
PORT=8090
DB_TYPE=sqlite
DEBUG=true
Important: Replace your_bot_token_from_botfather
with your actual bot token!
Ensure these files exist in your project root:
config.json
- Bot configuration and messages.env
- Environment variablesmain.go
- Application entry pointbot.go
- Bot logic and handlersconfig.go
- Configuration management
For webhook setup, you'll need to expose your local server to the internet.
# Download ngrok from https://ngrok.com/download
# Or install via package manager:
# macOS
brew install ngrok
# Windows (Chocolatey)
choco install ngrok
# Linux
sudo snap install ngrok
# Terminal 1: Start your bot in webhook mode
export BOT_TOKEN="your_actual_bot_token"
export BOT_MODE="webhook"
go run .
# Terminal 2: Expose port 8090
ngrok http 8090
ngrok output example:
Forwarding https://abc123.ngrok.io -> http://localhost:8090
- Open Postman
- Create a new POST request
- URL:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook
- Headers:
Content-Type
:application/json
- Body (raw JSON):
{
"url": "https://abc123.ngrok.io/webhook"
}
- Click Send
Expected response:
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook" \
-H "Content-Type: application/json" \
-d '{"url": "https://abc123.ngrok.io/webhook"}'
Using Postman:
- GET request to:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfo
Using cURL:
curl "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfo"
Expected response:
{
"ok": true,
"result": {
"url": "https://abc123.ngrok.io/webhook",
"has_custom_certificate": false,
"pending_update_count": 0
}
}
- Search for your bot username in Telegram
- Start a conversation
Send these messages to test functionality:
/start
Expected: Welcome message with instructions
/help
Expected: List of all available commands
/ping
Expected: "π Pong! Bot ishlayapti β
"
/hazil
Expected: Random programming joke
/iqtibos
Expected: Motivational programming quote
/haqida
Expected: Information about the bot
/vaqt
Expected: Current timestamp
In your terminal running the bot, you should see:
π€ YourName (@yourusername): /start
π€ YourName (@yourusername): /help
π€ YourName (@yourusername): /ping
yordamchi-dev-bot/
βββ main.go # Application entry point
βββ bot.go # Bot logic and message handling
βββ config.go # Configuration management
βββ config.json # Bot settings and messages
βββ .env.example # Environment variables template
βββ .env # Your environment variables (create this)
βββ go.mod # Go module definition
βββ go.sum # Go module dependencies
βββ README.md # This file
βββ .gitignore # Git ignore rules
Solution:
# Check if .env file exists and contains BOT_TOKEN
cat .env
# Or set manually:
export BOT_TOKEN="your_actual_bot_token"
Solution:
# Verify config.json exists and is valid
ls -la config.json
cat config.json | jq '.' # Validates JSON format
Possible causes:
- Wrong bot token: Verify token from @BotFather
- Bot not running: Check terminal for "Bot polling started" message
- Webhook conflicts: Delete webhook if using polling mode
Solutions:
# Test bot token validity
curl "https://api.telegram.org/bot<YOUR_TOKEN>/getMe"
# Delete webhook (if using polling)
curl -X POST "https://api.telegram.org/bot<YOUR_TOKEN>/deleteWebhook"
Cause: Another bot instance is running or webhook is set while using polling.
Solution:
# Delete webhook
curl -X POST "https://api.telegram.org/bot<YOUR_TOKEN>/deleteWebhook"
# Kill other bot processes
pkill -f "go run"
Solutions:
# Check if ngrok is running
ps aux | grep ngrok
# Restart ngrok
ngrok http 8090
# Verify webhook URL matches ngrok URL
curl "https://api.telegram.org/bot<YOUR_TOKEN>/getWebhookInfo"
Enable debug logging:
export DEBUG=true
go run .
# Get bot information
curl "https://api.telegram.org/bot<TOKEN>/getMe"
# Get webhook info
curl "https://api.telegram.org/bot<TOKEN>/getWebhookInfo"
# Set webhook
curl -X POST "https://api.telegram.org/bot<TOKEN>/setWebhook" \
-H "Content-Type: application/json" \
-d '{"url": "https://your-ngrok-url.ngrok.io/webhook"}'
# Delete webhook
curl -X POST "https://api.telegram.org/bot<TOKEN>/deleteWebhook"
Import this collection to Postman for easy bot management:
Collection variables:
bot_token
: Your bot tokenwebhook_url
: Your ngrok URL + /webhook
Requests:
- GET Bot Info:
{{base_url}}/bot{{bot_token}}/getMe
- GET Webhook Info:
{{base_url}}/bot{{bot_token}}/getWebhookInfo
- SET Webhook:
{{base_url}}/bot{{bot_token}}/setWebhook
- DELETE Webhook:
{{base_url}}/bot{{bot_token}}/deleteWebhook
Where base_url
= https://api.telegram.org
This bot is designed for progressive enhancement:
- π GitHub API Integration: Repository statistics and issue search
- π Stack Overflow Search: Programming Q&A lookup
- π¨ Code Formatter: Go code formatting service
- π Documentation Lookup: Quick access to Go docs
- π Concurrent Processing: Goroutines and channels
- πΎ Database Integration: PostgreSQL user management
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
By completing this project, you will master:
- β Go Fundamentals: Packages, structs, functions, error handling
- β HTTP & JSON: REST API integration and data parsing
- β Concurrency: Goroutines for polling and webhook handling
- β Configuration Management: JSON and environment variables
- β Testing: Unit tests and integration testing
- β Deployment: Docker containerization and production setup
If you encounter any issues:
- Check the Troubleshooting section
- Review your
.env
andconfig.json
files - Verify your bot token with @BotFather
- Check the Issues page
- Create a new issue with detailed error logs
Happy Coding! π
Built with β€οΈ using Go β’ Part of the 30-Day GoBot Challenge