-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker_setup.sh
executable file
·121 lines (107 loc) · 3.79 KB
/
docker_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# horizontal line
function hzline() { printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - ; }
# Check if Docker is installed
if ! [ -x "$(command -v docker)" ]; then
echo 'Error: Docker is not installed. Please install Docker first: https://www.docker.com/get-started/' >&2
exit 1
fi
# Welcome Message
echo "" &&
hzline &&
echo "::: Welcome to the TelegramBot-OpenAI-API setup." &&
echo "::: Source code & repo: https://github.com/FlyingFathead/TelegramBot-OpenAI-API/" &&
hzline &&
echo
# Check if .env file already exists and prompt the user
if [ -f .env ]; then
echo "Warning: A .env file already exists in this directory."
while true; do
read -p "Do you want to overwrite the existing .env file? (y/n): " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Exiting setup without overwriting .env file."; exit 0;;
* ) echo "Please answer yes or no.";;
esac
done
fi
# Function to check for empty or invalid inputs for required keys
validate_input() {
if [[ -z "$1" || ${#1} -lt 10 ]]; then
echo "Error: Input cannot be blank or too short (must be at least 10 characters). Please try again."
return 1
fi
return 0
}
# Prompt for required API keys (OpenAI and Telegram)
while true; do
read -p "Please enter your OpenAI API key (required): " OPENAI_API_KEY
validate_input "$OPENAI_API_KEY" && break
done
while true; do
read -p "Please enter your Telegram Bot API Token (required): " TELEGRAM_BOT_TOKEN
validate_input "$TELEGRAM_BOT_TOKEN" && break
done
# Prompt for optional API keys (user can leave them blank)
hzline &&
echo "::: Below are optional keys for the bot's supported API functions." &&
echo "::: They're not required for basic functionality, but are a great enhancement." &&
echo "::: If you don't have an API key right now, just press ENTER to leave them blank." &&
hzline &&
read -p "Please enter your Perplexity API key (optional): " PERPLEXITY_API_KEY
read -p "Please enter your OpenWeatherMap API key (optional): " OPENWEATHERMAP_API_KEY
read -p "Please enter your WeatherAPI key (optional): " WEATHERAPI_KEY
read -p "Please enter your MapTiler API key (optional): " MAPTILER_API_KEY
read -p "Please enter your Openrouteservice API key (optional): " OPENROUTESERVICE_API_KEY
# Create a .env file with the required and optional keys
hzline &&
echo "Generating .env file..."
cat <<EOL > .env
OPENAI_API_KEY=$OPENAI_API_KEY
TELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN
OPENWEATHERMAP_API_KEY=$OPENWEATHERMAP_API_KEY
WEATHERAPI_KEY=$WEATHERAPI_KEY
MAPTILER_API_KEY=$MAPTILER_API_KEY
OPENROUTESERVICE_API_KEY=$OPENROUTESERVICE_API_KEY
PERPLEXITY_API_KEY=$PERPLEXITY_API_KEY
# Additional variables can be added here
EOL
echo "Environment variables saved to .env." &&
hzline &&
# Instructions for the next steps
echo
echo "Next Steps:"
echo "1. Build the Docker image by running the following command:"
echo " sudo docker build -t telegrambot-openai-api ."
echo
echo "2. After building the image, start the bot container using:"
echo " sudo docker run --env-file .env --name telegrambot-openai-api -d telegrambot-openai-api"
echo
echo "3. Check the container status with:"
echo " sudo docker ps"
echo
echo "4. Check the logs with:"
echo " sudo docker logs telegrambot-openai-api"
echo
echo "5. Stop the container with:"
echo " sudo docker stop <container_id>"
echo
echo "After that, you're all set! Enjoy, and don't forget to start the repository if you like it. :-)"
hzline &&
echo ""
# optional build & run function
function build_and_run() {
# Build Docker image
sudo docker build -t telegrambot-openai-api .
if [[ $? -ne 0 ]]; then
echo "Error: Docker image build failed."
exit 1
fi
# Run Docker container
sudo docker run --env-file .env -d telegrambot-openai-api
if [[ $? -ne 0 ]]; then
echo "Error: Failed to run the Docker container."
exit 1
fi
}
# build_and_run