-
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.
Merge pull request #39 from Makes-Innovation-Hub/return-question--ans…
…wers-to-ui-11 Return question answers to UI 11
- Loading branch information
Showing
11 changed files
with
449 additions
and
21 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
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,18 +1,64 @@ | ||
# code-genie-bot | ||
telegram bot for code genie \ | ||
The Code Genie bot will assist graduates in maintaining their study habits and tracking their progress through interactive sessions and resources. | ||
## Team Members | ||
Basel Amin\ | ||
Mohammad Shaheen\ | ||
Hasan Masalha\ | ||
Aya Abbas\ | ||
Shaden Hakim | ||
## Product Goal | ||
This Telegram bot, named Code Genie, is designed to help graduates continue their studies and stay committed to practicing even after graduation | ||
## General Architecture | ||
### Telegram Bot: | ||
User Interface: Interacts with users via the Telegram platform. \ | ||
Bot Logic: Handles commands and messages from users, processes requests, and sends responses. | ||
|
||
User <----> Bot <---> Server <-----> Database | ||
# 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/"** | ||
|
||
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: | ||
|
||
```sh | ||
python genie_bot.py prod | ||
``` | ||
|
||
|
||
├── 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,9 @@ | ||
HELP_COMMAND_TEXT = ( | ||
"Available commands:\n" | ||
"/start - Start the bot\n" | ||
"/help - Show this help message\n" | ||
"/ip - Get public ip\n" | ||
"/question - Get a question from the server\n " | ||
"/ip - get public ip\n" | ||
"/api - connect to server" | ||
) |
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 |
---|---|---|
@@ -0,0 +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 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() | ||
|
||
# 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__': | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
from telegram import Update | ||
from telegram.ext import ContextTypes, CallbackContext | ||
import requests | ||
from config import CONSTANTS | ||
|
||
|
||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
await update.message.reply_text('Hello! I am your bot. How can I help you?') | ||
|
||
|
||
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
await update.message.reply_text(CONSTANTS.HELP_COMMAND_TEXT) | ||
|
||
|
||
async def get_public_ip_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool: | ||
try: | ||
response = requests.get('https://api.ipify.org?format=json') | ||
public_ip = response.json()['ip'] | ||
await update.message.reply_text(f'The public IP address of the bot is: {public_ip}') | ||
return True | ||
except requests.RequestException as e: | ||
await update.message.reply_text(f'Failed to get public IP address: {e}') | ||
return False | ||
|
||
async def question_command(update: Update, context: CallbackContext) -> None: | ||
try: | ||
data = { | ||
"topic": "python", | ||
"difficulty": "easy", | ||
"answers_num": 0 | ||
} | ||
# Define headers, if required | ||
headers = { | ||
"accept": "application/json", | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.post( | ||
f'{os.getenv("SERVER_URL")}/question/generate', | ||
json=data, | ||
headers=headers | ||
) | ||
response_data = response.json() | ||
question = response_data.get('Question', 'No question found') | ||
to_return = question | ||
await update.message.reply_text(f"{to_return}") | ||
except requests.exceptions.RequestException as e: | ||
await update.message.reply_text(f"An error occurred: {e}") | ||
|
||
|
||
async def api_command(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
try: | ||
response = requests.get(os.getenv("SERVER_URL")) | ||
response.raise_for_status() | ||
data = response.json() | ||
await update.message.reply_text(data) | ||
except requests.RequestException as e: | ||
await update.message.reply_text(f"Request failed: {str(e)}") | ||
except Exception as e: | ||
await update.message.reply_text(f"An unexpected error occurred: {str(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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
annotated-types==0.7.0 | ||
anyio==4.4.0 | ||
certifi==2024.7.4 | ||
charset-normalizer==3.3.2 | ||
click==8.1.7 | ||
colorama==0.4.6 | ||
distro==1.9.0 | ||
dnspython==2.6.1 | ||
email_validator==2.2.0 | ||
fastapi==0.111.1 | ||
fastapi-cli==0.0.4 | ||
h11==0.14.0 | ||
httpcore==1.0.5 | ||
httptools==0.6.1 | ||
httpx==0.27.0 | ||
idna==3.7 | ||
iniconfig==2.0.0 | ||
Jinja2==3.1.4 | ||
markdown-it-py==3.0.0 | ||
MarkupSafe==2.1.5 | ||
mdurl==0.1.2 | ||
openai==1.36.1 | ||
packaging==24.1 | ||
pluggy==1.5.0 | ||
pydantic==2.8.2 | ||
pydantic_core==2.20.1 | ||
Pygments==2.18.0 | ||
pytest==8.3.1 | ||
pytest-asyncio==0.23.8 | ||
python-dotenv==1.0.1 | ||
python-multipart==0.0.9 | ||
python-telegram-bot==21.4 | ||
PyYAML==6.0.1 | ||
requests==2.32.3 | ||
rich==13.7.1 | ||
shellingham==1.5.4 | ||
sniffio==1.3.1 | ||
starlette==0.37.2 | ||
tqdm==4.66.4 | ||
typer==0.12.3 | ||
typing_extensions==4.12.2 | ||
urllib3==2.2.2 | ||
uvicorn==0.30.3 | ||
watchfiles==0.22.0 | ||
websockets==12.0 |
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.test_bot import * |
Oops, something went wrong.