diff --git a/app.py b/app.py index fa9dfe5..2da5499 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ import datetime from utils.mentors import handle_mentors_search, handle_mentor_selection +from utils.mentorship_code import AtilaMentorship from utils.whatsapp import send_whatsapp_message from utils.global_state import set_searching @@ -15,6 +16,7 @@ one_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1) unix_timestamp_one_hour_ago = int(one_hour_ago.timestamp()) + @app.route('/', methods=['GET', 'POST']) def index(): return 'Welcome to Atila' @@ -27,19 +29,14 @@ async def whatsapp(): incoming_msg = request.form.get('Body').strip() incoming_number = request.form.get('WaId') first_name = request.form.get('ProfileName') - + command_string = incoming_msg.lower() if WHATSAPP_NUMBER in incoming_number: response = 'this number is from the bot' print(response) return response - if incoming_msg.lower().startswith('mentor search'): - value = incoming_msg.lower().split('mentor search')[1].strip() - handle_mentors_search(value, incoming_number) - set_searching(incoming_number) - elif incoming_msg.lower().startswith('mentor select'): - handle_mentor_selection(incoming_msg.lower().split('mentor select')[1].strip(), incoming_number, None) - elif is_integer(incoming_msg): - handle_mentor_selection(incoming_msg, incoming_number, None) + if command_string.startswith('mentor code generate'): + response = AtilaMentorship.handle_command_generate_mentorship_code(command_string) + send_whatsapp_message(response, incoming_number) else: send_whatsapp_message(f"invalid command", incoming_number) @@ -47,6 +44,7 @@ async def whatsapp(): # return handle_incoming_atlas_chat_message(incoming_msg, incoming_number, first_name) + def is_integer(value): try: int(value) @@ -54,6 +52,7 @@ def is_integer(value): except ValueError: return False + async def run_flask_app(): # Mac OSX Monterey (12.x) currently uses ports 5000 and 7000 for its Control centre hence the issue. # Try running your app from port other than 5000 and 7000 diff --git a/utils/credentials.py b/utils/credentials.py index 0407202..84452e9 100644 --- a/utils/credentials.py +++ b/utils/credentials.py @@ -15,3 +15,7 @@ ALGOLIA_API_KEY = os.getenv('ALGOLIA_API_KEY', False) ATILA_CORE_SERVICE_URL = "http://127.0.0.1:8000/api" + +# In staging and prod ATILA_CORE_SERVICE_URL and ATILA_API_URL will be different endpoints +ATILA_API_URL = os.getenv('ATILA_API_URL', False) +ATILA_API_KEY = os.getenv('ATILA_API_KEY', False) diff --git a/utils/mentorship_code.py b/utils/mentorship_code.py new file mode 100644 index 0000000..61d9ea7 --- /dev/null +++ b/utils/mentorship_code.py @@ -0,0 +1,48 @@ +from http.client import HTTPException + +import requests + +from utils.credentials import ATILA_API_KEY, ATILA_API_URL + + +class AtilaMentorship: + mentorship_url = f"{ATILA_API_URL}/mentorship" + mentorship_code_url = f"{mentorship_url}/codes/" + + @staticmethod + def handle_command_generate_mentorship_code(command_string): + + parts = command_string.split() + + # Extract the count part if it exists + if len(parts) == 4: + count_str = parts[3] + # Convert count to integer, default to 1 if empty + count = int(count_str) if count_str.isdigit() else 1 + else: + # Default to 1 if the format is incorrect + count = 1 + codes = AtilaMentorship.generate_mentorship_code(count) + + codes_list = codes.get('codes', []) + codes_string = "\n".join(code['code'] for code in codes_list) + return codes_string + + @staticmethod + def generate_mentorship_code(count=1): + """ + Save a new scholarship to the database. + """ + + headers = { + 'Authorization': 'Token {}'.format(ATILA_API_KEY) + } + response = requests.post(AtilaMentorship.mentorship_code_url, json={"count": count}, headers=headers) + + print(f"\nresponse.status_code: {response.status_code}") + if 200 <= response.status_code < 300: + print(f"\nSuccessfully created codes") + else: + print(f"Error saving scholarship: {response.json()}") + raise HTTPException(response.json()) + return response.json()