-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,64 @@ | ||
# code-genie-bot | ||
telegram bot for code genie | ||
|
||
======= | ||
# Environment Variables (Explanation) | ||
# code-genie-bot | ||
telegram bot for code genie | ||
|
||
======= | ||
# Environment Variables (Explanation) | ||
In `.env_dev` file save the following vars: | ||
- `BOT_TOKEN_DEV`: The telegram bot token for full control. Example: **123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi_jklmnopqrstuvwx | ||
** | ||
- `SERVER_URL_DEV`: The backend server url for making requests. Example: **http://localhost:8000/"** | ||
|
||
- `BOT_TOKEN`: The telegram bot token for full control. Example: **123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi_jklmnopqrstuvwx | ||
** | ||
- `SERVER_URL`: The backend server url for making requests. Example: **http://localhost:8000/"** | ||
In `.env_prod` file save the following vars: | ||
- `BOT_TOKEN_PROD`: The telegram bot token for full control. Example: **123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi_jklmnopqrstuvwx | ||
** | ||
- `SERVER_URL_PROD`: The backend server url for making requests. Example: **http://localhost:8000/"** | ||
# Environment Variables (Usage) | ||
To load environment variables from a `.env` file in Python, you can use the `python-dotenv` package. Here’s how you can do it: | ||
|
||
1. Save an `.env` file in your project. **WARNING**: make sure it is found in `.gitignore`. Save the above [Variables](#environment-variables-explanation) in the `.env` file using the exact provided names. | ||
2. **Install the `python-dotenv` package** (if you haven’t already): | ||
```sh | ||
pip install python-dotenv | ||
``` | ||
3. A brief example on how to load a specific environment variable: | ||
```python | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv('.env') | ||
bot_token = os.getenv('BOT_TOKEN') | ||
``` | ||
Run command for genie_bot : | ||
1. To run in dev env: | ||
```sh | ||
python genie_bot.py dev | ||
#or | ||
python genie_bot.py | ||
``` | ||
2. To run in production env: | ||
|
||
# Environment Variables (Usage) | ||
```sh | ||
python genie_bot.py prod | ||
``` | ||
|
||
To load environment variables from a `.env` file in Python, you can use the `python-dotenv` package. Here’s how you can do it: | ||
|
||
1. Save an `.env` file in your project. **WARNING**: make sure it is found in `.gitignore`. Save the above [Variables](#environment-variables-explanation) in the `.env` file using the exact provided names. | ||
|
||
2. **Install the `python-dotenv` package** (if you haven’t already): | ||
```sh | ||
pip install python-dotenv | ||
|
||
3. A brief example on how to load a specific environment variable: | ||
```python | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
bot_token = os.getenv('BOT_TOKEN') | ||
``` | ||
## the structure of the bot | ||
├── code-genie-bot \ | ||
│ # Package initia lization \ | ||
│ # Main bot entry point \ | ||
│ ├── handlers \ | ||
│ │ # Handlers package initialization \ | ||
│ │ # Handle bot commands \ | ||
│ ├── utils \ | ||
│ │ # Utils package initialization \ | ||
│ │ # Helper functions \ | ||
│ │ # Input validation \ | ||
├── tests \ | ||
│ # Tests package initialization \ | ||
│ # Handlers tests \ | ||
│ # Utils tests \ | ||
├ # Environment variables \ | ||
├ # Git ignore file \ | ||
├ # requermint list \ | ||
├ # readme | ||
├── code-genie-bot \ | ||
│ # Package initialization \ | ||
│ # Main bot entry point \ | ||
│ ├── config\ | ||
│ │ # managing and loading configuration settings | ||
│ ├── handlers \ | ||
│ │ # Handlers package initialization \ | ||
│ │ # Handle bot commands \ | ||
│ ├── utils \ | ||
│ │ # Utils package initialization \ | ||
│ │ # Helper functions \ | ||
│ │ # Input validation \ | ||
├── tests \ | ||
│ # Tests package initialization \ | ||
│ # Handlers tests \ | ||
│ # Utils tests \ | ||
├ # Environment variables \ | ||
├ # Git ignore file \ | ||
├ # requermint list \ | ||
├ # readme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .config_env import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
|
||
def create_config_env(env_name): | ||
try: | ||
if env_name in ['dev', 'prod']: | ||
env_file = f'.env_{env_name}' | ||
print('env_file: ', env_file) | ||
suffix = env_name.upper() | ||
if load_dotenv(env_file): | ||
try: | ||
bot_token = os.getenv(f'BOT_TOKEN_{suffix}') | ||
server_url = os.getenv(f'SERVER_URL_{suffix}') | ||
except Exception as e: | ||
print(e) | ||
raise KeyError(f"error in loading env vars: {e}") | ||
else: | ||
raise FileNotFoundError(f"Error: Ensure that the .env_dev or .env_prod file exists.") | ||
else: | ||
raise ValueError(f"Unknown environment: {env_name}") | ||
|
||
if not bot_token or not server_url: | ||
raise ValueError(f"Missing environment variables for {env_name}") | ||
|
||
with open('.env', 'w') as f: | ||
f.write(f"BOT_TOKEN={bot_token}\n") | ||
f.write(f"SERVER_URL={server_url}\n") | ||
|
||
except FileNotFoundError as fnf_error: | ||
raise fnf_error | ||
except ValueError as val_error: | ||
raise val_error | ||
except Exception as e: | ||
raise e | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
import argparse | ||
from config.config_env import create_config_env | ||
from handlers.handlers import * | ||
from telegram.ext import ApplicationBuilder, CommandHandler | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
def main(): | ||
# Create the Application and pass it your bot's token. | ||
application = ApplicationBuilder().token(os.getenv('BOT_TOKEN')).build() | ||
|
||
# Register command handlers | ||
application.add_handler(CommandHandler("start", start_command)) | ||
application.add_handler(CommandHandler("help", help_command)) | ||
application.add_handler(CommandHandler("ip", get_public_ip_command)) | ||
application.add_handler(CommandHandler('question', question_command)) | ||
application.add_handler(CommandHandler('api', api_command)) | ||
def setup_and_load_env(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('env', choices=['dev', 'prod'], nargs='?', default='dev') | ||
args = parser.parse_args() | ||
try: | ||
create_config_env(args.env) | ||
except Exception as e: | ||
raise e | ||
|
||
|
||
def main(): | ||
try: | ||
setup_and_load_env() | ||
load_dotenv('.env') | ||
# Create the Application and pass it your bot's token. | ||
application = ApplicationBuilder().token(os.getenv("BOT_TOKEN")).build() | ||
|
||
# Start the Bot | ||
application.run_polling() | ||
# Register command handlers | ||
application.add_handler(CommandHandler("start", start_command)) | ||
application.add_handler(CommandHandler("help", help_command)) | ||
application.add_handler(CommandHandler("ip", get_public_ip_command)) | ||
application.add_handler(CommandHandler('question', question_command)) | ||
application.add_handler(CommandHandler('api', api_command)) | ||
|
||
# Start the Bot | ||
application.run_polling() | ||
except Exception as e: | ||
print('e: ', e) | ||
exit(1) | ||
|
||
if __name__ == '__main__': | ||
load_dotenv() | ||
main() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters