This project is a server application built using Node.js and Express that sends personalized motivational quotes to users via push notifications. The application leverages the OpenAI API to generate motivational quotes tailored to each user's profile, including their name, gender, age, occupation, and preferred language. Notifications are sent using the Expo push notification service.
Frontend code GitHub repository - https://github.com/eerla/Thrive
- User Registration: Users can register their device tokens and personal information to receive personalized motivational quotes.
- Personalized Quotes: Quotes are generated using OpenAI's API, customized based on user details.
- Push Notifications: Notifications are sent using Expo's push notification service.
- Scheduled Notifications: A cron job is set up to send notifications daily at 11 PM.
- Node.js and npm installed on your machine.
- An Expo account and access to Expo's push notification service.
- An OpenAI API key, set in your environment variables as
OPENAI_API_KEY.
-
Clone the repository:
git clone https://github.com/yourusername/motivational-quote-service.git cd motivational-quote-service -
Install dependencies:
npm install
-
Set up your environment variables in a
.envfile:OPENAI_API_KEY=your_openai_api_key -
Start the server:
node server.js
- URL:
/register - Method:
POST - Description: Registers a user's device token and personal information. Sends an initial motivational quote notification.
- Request Body:
{ "token": "ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]", "name": "John Doe", "gender": "male", "age": 30, "occupation": "engineer", "language": "English", "frequency": "daily" } - Responses:
200 OK: User data saved successfully and notification sent.400 Bad Request: Invalid Expo push token.
- URL:
/send-notification - Method:
POST - Description: Sends motivational quote notifications to all registered users.
- Responses:
200 OK: Notifications sent successfully.500 Internal Server Error: Failed to send notifications.
URL: /update
Method: PUT
Description: Updates a user's personal information.
Request Body:
- Responses:
-
{ "token": "ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]", "name": "John Doe", "gender": "male", "age": 30, "occupation": "engineer", "language": "English", "frequency": "daily" }200 OK: User data updated successfully.400 Bad Request: Invalid Expo push token.
URL: /send-notification
Method: POST
Description: Sends motivational quote notifications to all registered users.
Responses:
200 OK:Notifications sent successfully.500 Internal Server Error:Failed to send notifications.
URL: /send-notification-v2
Method: POST
Description: Sends notifications using a batch API call for efficiency.
Responses:
200 OK:Notifications processed successfully.500 Internal Server Error:Failed to process notifications.
URL: /create-batch-request-file Method: POST Description: Creates a batch request file for OpenAI API calls. Responses:
200 OK:Batch request file created successfully.500 Internal Server Error:Failed to create batch request file.
URL: /batch-api-call
Method: POST
Description: Triggers a batch API call to OpenAI.
Responses:
200 OK:Batch API call completed successfully.500 Internal Server Error:Failed to make batch API call.
- Daily Notification Job: A cron job is scheduled to run every day at 11 PM to send notifications to all registered users.
- Batch File Creation: Scheduled to run every day at 1 AM to create a batch request file. Batch API Call Execution: Scheduled to run every day at 2 AM to execute the batch API call.
- Node.js: JavaScript runtime for building the server.
- Express: Web framework for Node.js.
- Expo: Service for sending push notifications.
- OpenAI API: Used to generate motivational quotes.
- node-cron: Library for scheduling tasks.
- axios: HTTP client for making requests to the OpenAI API.
- PocketBase: Backend service for managing user data.
- Logs errors to the console for debugging purposes.
- Provides default motivational messages if the OpenAI API fails to return a quote.
- TO DO
This project is licensed under the MIT License.
For any questions or issues, please contact [gurubrahmam443@gmail.com].