Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -27,33 +29,30 @@ 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)

return 'ok'

# return handle_incoming_atlas_chat_message(incoming_msg, incoming_number, first_name)


def is_integer(value):
try:
int(value)
return True
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
Expand Down
4 changes: 4 additions & 0 deletions utils/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
48 changes: 48 additions & 0 deletions utils/mentorship_code.py
Original file line number Diff line number Diff line change
@@ -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()