Dify接入微信生态

如果希望将Dify接入微信或者钉钉的生态

需要使用一些第三方的框架项目,这类项目一般作为中间件,一方面接受来自微信的消息,另一方面调用Dify进行返回

这里主要涉及两个框架,分别是Dify on WeChat和LangBot

首先是Dify on WeChat

https://github.com/hanfangyuan4396/dify-on-wechat

是一个基于https://github.com/zhayujie/chatgpt-on-wechat 开发的框架

额外对接了Dify,学习和上手难度不高。

现在支持接入 个人微信 企业微信 企业服务号,企业微信个人号

这里以本地测试为例,安装Dify on WeChat

  1. 下载项目代码

git clone https://github.com/hanfangyuan4396/dify-on-wechat

cd dify-on-wechat/

  1. 安装依赖

pip3 install -r requirements.txt

pip3 install -r requirements-optional.txt

之后在项目根目录创建config.json文件

以如下的方式配置wx即可

{

“dify_api_base”: “https://api.dify.ai/v1”,

“dify_api_key”: “app-xxx”,

“dify_app_type”: “chatbot”,

“channel_type”: “gewechat”,

“gewechat_token”: “”, # 首次登录可留空,自动获取

“gewechat_app_id”: “”, # 首次登录可留空,自动获取

“gewechat_base_url”: “http://本机ip:2531/v2/api”, # gewechat服务API地址

“gewechat_callback_url”: “http://本机ip:9919/v2/api/callback/collect”, # 回调地址

“gewechat_download_url”: “http://本机ip:2532/download” # 文件下载地址

“model”: “dify”,

“single_chat_prefix”: [“”],

“single_chat_reply_prefix”: “”,

“group_chat_prefix”: [“@bot”],

“group_name_white_list”: [“ALL_GROUP”]

}

其中主要涉及的就是dify_app_type 可选为chatbot,agent,workflow三种。

之后安装gewechat服务来运行个人微信

# 从阿里云镜像仓库拉取(国内)

docker pull registry.cn-chengdu.aliyuncs.com/tu1h/wechotd:alpine

docker tag registry.cn-chengdu.aliyuncs.com/tu1h/wechotd:alpine gewe

# 创建数据目录并启动服务

mkdir -p gewechat/data

docker run -itd -v ./gewechat/data:/root/temp -p 2531:2531 -p 2532:2532 –restart=always –name=gewe gewe

最后python3 app.py运行即可。

通过扫码进行wx的运行。

另一个则是LangBot来及进行接入

LangBot是一个非常成熟的中间件

支持企业微信机器人,个人微信,微信公众号等

需要先行部署一下

https://docs.langbot.app/deploy/langbot/manual.html

# 克隆主仓库

git clone https://github.com/RockChinQ/LangBot

cd LangBot

# 构建前端,需要 NodeJS >= 22

cd web

npm install && npm run build

cd ..

python -m venv venv

source venv/bin/activate

pip install -r requirements.txt

python main.py

其次是设置连接消息平台

这里以企业微信机器人为例

这里在企业微信管理后台。进行机器人信息的创建

其中设置回调地址

查看机器人的Secret

最终配置完成获取到企业ID 机器人Secret 通讯录同步Secret

写入到data/config/platform.json之中

企业微信的格式为

{

“adapter”: “wecom”,

“enable”: false,

“host”: “0.0.0.0”,

“port”: 2290,

“corpid”: “”,

“secret”: “”,

“token”: “”,

“EncodingAESKey”: “”,

“contacts_secret”: “”

},

之后还需要设置Dify的相关地址

在data/config/provider.json中填写Dify相关信息即可。

首先设置runner为dify-service-api

其次是设置dify-serivce-api

“dify-service-api”: {

“base-url”: “https://api.dify.ai/v1”,

“app-type”: “chat”,

“options”: {

“convert-thinking-tips”: “original”

},

“chat”: {

“api-key”: “app-1234567890”,

“timeout”: 120

},

“agent”: {

“api-key”: “app-1234567890”,

“timeout”: 120

},

“workflow”: {

“api-key”: “app-1234567890”,

“output-key”: “summary”,

“timeout”: 120

}

}

这里的chat agent workflow三者启用哪一个根据上方的app_type决定。

最后运行python main.py即可。

发表评论