A ChatGPT Bot for Telegram.
- Chat with GPTs via Telegram UI.
- Include chat message history.
- Customisable GPT system prompts.
- Message moderation.
- Specify allowed chat ids.
- Configurable serverless deployment.
- Image recognition with GPT-4 Vision.
- Image generation with DALL-E 3.
- Audio recognition with Whisper.
- Audio generation.
This guide will walk you through deploying TelegramGPT on AWS. Ensure you have the AWS CLI and Terraform CLI setup, and Rust installed on your machine.
-
Clone the repository:
git clone https://github.com/rossheat/telegramgpt.git
-
Navigate to the project directory:
cd telegramgpt/ -
Open the project in your preferred editor:
code . # or vim .
-
Prepare configuration file: Rename
infra/terraform.tfvars.exampletoinfra/terraform.tfvars.
-
Create a new bot on Telegram via @BotFather. Follow the instructions to receive your bot token.
-
Set your bot token: Replace the example
telegram_bot_tokenininfra/terraform.tfvarswith your new bot token. -
Set your bot name: Replace the example
bot_namewith your new bot name. -
Adjust bot settings:
- Access your bot settings in BotFather.
- Navigate to ‘Bot Settings’ > ‘Group Privacy’ > ‘Turn off’. This allows your bot to access messages in group chats.
-
Set a webhook secret token: Generate a token using the command:
openssl rand -hex 32
Assign this token to
telegram_webhook_secret_tokenininfra/terraform.tfvars.
-
Generate an OpenAI API key at OpenAI Platform.
-
Set your OpenAI API key: Replace the example
openai_api_keyininfra/terraform.tfvarswith your new key. -
Customise the system prompt: The
system_promptguides how the GPT model will interact in the chat. When setting this prompt, follow these rules:- The prompt should be clear and specific to direct the model's responses.
- It must instruct the model to never output timestamps.
- Responses should be in the specified JSON format:
{"has_response" : <boolean>, "message" : <string>}. - If the model has no response, set
has_responsetofalseandmessageto an empty string.
Example for Group Chat:
"You are GPT, a helpful assistant in a Telegram group chat. Respond only to messages directed at you. Do not respond to general conversation. Never output timestamps. Respond in the following JSON format: {\"has_response\" : , \"message\" : }"
Update
system_promptininfra/terraform.tfvarsas per these guidelines.
- Create a MongoDB deployment at MongoDB Cloud.
- Configure MongoDB:
- Create a new project and a free M0 deployment.
- Set up a user for database access.
- Allow access from all IPs (
0.0.0.0/0) for Lambda function connectivity.
- Retrieve and set the connection string: Update
mongodb_uriininfra/terraform.tfvarswith your MongoDB connection string.
- Start a chat with your bot in Telegram and send a couple of messages. You will not get a response.
- Retrieve your chat ID:
Run
./scripts/get_private_chat_id.shfrom the project root. Locate the chat ID in the JSON output, and add it toallowed_chat_idsininfra/terraform.tfvars.
-
Perform local CI checks:
./scripts/ci.sh
Ensure that this script passes without errors before contributing.
-
Start the Rust AWS Lambda development server:
./scripts/watch.sh
-
Send a test webhook payload:
./scripts/local_lambda_test.sh
-
Initiate deployment:
./scripts/deploy.sh
Use the
--delete-messagesoption to delete any existing messages in the MongoDB messages collection. -
Test your deployment:
./scripts/prod_lambda_test.sh
After deployment, interact with your bot through the Telegram UI. Responses will come from your chosen OpenAI GPT model.
Lambda function logs are available in AWS CloudWatch.
To destroy the AWS infrastructure created by this project:
./scripts/destroy_infra.shWe welcome contributions to the TelegramGPT project. Please see CONTRIBUTING.md for guidelines.
To ensure a welcoming and positive environment for everyone, all contributors and participants are expected to adhere to our Code of Conduct. Please read CODE_OF_CONDUCT.md for details.
This project is open-source and available under the MIT License. By contributing, you agree that your contributions will be licensed under this license.
Join our community to discuss features, ideas, and get help:
- Telegram Group: Join our Telegram group here for discussions and support.
- GitHub Issues: For bug reports and feature requests, use the GitHub issues page.

