Skip to content

Commit

Permalink
feat: post user info to dify (#160)
Browse files Browse the repository at this point in the history
* docs: user info to dify guides

* docs: resize image

* docs: center image

* docs: resize images

* docs: center table

* docs: resize images
  • Loading branch information
hanfangyuan4396 authored Dec 14, 2024
1 parent be64eb0 commit 51a2517
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 19 deletions.
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ Dify接入微信生态的**详细教程**请查看文章 [**手摸手教你把 D
如果我的项目对您有帮助请点一个star吧~
</div>

<div align="center">
<img width="700" src="./docs/images/image1.jpg">
</div>


![image-1](./docs/images/image1.jpg)

![image-2](./docs/images/image2.jpg)
<div align="center">
<img width="700" src="./docs/images/image2.jpg">
</div>

基本的dify workflow api支持

![image-3](./docs/images/image4.jpg)
<div align="center">
<img width="700" src="./docs/images/image4.jpg">
</div>

目前Dify已经测试过的通道如下:

Expand All @@ -34,12 +38,17 @@ Dify接入微信生态的**详细教程**请查看文章 [**手摸手教你把 D
# 交流群


<div align="center">

|<img width="240" src="./docs/images/wechat.jpg">|<img width="240" src="./docs/images/supportme.jpg">|
|:-:|:-:|
|添加我的微信拉你进交流群|开源不易,感谢打赏🎉|

</div>


# 最新功能

## 1. 支持gewechat登录微信
基于[Gewechat](https://github.com/Devo919/Gewechat)项目实现的微信个人号通道,使用ipad协议登录,相比itchat协议更稳定。

Expand Down Expand Up @@ -83,11 +92,23 @@ python app.py
```
启动成功后,可以看到如下日志信息,注意token和appid会**自动保存**到config.json,无需手动保存

![gewechat_login](./docs/gewechat/gewechat_login.jpg)
<div align="center">
<img width="700" src="./docs/gewechat/gewechat_login.jpg">
</div>

## 2. 用户信息对接dify

新增用户信息对接dify的能力,会把用户id、用户名称、群聊id、群聊名称信息传递给dify,搭配 gewechat_channel 提供的wxid与chatroomid,
可以在dify中识别出每个用户,实现个性化服务。详细教程请查看:[用户信息对接dify](./docs/user-info/README.md)

<div align="center">
<img width="700" src="./docs/user-info/user-info-room.jpg">
</div>

## 2. 支持企业微信个人号(仅支持windows系统)
![wework](./docs/images/wework.jpg)
<div align="center">
<img width="700" src="./docs/images/wework.jpg">
</div>

> 1.**封号风险**,请使用企业微信**小号**测试
> 2. 在登录旧版本的企业微信时可能会出现企业微信版本过低,无法登录情况,参考[issue1525](https://github.com/zhayujie/chatgpt-on-wechat/issues/1525),请尝试更换其他企业微信号重试
Expand Down Expand Up @@ -160,8 +181,13 @@ python app.py
## 3. 集成[JinaSum](https://github.com/hanfangyuan4396/jina_sum)插件
使用Jina Reader和ChatGPT支持总结公众号、小红书、知乎等分享卡片链接,配置详情请查看[JinaSum](https://github.com/hanfangyuan4396/jina_sum)

![plugin-jinasum-1](./plugins/jina_sum/docs/images/wechat_mp.jpg)
![plugin-jinasum-1](./plugins/jina_sum/docs/images/red.jpg)
<div align="center">
<img width="700" src="./plugins/jina_sum/docs/images/wechat_mp.jpg">
</div>

<div align="center">
<img width="700" src="./plugins/jina_sum/docs/images/red.jpg">
</div>

## 4. 新增[CustomDifyApp](https://github.com/hanfangyuan4396/dify-on-wechat/tree/master/plugins/custom_dify_app)插件
支持根据群聊名称关键词自动切换不同的Dify应用,也支持为单聊配置专门的Dify应用。
Expand All @@ -176,11 +202,13 @@ dify官网已正式上线工作流模式,可以导入本项目下的[dsl文件
(ps: 感觉工作流类型应用不太适合作为聊天机器人,现在它还没有会话的概念,需要自己管理上下文。但是它可以调用各种工具,通过http请求和外界交互,适合执行业务逻辑复杂的任务;它可以导入导出工作流dsl文件,方便分享移植。也许以后dsl文件+配置文件就可以作为本项目的一个插件。)
## 6. 支持COZE API

![image-5](./docs/images/image5.jpg)

![image-6](./docs/images/image6.jpg)

<div align="center">
<img width="700" src="./docs/images/image5.jpg">
</div>

<div align="center">
<img width="700" src="./docs/images/image6.jpg">
</div>

### 6.1 如何快速启动coze微信机器人

Expand Down Expand Up @@ -233,6 +261,7 @@ dify语音相关配置如下,另外需要在dify应用中开启语音转文字
```

# 更新日志
- 2024/12/14 支持用户信息对接dify
- 2024/12/04 新增 [gewechat](https://github.com/Devo919/Gewechat) 通道,相比itchat更稳定。
- 2024/10/01 新增插件CustomDifyApp与GroupAtAutoreply,CustomDifyApp支持根据群聊名称关键词自动切换不同的Dify应用,GroupAtAutoreply支持群聊艾特自动回复,贡献者[**blankbro**](https://github.com/blankbro)
- 2024/09/18 支持dify voice
Expand Down Expand Up @@ -377,5 +406,4 @@ docker logs -f dify-on-wechat # 查看二维码并登录
感谢 **NLP工程化** 知识星球对本项目的支持

<img width="240" src="./docs/images/NLP工程化.png">

(提供Dify源码剖析及答疑,Dify对话系统源码等)
132 changes: 132 additions & 0 deletions channel/gewechat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# 1. gewechat channel简介

Gewechat channel是基于[Gewechat](https://github.com/Devo919/Gewechat)项目实现的微信个人号通道,使用ipad协议登录,该协议能获取到wxid,能发送语音条消息,相比itchat协议更稳定。

api文档地址为:[gewechat api](https://apifox.com/apidoc/shared-69ba62ca-cb7d-437e-85e4-6f3d3df271b1/api-197179336)

# 2. gewechat 服务部署教程

gewechat 服务需要自行部署,[dify-on-wechat](https://github.com/hanfangyuan4396/dify-on-wechat) 项目只负责对接gewechat服务,请参考下方教程部署gewechat服务。

## 2.1 下载镜像

感谢gewechat交流群中的大佬 `@1H` 重构了镜像,让gewe镜像不依赖cgroup和docker --privilege,可以在更高版本的ubuntu、debian以及macos系统上运行。

```bash
# 从阿里云镜像仓库拉取(国内)
docker pull registry.cn-chengdu.aliyuncs.com/tu1h/wechotd:alpine
docker tag registry.cn-chengdu.aliyuncs.com/tu1h/wechotd:alpine gewe

# 或从GitHub镜像仓库拉取
docker pull ghcr.io/tu1h/wechotd/wechotd:alpine
docker tag ghcr.io/tu1h/wechotd/wechotd:alpine gewe
```

## 2.2 使用docker启动

```bash
mkdir -p gewechat/data
docker run -itd -v gewechat/data:/root/temp -p 2531:2531 -p 2532:2532 --restart=always --name=gewe gewe
```

## 2.3 使用docker compose启动

首先创建必要的数据目录:

```bash
mkdir -p gewechat/data
```

创建 `docker-compose.yml` 文件:

```yaml
version: '3'
services:
gewechat:
image: gewe
container_name: gewe
volumes:
- ./gewechat/data:/root/temp
ports:
- "2531:2531"
- "2532:2532"
restart: always
```
运行:
```bash
docker compose up -d
```

## 2.4 成功日志

看到如下日志,表示gewechat服务启动成功

<div align="center">
<img width="700" src="../../docs/gewechat/gewechat_service_success.jpg">
</div>

# 3. 使用dify-on-wechat对接gewechat服务

## 3.1 gewechat相关参数配置

在config.json中需要配置以下gewechat相关的参数:

```bash
{
"channel_type": "gewechat" # 通道类型,请设置为gewechat
"gewechat_token": "", # gewechat服务的token,用于接口认证
"gewechat_app_id": "", # gewechat服务的应用ID
"gewechat_base_url": "http://本机ip:2531/v2/api", # gewechat服务的API基础URL
"gewechat_callback_url": "http://本机ip:9919/v2/api/callback/collect", # 回调URL,用于接收消息
"gewechat_download_url": "http://本机ip:2532/download", # 文件下载URL
}
```

参数说明:
- `gewechat_token`: gewechat服务的认证token,首次登录时,可以留空,启动dify-on-wechat服务时,会**自动获取token****自动保存到config.json**
- `gewechat_app_id`: gewechat服务分配的设备ID,首次登录时,可以留空,启动dify-on-wechat服务时,会**自动获取appid****自动保存到config.json**
- `gewechat_base_url`: gewechat服务的API基础地址,请根据实际情况配置,如果gewechat服务与dify-on-wechat服务部署在同一台机器上,可以配置为`http://本机ip:2531/v2/api`
- `gewechat_callback_url`: 接收gewechat消息的回调地址,请根据实际情况配置,如果gewechat服务与dify-on-wechat服务部署在同一台机器上,可以配置为`http://本机ip:9919/v2/api/callback/collect`,如无特殊需要,请使用9919端口号
- `gewechat_download_url`: 文件下载地址,用于下载语音、图片等文件,请根据实际部署情况配置,如果gewechat服务与dify-on-wechat服务部署在同一台机器上,可以配置为`http://本机ip:2532/download`

注意:请确保您的回调地址(callback_url),即dify-on-wechat启动的回调服务可以被gewechat服务正常访问到。如果您使用Docker部署,需要注意网络配置,确保容器之间可以正常通信。

## 3.2 dify相关参数配置

在config.json中需要配置以下dify相关参数:

```bash
{
"dify_api_base": "https://api.dify.ai/v1", # dify base url
"dify_api_key": "app-xxx", # dify api key
"dify_app_type": "chatbot", # dify应用类型,对应聊天助手
"channel_type": "gewechat", # 通道类型设置为gewechat
"model": "dify", # 模型名称设置为dify
"single_chat_prefix": [""], # 私聊触发前缀
"single_chat_reply_prefix": "", # 私聊回复前缀
"group_chat_prefix": ["@bot"], # 群聊触发前缀
"group_name_white_list": ["ALL_GROUP"], # 允许响应的群组
}
```

关于dify_api_base、dify_api_key等参数的获取方法,请参考文章 [手摸手教你把 Dify 接入微信生态](https://docs.dify.ai/v/zh-hans/learn-more/use-cases/dify-on-wechat)

## 3.3 启动dify-on-wechat服务

完成上述配置后,你需要确保gewechat服务已正常启动,dify-on-wechat的依赖已安装(见 [dify-on-wechat项目README](https://github.com/hanfangyuan4396/dify-on-wechat)[手摸手教你把 Dify 接入微信生态](https://docs.dify.ai/v/zh-hans/learn-more/use-cases/dify-on-wechat) ),然后运行以下命令启动服务:

```bash
python app.py
```
启动成功后,可以看到如下日志信息,注意token和appid会自动保存到config.json,无需手动保存

<div align="center">
<img width="700" src="../../docs/gewechat/gewechat_login.jpg">
</div>

# 4. gewechat_channel 服务的限制
1. gewechat 要求必须搭建服务到**同省**服务器或者电脑里方可正常使用,即登录微信的手机与gewechat服务必须在同一省
2. gewechat 开源框架**只支持**下载接收到的图片,不支持下载文件
3. gewechat_channel 目前暂时**只支持接收文字消息****只支持发送文字消息与图片消息**,后续支持的消息类型会逐步完善
4. 此项目仅用于个人娱乐场景,请**勿用于任何商业场景**
8 changes: 6 additions & 2 deletions docs/gewechat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ docker compose up -d

看到如下日志,表示gewechat服务启动成功

![gewechat_service_success](./gewechat_service_success.jpg)
<div align="center">
<img width="700" src="./gewechat_service_success.jpg">
</div>

# 3. 使用dify-on-wechat对接gewechat服务

Expand Down Expand Up @@ -119,7 +121,9 @@ python app.py
```
启动成功后,可以看到如下日志信息,注意token和appid会自动保存到config.json,无需手动保存

![gewechat_login](./gewechat_login.jpg)
<div align="center">
<img width="700" src="./gewechat_login.jpg">
</div>

# 4. gewechat_channel 服务的限制
1. gewechat 要求必须搭建服务到**同省**服务器或者电脑里方可正常使用,即登录微信的手机与gewechat服务必须在同一省
Expand Down
65 changes: 65 additions & 0 deletions docs/user-info/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 用户信息对接dify

## 1. 简介

新增用户信息对接dify的能力,会把用户id、用户名称、群聊id、群聊名称信息传递给dify,搭配 [**gewechat_channel**](../gewechat/README.md) 提供的wxid与chatroomid,
可以在dify中识别出每个用户,实现个性化服务。

此功能参考了 [wechat-assistant-pro](https://github.com/leochen-g/wechat-assistant-pro/issues/76)

## 2.快速开始

### 2.1 创建Dify应用

下载提供的 [用户信息.yml](../../dsl/用户信息.yml) 文件,在Dify中通过导入DSL文件的方式创建应用,如下图所示:

<div align="center">
<img width="700" src="./import-user-info-dsl.jpg">
</div>

### 2.2 添加用户信息变量

如下图所示,在刚刚创建的应用中,已经预先创建了`user_id``user_name``room_id``room_name`变量,在工作流中可以使用这些变量来唯一标识用户或获取用户信息,从而实现对不同用户的个性化服务。

<div align="center">
<img width="700" src="./user-info-fields.jpg">
</div>

你可以在自己创建的工作流中添加上述4个变量,但是在配置变量时,需要注意:
1. 保证变量名一致
2. 最大长度设置大一些,推荐128
3. **取消**勾选必填

### 2.3 发布应用,启动dify-on-wechat服务

发布此应用,并获取dify_api_base、dify_api_key,详情请参考[**手摸手教你把 Dify 接入微信生态**](https://docs.dify.ai/v/zh-hans/learn-more/use-cases/dify-on-wechat),不再赘述。

在dify-on-wechat的config.json中需要配置以下dify相关参数:

```bash
{
"dify_api_base": "https://api.dify.ai/v1", # dify base url
"dify_api_key": "app-xxx", # dify api key
"dify_app_type": "chatbot", # dify应用类型,对应聊天助手
"channel_type": "gewechat", # 通道类型设置为gewechat
"model": "dify", # 模型名称设置为dify
"single_chat_prefix": [""], # 私聊触发前缀
"single_chat_reply_prefix": "", # 私聊回复前缀
"group_chat_prefix": ["@bot"], # 群聊触发前缀
"group_name_white_list": ["ALL_GROUP"], # 允许响应的群组
}
```

上述的channel_type使用了 **gewechat**,gewechat_channel 的使用方法请参考 [**gewechat_channel**](../gewechat/README.md)

### 2.4 接收用户信息测试

分别测试私聊和群聊,在下图中可以看到能够正常获取到用户信息并返回。

<div align="center">
<img width="700" src="./user-info-single.jpg">
</div>

<div align="center">
<img width="700" src="./user-info-room.jpg">
</div>
Binary file added docs/user-info/import-user-info-dsl.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/user-info/user-info-fields.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/user-info/user-info-room.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/user-info/user-info-single.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 51a2517

Please sign in to comment.