This project is a Django-based API system designed to process incoming messages, execute commands via plugins, and interact with a WhatsApp client. It also supports OpenAI's GPT model for generating conversational responses. The system handles different types of media, manages user registrations, and provides both user and admin-level commands.
- Plugin System: Dynamically load and execute commands through a plugin architecture.
- Admin Privileges: Support for admin-only commands.
- Message Handling: Process and validate incoming messages from users or groups.
- Media Support: Handle images, videos, audio, and documents.
- GPT Integration: Generate and respond to messages using OpenAI's GPT.
- Error Handling: Custom exceptions for various errors (e.g., invalid commands, empty messages in groups).
- Timezone Management: Extract and use the sender’s timezone for time-sensitive responses.
-
Clone the repository:
git clone https://github.com/abdbbdii/Whatsapp_API.git cd Whatsapp_API
-
Install dependencies:
pip install -r requirements.txt
-
Set up Environment Variables:
-
Create a .env file in the root directory. (This will be needed only the first time. After that, it will be saved in the database.)
-
Add the following environment variables to the .env file:
DJANGO_KEY
DATABASE_URL
OPENAI_API_KEY
PUBLIC_IP
WHATSAPP_CLIENT_URL
WHATSAPP_CLIENT_URL_TEST
PUBLIC_URL
PUBLIC_URL_TEST
ADMIN_IDS
BLACKLIST_IDS
ADMIN_COMMAND_PREFIX
-
-
Set up Django:
- Configure your settings.py with appropriate database and application settings.
- Apply migrations:
python manage.py migrate
-
Run the Django server:
python manage.py runserver
- Create a new Python file in the api/plugins directory.
- Define a pluginInfo dictionary containing:
- command_name: The name of the command.
- admin_privilege: Boolean indicating if admin privilege is required.
- description: A short description of the command.
- handle_function: The function that handles the command's logic.
- (Optional) preprocess: A function for preprocessing messages.
- (Optional) helpMessage: A dictionary defining the help message structure.
- Implement the command's logic in the file.
- The plugin will be automatically loaded by the Plugin.load_plugins() method.
pluginInfo = {
"command_name": "example",
"admin_privilege": False,
"description": "This is an example command.",
}
def handle_function(message):
message.outgoing_text_message = "This is an example response."
message.send_message()
The system includes custom exceptions for specific scenarios:
- SenderInBlackList: Raised when the sender is in the blacklist.
- SenderNotAdmin: Raised when a non-admin user attempts to execute an admin command.
- EmptyMessageInGroup: Raised when a message in a group is empty.
- CommandNotFound: Raised when an unrecognized command is received.
- MessageNotValid: Raised when the message is not valid.
- SendHelp: Used to trigger the sending of help messages.
The system integrates with OpenAI's GPT model to generate conversational responses. The gptResponse() method formats the system and user messages and interacts with OpenAI's API to generate a response.
-
Help Command:
- Users can send /help to receive a list of available commands.
- Admins can send /admin help for admin-specific commands.
-
Custom Commands:
- Commands are defined in plugins and are dynamically loaded based on user input.
- The system processes incoming messages and determines if they are from a group or individual.
- Messages are validated and any associated media is processed.
- Commands are extracted and executed if found.
- The response is sent back to the user or group.
Feel free to contribute to this project by adding new plugins, improving the existing codebase, or fixing bugs. Please ensure that your contributions are well-documented and tested.
This project is licensed under the MIT License. See the LICENSE file for more details.