A powerful Telegram bot built with Fastify that leverages Large Language Models (LLM) to provide intelligent responses and tool-enabled capabilities. This bot uses Google's Gemini model for natural language processing and includes database integration for persistent storage.
- 🤖 Telegram Bot Integration
- 🧠 LLM-powered responses using Google Gemini
- 🛠️ Tool-enabled capabilities
- 📊 Database integration with Prisma
- 🔄 Real-time message processing
- ⚡ Fast and efficient Fastify backend
Before you begin, ensure you have the following:
- Node.js (v18 or higher)
- pnpm (recommended) or npm
- A PostgreSQL database (or compatible database)
- Telegram Bot Token
- Google Gemini API Key
- Clone the repository:
git clone <your-repository-url>
cd fastify-telegram-bot- Install dependencies:
pnpm install- Set up your environment variables:
cp .env.example .env- Update the
.envfile with your credentials:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
DATABASE_URL=your_database_url
GEMINI_API_KEY=your_gemini_api_key
SMITHERY_API_KEY=your_smithery_api_key
PORT=3000- Open Telegram and search for @BotFather
- Start a chat and use the
/newbotcommand - Follow the instructions to create your bot
- Copy the API token provided by BotFather
- Add the token to your
.envfile asTELEGRAM_BOT_TOKEN
- Visit the Google AI Studio
- Sign in with your Google account
- Create a new API key
- Copy the key and add it to your
.envfile asGEMINI_API_KEY
- Visit the Smithery AI website
- Create an account or log in
- Navigate to your API keys
- Generate a new API key
- Add the key to your
.envfile asSMITHERY_API_KEY
If you don't have a database ready, you can use one of these options:
-
Neon (Recommended for development)
- Visit Neon
- Create a free account
- Create a new project
- Copy the connection string to your
DATABASE_URL
-
Supabase
- Visit Supabase
- Create a free account
- Create a new project
- Get the PostgreSQL connection string from the project settings
- Generate Prisma client:
pnpm db:generate- Push the database schema:
pnpm db:push- Start the development server:
pnpm devpnpm dev- Start the development serverpnpm start- Start the production serverpnpm db:studio- Open Prisma Studio for database managementpnpm db:push- Push database schema changespnpm db:generate- Generate Prisma clientpnpm format- Format code using Prettierpnpm lint- Run ESLint
├── src/ # Source code
├── prisma/ # Database schema and migrations
├── .env.example # Example environment variables
- `pnpm format` - Format code using Prettier and scripts
The bot supports MCP (Model Context Protocol) services that extend its capabilities. These services provide additional tools to the AI agent, such as weather forecasting, web search, and more.
To add new MCP services:
- Edit the
src/constants/mcp.jsonfile:
{
"mcpServers": {
"your-new-service-name": {
"command": "npx",
"args": [
"-y",
"@smithery/cli@latest",
"run",
"@namespace/service-name",
"--key",
"${SMITHERY_API_KEY}"
]
}
}
}-
If your service requires an API key, add it to your
.envfile and reference it in the args array using${ENV_VAR_NAME}syntax. -
The service will be automatically available to the agent on next startup.
Here are some popular MCP services you can add:
@turkyden/weather: Provides weather forecast capabilities@smithery-ai/server-sequential-thinking: Enables step-by-step reasoning@nickclyde/duckduckgo-mcp-server: Adds web search capabilities
Visit Smithery AI to discover more MCP services.
The bot's personality and capabilities are defined by its system prompt. To customize this:
- Edit the
src/constants/agent-prompts.jsonfile:
{
"telegram": {
"system_prompt": [
"You are a helpful telegram assistant.",
"Limit your message responses to 4000 characters.",
"Format your replies in plain text.",
"Add your custom instructions here."
],
"description": "System prompt for the Telegram bot agent"
}
}-
Each line in the
system_promptarray will be joined with newlines to form the complete prompt. -
The changes will take effect the next time the bot starts.
When customizing the system prompt, consider these best practices:
- Be specific about the bot's personality and tone
- Define limitations clearly (e.g., message length, content restrictions)
- Specify formatting preferences for different types of responses
- Prioritize tools if you want certain capabilities to be used more frequently
- Keep instructions concise as very long prompts can reduce effectiveness
Example system prompts for different use cases:
// Weather Assistant
{
"telegram": {
"system_prompt": [
"You are a friendly weather assistant specialized in providing accurate weather forecasts.",
"Always use the weather tool when users ask about weather conditions.",
"Limit your message responses to 4000 characters.",
"Format temperature in both Celsius and Fahrenheit.",
"For weather forecasts, always include: temperature, conditions, and precipitation probability."
]
}
}
// Research Assistant
{
"telegram": {
"system_prompt": [
"You are a research assistant that helps users find information online.",
"Always use the web search tool for factual queries and current events.",
"Limit your message responses to 4000 characters.",
"When providing information, cite your sources.",
"Format complex information in easy-to-read bullet points."
]
}
}Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the ISC License.
When customizing the system prompt, consider these best practices:
- Be specific about the bot's personality and tone
- Define limitations clearly (e.g., message length, content restrictions)
- Specify formatting preferences for different types of responses
- Prioritize tools if you want certain capabilities to be used more frequently
- Keep instructions concise as very long prompts can reduce effectiveness