Skip to content

Commit

Permalink
Add Wx chatbot doc (#970)
Browse files Browse the repository at this point in the history
* Add  README.md to the wechat-bot Use Case

* add wxchat-bot doc

* Add new doc
  • Loading branch information
Luffy2004-c authored Oct 13, 2024
1 parent 6334f09 commit f2d2a12
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export default withPwa(defineConfig({
{ text: 'Build math application with agent', link: '/use_cases/build-math-application-with-agent' },
{ text: 'Groq, llama3, Streamlit to build a application', link: '/use_cases/streamlit-groq-llama3' },
{ text: 'Build knowledge map with streamlit', link: '/use_cases/llmapper' },
{ text: 'Chat with GitHub repo', link: '/use_cases/chat-to-github-repo' }
{ text: 'Chat with GitHub repo', link: '/use_cases/chat-to-github-repo' },
{ text: 'Build Wechat-bot with itchat', link: '/use_cases/wx-chatbot' },
]
},
{
Expand Down
104 changes: 104 additions & 0 deletions docs/use_cases/wx-chatbot.md
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.
34 changes: 34 additions & 0 deletions example/wx-chatbot/README.md
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
```
4 changes: 3 additions & 1 deletion example/wx-chatbot/requirements.txt
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

0 comments on commit f2d2a12

Please sign in to comment.