Skip to content

Commit decbfc6

Browse files
committed
Docker build files & setup
1 parent 5529363 commit decbfc6

File tree

2 files changed

+160
-19
lines changed

2 files changed

+160
-19
lines changed

README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
---
4545

46-
# Installing
46+
# Installing (No Docker)
4747

4848
1. **Clone the repository with:**
4949

@@ -91,6 +91,126 @@
9191

9292
---
9393

94+
## **Dockerized Install Instructions**
95+
96+
### **Prerequisites**
97+
1. **Docker** must be installed on your machine.
98+
- If not installed, you can download and install it from [Docker's official site](https://www.docker.com/get-started).
99+
100+
2. **Telegram Bot API Key** and **OpenAI API Key**:
101+
- You will need a valid Telegram Bot API key. You can get one by creating a bot with [BotFather](https://core.telegram.org/bots#botfather).
102+
- You will also need an OpenAI API key. If you don't have one, you can generate it from the [OpenAI API page](https://beta.openai.com/signup/).
103+
104+
### **Step 1: Clone the Repository**
105+
106+
First, clone the repository from GitHub:
107+
108+
```bash
109+
git clone https://github.com/FlyingFathead/TelegramBot-OpenAI-API.git
110+
cd TelegramBot-OpenAI-API
111+
```
112+
113+
### **Step 2: Run the Setup Script**
114+
115+
This project includes a setup script that will guide you through entering your API keys and generating a `.env` file.
116+
117+
Run the script:
118+
119+
```bash
120+
./docker_setup.sh
121+
```
122+
123+
Follow the instructions provided by the script. It will ask for your OpenAI API key and Telegram Bot API key, validate them, and create a `.env` file with your credentials.
124+
125+
### **Step 3: Build the Docker Image**
126+
127+
Once your `.env` file has been created, you need to build the Docker image.
128+
129+
Run the following command to build the Docker image:
130+
131+
```bash
132+
sudo docker build -t telegrambot-openai-api .
133+
```
134+
135+
This will build the image locally based on the `Dockerfile` in the repository.
136+
137+
### **Step 4: Run the Docker Container**
138+
139+
After the image is successfully built, you can start the bot in a Docker container.
140+
141+
Run the container with the following command:
142+
143+
```bash
144+
sudo docker run --env-file .env -d telegrambot-openai-api
145+
```
146+
147+
- The `-d` flag runs the container in detached mode (in the background).
148+
- The `--env-file .env` flag injects your API keys into the container.
149+
150+
### **Step 5: Check the Running Container**
151+
152+
You can check if the container is running by using:
153+
154+
```bash
155+
sudo docker ps
156+
```
157+
158+
This will list all running containers. If your bot is running correctly, it should appear in the list.
159+
160+
### **Step 6: Stopping the Container**
161+
162+
If you need to stop the bot, you can do so by running:
163+
164+
```bash
165+
sudo docker stop <container_id>
166+
```
167+
168+
Replace `<container_id>` with the actual container ID, which you can obtain from the `docker ps` output.
169+
170+
### **Additional Steps (Optional)**
171+
172+
- **Logs**: If you need to view the bot’s logs to troubleshoot any issues, you can use:
173+
174+
```bash
175+
sudo docker logs <container_id>
176+
```
177+
178+
- **Restart the Container**: If you stop the container and want to start it again, you can either run the `docker run` command again or restart the existing container with:
179+
180+
```bash
181+
sudo docker start <container_id>
182+
```
183+
184+
### **Updating the Bot**
185+
186+
If the repository receives updates and you want to apply them, follow these steps:
187+
188+
1. Pull the latest changes from GitHub:
189+
```bash
190+
git pull origin main
191+
```
192+
193+
2. Rebuild the Docker image:
194+
```bash
195+
sudo docker build -t telegrambot-openai-api .
196+
```
197+
198+
3. Stop the currently running container:
199+
```bash
200+
sudo docker stop <container_id>
201+
```
202+
203+
4. Start a new container using the updated image:
204+
```bash
205+
sudo docker run --env-file .env -d telegrambot-openai-api
206+
```
207+
208+
You should now have the TelegramBot-OpenAI-API running in a Docker container, fully connected to both Telegram and OpenAI. Enjoy your bot!
209+
210+
If you run into any issues, consult the logs or reach out on the repository's [Issues page](https://github.com/FlyingFathead/TelegramBot-OpenAI-API/issues).
211+
212+
---
213+
94214
# Updating
95215

96216
- Use the `configmerger.py` to update old configuration files into a newer version's `config.ini`. You can do this by saving a copy of your existing config to i.e. a file named `myconfig.txt` and including in it the lines you want to keep for the newer version.
@@ -106,6 +226,7 @@
106226
---
107227

108228
# Changelog
229+
- v0.7502 - added `docker_setup.sh` for easier Docker-based deployment
109230
- v0.7501 - `Dockerfile` and better error catching when receiving `401 Unauthorized`
110231
- v0.75 **Major refactoring** _(5. Oct 2024)_ 👀💦🌀
111232
- entire project has been tidied up and a lot of bugs fixed while at it

docker_setup.sh

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ echo "Welcome to the TelegramBot-OpenAI-API setup."
1111
echo "Source code: https://github.com/FlyingFathead/TelegramBot-OpenAI-API/"
1212
echo
1313

14-
# Function to check for empty or invalid inputs
14+
# Function to check for empty or invalid inputs for required keys
1515
validate_input() {
1616
if [[ -z "$1" || ${#1} -lt 10 ]]; then
1717
echo "Error: Input cannot be blank or too short (must be at least 10 characters). Please try again."
@@ -20,33 +20,38 @@ validate_input() {
2020
return 0
2121
}
2222

23-
# Prompt for OpenAI API Key with validation
23+
# Prompt for required API keys (OpenAI and Telegram)
2424
while true; do
25-
read -p "Please enter your OpenAI API key: " OPENAI_API_KEY
25+
read -p "Please enter your OpenAI API key (required): " OPENAI_API_KEY
2626
validate_input "$OPENAI_API_KEY" && break
2727
done
2828

29-
# Prompt for Telegram Bot API Key with validation
3029
while true; do
31-
read -p "Please enter your Telegram Bot API key: " TELEGRAM_BOT_API_KEY
30+
read -p "Please enter your Telegram Bot API key (required): " TELEGRAM_BOT_API_KEY
3231
validate_input "$TELEGRAM_BOT_API_KEY" && break
3332
done
3433

35-
# Create a .env file with the collected keys
34+
# Prompt for optional API keys (user can leave them blank)
35+
echo "Below are optional keys for the bot's supported API functionalities that you can add in, or just press ENTER to leave them blank."
36+
read -p "Please enter your Perplexity API key (optional): " PERPLEXITY_API_KEY
37+
read -p "Please enter your OpenWeatherMap API key (optional): " OPENWEATHERMAP_API_KEY
38+
read -p "Please enter your WeatherAPI key (optional): " WEATHERAPI_KEY
39+
read -p "Please enter your MapTiler API key (optional): " MAPTILER_API_KEY
40+
read -p "Please enter your Openrouteservice API key (optional): " OPENROUTESERVICE_API_KEY
41+
42+
# Create a .env file with the required and optional keys
3643
echo "Generating .env file..."
3744
cat <<EOL > .env
3845
OPENAI_API_KEY=$OPENAI_API_KEY
3946
TELEGRAM_BOT_API_KEY=$TELEGRAM_BOT_API_KEY
47+
OPENWEATHERMAP_API_KEY=$OPENWEATHERMAP_API_KEY
48+
WEATHERAPI_KEY=$WEATHERAPI_KEY
49+
MAPTILER_API_KEY=$MAPTILER_API_KEY
50+
OPENROUTESERVICE_API_KEY=$OPENROUTESERVICE_API_KEY
51+
PERPLEXITY_API_KEY=$PERPLEXITY_API_KEY
4052
# Additional variables can be added here
4153
EOL
4254

43-
# Optionally, check for Docker Compose if it's needed later
44-
# if ! [ -x "$(command -v docker-compose)" ]; then
45-
# echo 'Warning: Docker Compose is not installed. Consider installing it if needed.' >&2
46-
# fi
47-
48-
49-
# Inform the user the setup is complete
5055
echo "Environment variables saved to .env."
5156

5257
# Instructions for the next steps
@@ -58,13 +63,28 @@ echo
5863
echo "2. After building the image, start the bot container using:"
5964
echo " sudo docker run --env-file .env -d telegrambot-openai-api"
6065
echo
61-
echo " This will start the bot in detached mode (-d) using the environment variables from the .env file."
62-
echo
63-
echo "3. Optionally, check if the container is running using:"
66+
echo "3. Check the container status with:"
6467
echo " sudo docker ps"
6568
echo
66-
echo "4. To stop the container, you can run:"
69+
echo "4. Stop the container with:"
6770
echo " sudo docker stop <container_id>"
6871
echo
69-
echo "You're all set! The bot should now be running and connected to Telegram and OpenAI."
72+
echo "You're all set!"
73+
74+
function build_and_run() {
75+
# Build Docker image
76+
sudo docker build -t telegrambot-openai-api .
77+
if [[ $? -ne 0 ]]; then
78+
echo "Error: Docker image build failed."
79+
exit 1
80+
fi
81+
82+
# Run Docker container
83+
sudo docker run --env-file .env -d telegrambot-openai-api
84+
if [[ $? -ne 0 ]]; then
85+
echo "Error: Failed to run the Docker container."
86+
exit 1
87+
fi
88+
}
7089

90+
# build_and_run

0 commit comments

Comments
 (0)