|
1 |
| -# ChatConnect-Pro |
2 |
| -Integrate ChatGPT with WhatsApp without api key. |
| 1 | +## ChatConnect-Pro Documentation |
| 2 | + |
| 3 | +### Introduction |
| 4 | + |
| 5 | +ChatConnect-Pro is an advanced integration project that seamlessly connects the WhatsApp platform with the OpenAI Chatbot, facilitating efficient communication and information exchange between the two platforms. Leveraging sophisticated automation tools and cutting-edge conversational AI capabilities, ChatConnect-Pro streamlines the interaction process without the need for OpenAI API keys, relying solely on the presence of a signed-in ChatGPT window. |
| 6 | + |
| 7 | +### Requirements |
| 8 | + |
| 9 | +Before deploying the ChatConnect-Pro project, ensure the following prerequisites are in place: |
| 10 | + |
| 11 | +- **Python 3.x:** Ensure that the latest version of Python 3.x is installed on the system. |
| 12 | +- **Flask:** Install the Flask web framework to facilitate server-side scripting and routing. |
| 13 | +- **Playwright:** Set up the Playwright library for seamless automation and interaction with web pages. |
| 14 | +- **Requests:** Install the Requests library to facilitate HTTP requests and streamline communication between the server and other components. |
| 15 | +- **Golang:** Set up Golang to enable the deployment of middleware for WhatsApp integration. |
| 16 | +- **Whatsmeow:** Install the Whatsmeow library, which acts as a bridge for communication with the WhatsApp platform. |
| 17 | +- **SQLite:** Configure the SQLite database connector to support data storage and management within the project. |
| 18 | + |
| 19 | +### Browser Configuration |
| 20 | + |
| 21 | +To configure the browser settings for the ChatConnect-Pro project, follow these instructions: |
| 22 | + |
| 23 | +- To switch the browser from Firefox to Chrome, locate the following line of code in the `server.py` script: |
| 24 | + |
| 25 | +```python |
| 26 | +BROWSER = PLAY.firefox.launch_persistent_context(user_data_dir=PROFILE_DIR, headless=True) |
| 27 | +``` |
| 28 | + |
| 29 | +- Replace `firefox` with `chrome` to switch the browser to Chrome. |
| 30 | + |
| 31 | +- Additionally, you can change the headless mode by modifying the `headless=True` parameter. Setting it to `True` enables headless mode, which runs the browser without a user interface, making it ideal for automated tasks. Setting it to `False` activates the browser's graphical interface, allowing real-time visualization during script execution. |
| 32 | + |
| 33 | +### Headless Mode |
| 34 | + |
| 35 | +The headless mode enables the browser to operate without a graphical user interface, allowing it to run in the background. This mode is particularly useful for automated tasks and server-side operations where a visible browser window is not required. It significantly reduces resource consumption and enhances the efficiency of the automation process. |
| 36 | + |
| 37 | + |
| 38 | +### get_last_message function |
| 39 | + |
| 40 | +time.sleep function at specific lines within the get_last_message function: |
| 41 | + |
| 42 | +```python |
| 43 | +# Wait for the latest message to appear on the page |
| 44 | +while True: |
| 45 | + time.sleep(1) # Delays the execution for 1 second |
| 46 | +``` |
| 47 | + |
| 48 | +At this line, time.sleep(1) introduces a one-second delay in the loop. This delay allows the function to periodically pause its execution, enabling it to wait for the latest message to appear on the ChatGPT interface. This approach ensures that the function doesn't excessively consume system resources by continuously checking for updates and that it waits for a reasonable time before proceeding to retrieve the most recent message. |
| 49 | + |
| 50 | +```python |
| 51 | +time.sleep(0.2) # Checks whether the message is being generated & adds Small delay to reduce CPU usage |
| 52 | +# Increase the delay if ChatGPT is creating slow response in the browser, preferrably change it to '3'. |
| 53 | +``` |
| 54 | + |
| 55 | +In this line, time.sleep(0.2) introduces a smaller delay of 0.2 seconds. This short pause is implemented to reduce CPU usage during the iterative process of checking and updating the latest message. By incorporating this small delay, the function optimizes its performance by reducing unnecessary computational load while ensuring that it captures the most recent non-empty message accurately. |
| 56 | + |
| 57 | +The strategic use of time.sleep at these specific lines demonstrates the function's efficient and optimized approach to wait for and retrieve the latest non-empty message from the ChatGPT interface, enhancing the overall performance and resource management of the ChatConnect-Pro project. |
| 58 | + |
| 59 | + |
| 60 | +### Usage |
| 61 | + |
| 62 | +To effectively utilize the ChatConnect-Pro integration project, follow these steps: |
| 63 | + |
| 64 | +1. **Environment Setup:** |
| 65 | + Ensure that the system environment is configured with the required dependencies and libraries specified in the project's documentation. |
| 66 | + |
| 67 | +2. **Server Deployment:** |
| 68 | + - Launch the `server.py` script to initiate the connection with the OpenAI Chat platform. Modify the port and profile directory parameters as needed to suit your configuration. |
| 69 | + |
| 70 | +3. **Middleware Activation:** |
| 71 | + - Run the `main.go` script to activate the middleware, facilitating the integration with the WhatsApp platform. Configure the appropriate database connector imports to establish seamless communication. |
| 72 | + |
| 73 | +4. **Enhanced Functionality (Optional):** |
| 74 | + - For advanced functionality and multi-instance communication capabilities, deploy the `multichat.py` script. This script enables multiple instances of the OpenAI Chatbot to communicate with each other seamlessly, enhancing the overall system's conversational capabilities. |
| 75 | + |
| 76 | +5. **Monitoring and Interaction:** |
| 77 | + - Monitor the ongoing communication and interaction between the WhatsApp platform and the OpenAI Chatbot through the integrated communication channel. Leverage the powerful capabilities of ChatConnect-Pro to streamline communication processes and enhance the overall user experience for both WhatsApp users and the OpenAI Chatbot. |
| 78 | + |
| 79 | +### Introduction |
| 80 | +The `server.py` file is a Python script that facilitates communication between the OpenAI Chatbot and the WhatsApp platform. It establishes a connection between these two platforms, allowing seamless integration for the exchange of messages. The script utilizes the Flask web framework for creating the server, and the Playwright library for web automation tasks. |
| 81 | + |
| 82 | +### Dependencies |
| 83 | +- Python 3.x |
| 84 | +- Flask |
| 85 | +- Playwright |
| 86 | + |
| 87 | +### Code Structure |
| 88 | + |
| 89 | +1. **Imports**: |
| 90 | + - `time`, `asyncio`, `os`, `flask`, `sys`: Python libraries for various functionalities. |
| 91 | + - `sync_playwright`: Provides synchronous API for Playwright. |
| 92 | +2. **Constants**: |
| 93 | + - `PROFILE_DIR`: A string variable that stores the directory for the Playwright user data. |
| 94 | + - `PORT`: An integer variable that represents the port number for the Flask server. |
| 95 | +3. **Initialization**: |
| 96 | + - The Flask application is initialized. |
| 97 | + - A new Playwright instance is launched with a Firefox browser in a persistent context. |
| 98 | + - A new page is created within the browser. |
| 99 | +4. **Helper Functions**: |
| 100 | + - `get_input_box`: Finds the input box on the web page for sending messages. |
| 101 | + - `is_logged_in`: Checks if the user is logged in to the OpenAI Chat. |
| 102 | + - `send_message`: Sends a message to the OpenAI Chat. |
| 103 | + - `get_last_message`: Retrieves the latest message from the OpenAI Chat. |
| 104 | +5. **Routes**: |
| 105 | + - The `/chat` route is defined for handling incoming messages from WhatsApp and sending them to the OpenAI Chat. |
| 106 | +6. **Main Function**: |
| 107 | + - The `start_browser` function is called to initiate the browser and the Flask server. |
| 108 | + - If the user is not logged in, a message is displayed, and the script waits for the user to log in. |
| 109 | + - If the user is logged in, a success message is printed. |
| 110 | + |
| 111 | +### Usage |
| 112 | +Run the `server.py` script to initiate the server. Ensure that the required dependencies are installed before executing the script. The server listens on the specified port for incoming messages from WhatsApp, which are then sent to the OpenAI Chat for processing. The script handles the communication seamlessly between these two platforms. |
| 113 | + |
| 114 | +## Main.go Documentation |
| 115 | + |
| 116 | +### Introduction |
| 117 | +The `main.go` file is a Golang script that serves as a middleware between the WhatsApp platform and the OpenAI Chatbot. It connects to the WhatsApp platform, listens for incoming messages, and forwards them to the OpenAI Chatbot for processing. Upon receiving the response from the OpenAI Chatbot, it sends the reply back to the WhatsApp platform. The script uses the Whatsmeow library for WhatsApp connectivity. |
| 118 | + |
| 119 | +### Dependencies |
| 120 | +- Go |
| 121 | +- Whatsmeow |
| 122 | + |
| 123 | +### Code Structure |
| 124 | + |
| 125 | +1. **Imports**: |
| 126 | + - `bytes`, `context`, `fmt`, `http`, `net/url`, `os`, `os/signal`, `syscall`: Standard Go libraries for various functionalities. |
| 127 | + - Various imports related to Whatsmeow for WhatsApp functionalities. |
| 128 | +2. **Struct Definition**: |
| 129 | + - `MyClient`: A struct that contains the Whatsmeow client and its event handler. |
| 130 | +3. **Functions**: |
| 131 | + - `register`: Registers the event handler for the Whatsmeow client. |
| 132 | + - `eventHandler`: Handles the incoming WhatsApp messages, forwards them to the OpenAI Chatbot, and sends the response back to WhatsApp. |
| 133 | +4. **Main Function**: |
| 134 | + - Initializes the SQLite database and Whatsmeow client. |
| 135 | + - If the client is not already logged in, it retrieves the QR code for authentication and waits for the user to scan the code. |
| 136 | + - If the client is already logged in, it connects to the WhatsApp platform. |
| 137 | + - Listens for interrupt signals and disconnects the client upon receiving the signal. |
| 138 | + |
| 139 | +### Usage |
| 140 | +Compile and run the `main.go` script to establish the connection between the WhatsApp platform and the OpenAI Chatbot. Make sure to install the required dependencies before running the script. The script listens for incoming messages from WhatsApp, forwards them to the OpenAI Chatbot, and sends the response back to WhatsApp. |
| 141 | + |
| 142 | +## Multichat.py Documentation |
| 143 | + |
| 144 | +### Introduction |
| 145 | +The `multichat.py` script is designed to create a conversation between two instances of the `server.py` script, effectively allowing the OpenAI Chatbot to communicate with itself. By sending requests to two different instances of the server running on different ports, it simulates a conversation between two entities, each representing one side of the conversation. |
| 146 | + |
| 147 | +### Dependencies |
| 148 | +- Python 3.x |
| 149 | +- Requests |
| 150 | + |
| 151 | +### Code Structure |
| 152 | + |
| 153 | +1. **Imports**: |
| 154 | + - `requests`: Python library for making HTTP requests. |
| 155 | + |
| 156 | +2. **Constants**: |
| 157 | + - `metaprompt`: A string representing a specific message prompt. |
| 158 | + |
| 159 | +3. **Functionality**: |
| 160 | + - The script initiates two instances of the `server.py` script on different ports, 5001 and 5002, respectively. |
| 161 | + - The script sends a request to the first instance, initiating the conversation by providing an initial message. |
| 162 | + - It then continuously exchanges messages between the two instances, allowing the OpenAI Chatbot to communicate with itself. |
| 163 | + - The `metaprompt` is used to identify and replace specific prompts during the conversation, ensuring the messages remain coherent and meaningful. |
| 164 | + |
| 165 | +### Usage |
| 166 | +Ensure that the `server.py` script is already running on ports 5001 and 5002 before executing the `multichat.py` script. The script simulates a conversation between two instances of the OpenAI Chatbot, with each instance communicating with the other in an alternating manner. The `metaprompt` helps in maintaining the coherence and flow of the conversation. |
0 commit comments