Skip to content

Commit 6c4035c

Browse files
base v1
ChatConnect-Pro
1 parent 75db660 commit 6c4035c

10 files changed

+591
-18
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
env/
2+
examplestore.db
3+
node_modules/
4+
/test-results/
5+
/playwright-report/
6+
/blob-report/
7+
/playwright/.cache/

LICENSE

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
MIT License
22

3-
Copyright (c) 2023 codingwithzaz
3+
Copyright © 2023 Muhammad Aitzaz
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Welcome to the Creative Code Commons! You are granted the liberty to explore, experiment, and create using this software and any accompanying documentation. This license allows you the freedom to delve into the depths of innovation without any limitations on your imaginative journey.
116

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
7+
You are encouraged to utilize, modify, and distribute this code, promoting a collaborative spirit of creativity and advancement within the coding community. Let your imagination soar and your creations inspire others as you leverage the power of this software for your unique projects and endeavors.
148

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
9+
Remember, the possibilities are endless. Embrace the spark of ingenuity and contribute to the ever-expanding tapestry of technological marvels, all while attributing the original source and extending the same freedoms to your recipients.
10+
11+
May your coding adventures be as boundless as your imagination, and may the innovations you craft shape the future of technological brilliance.
12+
13+
With endless enthusiasm and a shared passion for coding,
14+
15+
ChatConnect Pro

README.md

+166-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,166 @@
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.

go.mod

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module example.com/m
2+
3+
go 1.19
4+
5+
require (
6+
github.com/mattn/go-sqlite3 v1.14.17
7+
github.com/mdp/qrterminal/v3 v3.1.1
8+
go.mau.fi/whatsmeow v0.0.0-20231022190735-0c67ac8e0bdd
9+
google.golang.org/protobuf v1.31.0
10+
)
11+
12+
require (
13+
filippo.io/edwards25519 v1.0.0 // indirect
14+
github.com/gorilla/websocket v1.5.0 // indirect
15+
github.com/mdp/qrterminal v1.0.1 // indirect
16+
go.mau.fi/libsignal v0.1.0 // indirect
17+
go.mau.fi/util v0.2.0 // indirect
18+
golang.org/x/crypto v0.14.0 // indirect
19+
rsc.io/qr v0.2.0 // indirect
20+
)

go.sum

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
2+
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
3+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
6+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
8+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
9+
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
10+
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
11+
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
12+
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
13+
github.com/mdp/qrterminal v1.0.1 h1:07+fzVDlPuBlXS8tB0ktTAyf+Lp1j2+2zK3fBOL5b7c=
14+
github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ=
15+
github.com/mdp/qrterminal/v3 v3.0.0 h1:ywQqLRBXWTktytQNDKFjhAvoGkLVN3J2tAFZ0kMd9xQ=
16+
github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXlBqXQEwvE0=
17+
github.com/mdp/qrterminal/v3 v3.1.1 h1:cIPwg3QU0OIm9+ce/lRfWXhPwEjOSKwk3HBwL3HBTyc=
18+
github.com/mdp/qrterminal/v3 v3.1.1/go.mod h1:5lJlXe7Jdr8wlPDdcsJttv1/knsRgzXASyr4dcGZqNU=
19+
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf h1:mzPxXBgDPHKDHMVV1tIWh7lwCiRpzCsXC0gNRX+K07c=
20+
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf/go.mod h1:XCjaU93vl71YNRPn059jMrK0xRDwVO5gKbxoPxow9mQ=
21+
go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c=
22+
go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I=
23+
go.mau.fi/util v0.2.0 h1:AMGBEdg9Ya/smb/09dljo9wBwKr432EpfjDWF7aFQg0=
24+
go.mau.fi/util v0.2.0/go.mod h1:AxuJUMCxpzgJ5eV9JbPWKRH8aAJJidxetNdUj7qcb84=
25+
go.mau.fi/whatsmeow v0.0.0-20221202110551-e067ee7293b0 h1:danzDOlj/KiDi8kNsaHOhwJ7IZdo7V7hXelkZXhJhsc=
26+
go.mau.fi/whatsmeow v0.0.0-20221202110551-e067ee7293b0/go.mod h1:2yweL8nczvtlIxkrvCb0y8xiO13rveX9lJPambwYV/E=
27+
go.mau.fi/whatsmeow v0.0.0-20231022190735-0c67ac8e0bdd h1:t+5ga49o4IoF7v6LSD4MTCl82AHinPv3RlCU2Qbq1Ss=
28+
go.mau.fi/whatsmeow v0.0.0-20231022190735-0c67ac8e0bdd/go.mod h1:u557d2vph8xcLrk3CKTBknUHoB6icUpqazA4w+binRU=
29+
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNRTKxGhlPfD6OA87W/PLkqg=
30+
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
31+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
32+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
33+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
34+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
35+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
36+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
37+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
38+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
39+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
40+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
41+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=

0 commit comments

Comments
 (0)