This project is a Telegram chatbot that integrates OpenAI's GPT models, allowing users to interact with the bot and ask questions. The bot responds using the GPT model and provides relevant answers to the user's queries. Additionally, a basic Flask web server is included to demonstrate the project running as a background service.
- Telegram bot interface using the aiogram library.
- Integration with OpenAI's GPT API to provide intelligent responses.
- Basic Flask server running in a background thread.
- Easy-to-deploy project with environment variable support.
- Python: Make sure Python 3.8 or higher is installed on your system.
- Telegram Bot Token: Create a new bot on Telegram via BotFather and get the token.
- OpenAI API Key: Sign up on OpenAI and get your API key.
git clone https://github.com/your-username/telegram-chatbot-gpt.git cd telegram-chatbot-gpt
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
Create a .env file in the root directory and add the following variables:
OPENAI_API_KEY=your-openai-api-key TELEGRAM_BOT_TOKEN=your-telegram-bot-token
Alternatively, you can export these variables in your shell:
export OPENAI_API_KEY=your-openai-api-key export TELEGRAM_BOT_TOKEN=your-telegram-bot-token
You can now run the bot using the following command:
python bot.py
This will start the bot and Flask server in the background.
telegram-chatbot-gpt/
│
├── bot.py # Main Telegram bot code
├── example.py # Flask server running in a thread
├── requirements.txt # List of dependencies├── .env # Environment variables
└── README.md # Project documentation
The main script that contains the code to handle incoming Telegram messages, call the OpenAI GPT API, and reply to the user.
Contains a simple Flask server that runs in the background to simulate additional services running alongside the bot.
- Start the bot: Once the bot is running, you can interact with it on Telegram.
- Send a message: The bot will respond to your queries using OpenAI's GPT model.
- /start: Starts the bot and sends a welcome message.
- Any other text message will be processed by GPT-3.5 Turbo.
- aiogram: Telegram bot framework for Python.
- openai: OpenAI API library.
- Flask: Lightweight web framework to run the example server.
Install dependencies via:
pip install aiogram openai Flask
You can deploy this bot on platforms such as:
- Heroku
- Google Cloud
- AWS
Make sure to properly set up your environment variables in your chosen deployment platform.
If you would like to contribute to this project, feel free to fork the repository and submit a pull request.
If you have any questions, feel free to reach out or create an issue in the repository.
This README provides step-by-step instructions for setting up the bot and the basic functionality it offers. Adjust as needed for your specific project details.