-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add README.md to the wechat-bot Use Case * add wxchat-bot doc * Add new doc
- Loading branch information
1 parent
6334f09
commit f2d2a12
Showing
4 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# WeChat Chatbot | ||
|
||
## Overview | ||
|
||
This code implements a WeChat chatbot that interacts with WeChat using the `itchat` library and generates replies using OpenAI's GPT-3.5-turbo model. The bot can receive text messages and process them accordingly. | ||
|
||
## Main Features | ||
|
||
1. **Message Handling**: Receives text messages from WeChat and generates replies using the GPT-3.5-turbo model. | ||
2. **QR Code Login**: Generates a QR code for users to scan and log in to WeChat. | ||
3. **Error Handling**: Captures and logs exceptions during message processing. | ||
|
||
## Code Structure | ||
|
||
### 1. Import Necessary Libraries | ||
|
||
```python | ||
import time | ||
import chat_message | ||
import itchat | ||
import pne | ||
from itchat.content import TEXT | ||
from itchat.storage.messagequeue import Message | ||
from promptulate.utils import logger | ||
``` | ||
|
||
- `time`: Used to control the program's runtime. | ||
- `chat_message`: A custom module for handling chat messages. | ||
- `itchat`: Used for interacting with WeChat. | ||
- `pne`: Used to call OpenAI's chat interface. | ||
- `logger`: Used for logging. | ||
|
||
### 2. Message Handling Function | ||
|
||
```python | ||
@itchat.msg_register([TEXT]) | ||
def handler_single_msg(msg: Message): | ||
... | ||
``` | ||
|
||
- This function is registered as a message handler, receiving text messages and calling the `handle` method of the `MessageHandler` class for processing. | ||
|
||
### 3. QR Code Callback Function | ||
|
||
```python | ||
def qrCallback(uuid, status, qrcode): | ||
... | ||
``` | ||
|
||
- This function is called after the QR code is generated, providing multiple QR code links for the user to scan and log in. | ||
|
||
### 4. Startup Function | ||
|
||
```python | ||
def startup(): | ||
... | ||
``` | ||
|
||
- This function initializes `itchat`, logs in to WeChat, and starts receiving messages. | ||
|
||
### 5. Message Handling Class | ||
|
||
```python | ||
class MessageHandler: | ||
... | ||
``` | ||
|
||
- This class is responsible for processing received messages and generating replies using OpenAI's API. | ||
|
||
### 6. Main Program Entry | ||
|
||
```python | ||
if __name__ == "__main__": | ||
startup() | ||
while True: | ||
time.sleep(1) | ||
``` | ||
|
||
- The main entry point of the program, calling the `startup` function and keeping the program running. | ||
|
||
## Usage Instructions | ||
|
||
1. **Install Dependencies**: | ||
Ensure that the `itchat` and `pne` libraries are installed. You can use the following command: | ||
|
||
```bash | ||
pip install itchat pne | ||
``` | ||
|
||
2. **Configure API Key**: | ||
In the `MessageHandler` class, replace `api_key` with your OpenAI API key. | ||
|
||
3. **Run the Program**: | ||
Execute the `app.py` file, and the program will generate a QR code for the user to scan and log in. | ||
|
||
4. **Send Messages**: | ||
After logging in, users can send text messages, and the bot will automatically reply. | ||
|
||
## Notes | ||
|
||
- Ensure a stable internet connection to access OpenAI's API. | ||
- Be cautious when handling sensitive information, such as protecting the API key and user data. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Build a WeChat chatbot using pne and itchat | ||
|
||
## Introduction | ||
|
||
This is a simple example of building a WeChat chatbot using pne and itchat. | ||
|
||
## Prerequisites | ||
|
||
Before you start, you need to have the following : | ||
|
||
1. Python 3.8+ | ||
2. Required Python packages: | ||
- `itchat` | ||
- `promptulate` | ||
- `pne` | ||
3. An OpenAI API key for pne | ||
4. A WeChat account with the necessary permissions to use itchat | ||
|
||
## Installation | ||
|
||
Install the necessary Python packages using pip: | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Usage | ||
|
||
First,you need to edit the `model_config` in app.py to set your OpenAI API key.And then | ||
you can run the chatbot using the following command: | ||
|
||
```bash | ||
python app.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
itchat-uos == 1.4.1 | ||
itchat-uos == 1.4.1 | ||
pne | ||
promotulate |