English | ็ฎไฝไธญๆ | ็น้ซไธญๆ | ๆฅๆฌ่ช | Espaรฑol | Franรงais | ะ ัััะบะธะน | Deutsch
Welcome to Telegram GPT Worker! ๐ This is an efficient Telegram bot developed in TypeScript. It supports multiple languages and AI models, deployed on Cloudflare Workers, providing users with a fast and scalable service experience.
Before getting started, please ensure you have the following:
For detailed deployment steps, please refer to the tutorial below.
/start
- Start the bot/language
- Switch language/switchmodel
- Switch AI model/new
- Start a new conversation/history
- Get conversation history summary/help
- Get help information/img
- Generate image (DALL-E)/flux
- Generate image (Cloudflare Flux)/GPT-Telegram-Worker
โ
โโโ /src
โ โโโ /api
โ โ โโโ azure.ts # Handle Azure API interactions
โ โ โโโ claude.ts # Handle Claude API interactions
โ โ โโโ flux-cf.ts # Handle Cloudflare AI drawing interface
โ โ โโโ gemini.ts # Handle Google Gemini API interactions
โ โ โโโ groq.ts # Handle Groq API interactions
โ โ โโโ image_generation.ts # Handle DALLยทE drawing interface
โ โ โโโ model_api_interface.ts # Common interface defining model API standard structure
โ โ โโโ openai_api.ts # Handle OpenAI API interactions
โ โ โโโ openai_compatible.ts # Handles OpenAI compatible API interactions
โ โ โโโ telegram.ts # Handle Telegram bot logic
โ โโโ /config # Configuration files
โ โ โโโ commands.ts # Telegram bot commands
โ โโโ /utils
โ โ โโโ helpers.ts # Utility functions and tools
โ โ โโโ i18n.ts # Multilingual functions
โ โ โโโ redis.ts # Upstash Redis functions
โ โ โโโ image_analyze.ts # Image upload functions
โ โโโ index.ts # Entry file, handling requests and responses
โ โโโ env.ts # Configure environment variables
โโโ /types # Type definition files
โ โโโ telegram.d.ts # Type definitions for Telegram API
โโโ wrangler.toml # Cloudflare Worker configuration file
โโโ tsconfig.json # TypeScript configuration file
โโโ package.json # Project dependency file
โโโ README.md # Project documentation
Install Wrangler CLI:
npm install -g @cloudflare/wrangler
Log in to your Cloudflare account:
wrangler login
Create a new Workers project:
wrangler init telegram-bot
Copy the dist/index.js
file into your project.
Edit the wrangler.toml
file to configure your project:
name = "telegram-bot"
type = "javascript"
account_id = "your_account_id"
workers_dev = true
Deploy to Cloudflare Workers:
wrangler publish
dist/index.js
into the editor, then save the file.Use the Telegram Bot API to set up the Webhook. URL example:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook?url=https://your-worker.your-subdomain.workers.dev/webhook
https://api.telegram.org/bot123456789:abcdefghijklmn/setWebhook?url=https://gpt-telegram-worker.abcdefg.workers.dev/webhook
Clone the project:
git clone https://github.com/snakeying/telegram-bot.git
Install dependencies:
npm install
Set up environment variables.
Compile TypeScript:
npm run build
Start the bot:
npm start
Variable Name | Description | Default Value | Example |
---|---|---|---|
OPENAI_API_KEY | OpenAI API key | - | sk-abcdefghijklmnopqrstuvwxyz123456 |
OPENAI_BASE_URL | OpenAI API base URL | https://api.openai.com/v1 | https://your-custom-endpoint.com/v1 |
OPENAI_MODELS | List of available OpenAI models | - | gpt-3.5-turbo,gpt-4 |
TELEGRAM_BOT_TOKEN | Telegram bot token | - | 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 |
WHITELISTED_USERS | List of user IDs allowed to use the bot | - | 12345678,87654321 |
SYSTEM_INIT_MESSAGE | System initialization message | You are a helpful assistant. | You are a helpful assistant. |
SYSTEM_INIT_MESSAGE_ROLE | System initialization message role | system | system |
DEFAULT_MODEL | Default AI model to use | - | gpt-3.5-turbo |
UPSTASH_REDIS_REST_URL | Upstash Redis REST URL | - | https://your-redis-url.upstash.io |
UPSTASH_REDIS_REST_TOKEN | Upstash Redis REST token | - | your-redis-token |
DALL_E_MODEL | DALL-E model version | dall-e-3 | dall-e-3 |
CLOUDFLARE_API_TOKEN | Cloudflare API token | - | your-cloudflare-api-token |
CLOUDFLARE_ACCOUNT_ID | Cloudflare account ID | - | your-cloudflare-account-id |
FLUX_STEPS | Number of Flux generation steps | 4 | 4-8, maximum steps is 8 |
PROMPT_OPTIMIZATION | Enable prompt optimization | false | true |
EXTERNAL_API_BASE | External API base URL | - | https://external-api.com |
EXTERNAL_MODEL | External model name | - | external-model-name |
EXTERNAL_API_KEY | External API key | - | external-api-key |
GOOGLE_MODEL_KEY | Google AI model API key | - | your-google-api-key |
GOOGLE_MODEL_BASEURL | Google AI model API base URL | https://generativelanguage.googleapis.com/v1beta | https://your-custom-google-endpoint.com |
GOOGLE_MODELS | List of available Google AI models | - | gemini-pro,gemini-pro-vision |
GROQ_API_KEY | Groq API key | - | your-groq-api-key |
ANTHROPIC_API_KEY | Anthropic API key | - | your-anthropic-api-key |
ANTHROPIC_BASE_URL | Anthropic API base URL | https://api.anthropic.com | https://your-custom-anthropic-endpoint.com |
OPENAI_COMPATIBLE_KEY | OpenAI Compatible API Key | - | sk-abcdefghijklmnopqrstuvwxyz123456 |
OPENAI_COMPATIBLE_URL | OpenAI Compatible API Base URL | - | https://your-custom-endpoint.com/v1 |
Note: Some variables need to be manually configured and have no default values.
Allows users to upload images and receive AI analysis results. Here's how to use it:
Note: Make sure the AI model you're using supports image analysis. If the current model doesn't support it, the bot will prompt you to switch to a multimodal-supporting model.
When the PROMPT_OPTIMIZATION environment variable is set to true, the Flux image generation feature uses an external API to optimize prompts. This feature works through the following steps:
This feature can help generate more precise images that better align with Flux model characteristics. To use this feature, make sure all relevant environment variables are correctly configured.
This project is licensed under the MIT License.
Copyright (c) 2024 [snakeying]