{"id":19725485,"url":"https://github.com/easychen/botchan","last_synced_at":"2025-04-13T06:34:00.095Z","repository":{"id":199194011,"uuid":"702328589","full_name":"easychen/botchan","owner":"easychen","description":"基于微信测试号的ChatBot，对接OpenAI API","archived":false,"fork":false,"pushed_at":"2023-10-22T09:41:29.000Z","size":267,"stargazers_count":237,"open_issues_count":7,"forks_count":19,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-24T10:17:24.238Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/easychen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-09T05:34:49.000Z","updated_at":"2024-12-04T06:24:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab3a8340-6763-4747-8883-ad866db421e8","html_url":"https://github.com/easychen/botchan","commit_stats":null,"previous_names":["easychen/botchan"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easychen%2Fbotchan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easychen%2Fbotchan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easychen%2Fbotchan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easychen%2Fbotchan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/easychen","download_url":"https://codeload.github.com/easychen/botchan/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674686,"owners_count":21143760,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T23:30:31.827Z","updated_at":"2025-04-13T06:34:00.066Z","avatar_url":"https://github.com/easychen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BotChan · Bot酱\n\nBot酱使用微信测试号对接任何兼容OpenAI规范的API接口（例如[API+](https://apipl.us/?mtm_campaign=botchan)、[API2D](https://api2d.com/r/186008)），实现ChatBot。\n\n![](images/20231009132204.png)\n\n## ⚠️ 特别提示\n\n如果配置好以后，服务器端有日志输出，但微信无反应，日志提示 get access token failed，那么恭喜你遇到微信测试号平台的Bug了。尤其是对于刚开通的账号。可以试试手动退出微信测试号平台，重新扫码登录，查看 appsecret 是不是变了，然后改用新的 appsecret 。\n\n## 安装和配置\n\n### 准备资料\n\n1. [访问并开通微信测试号](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login)\n\n将页面上的 appID、appsecret作为环境变量\n\n```yml\n- MP_APPID=appID\n- MP_APPSECRET=appsecret\n```\n2. 随机编写一个Token\n\n```yml\n- MP_TOKEN=your-random-token\n```\n\n3. 设置 OpenAI 接口\n\n\u003e 如果要使用 OpenAI 官方接口，需要将镜像部署在海外，否则很可能网络不通\n\nOpenAI 配置示例\n\n```yml\n- DEFAULT_API_URL=https://api.openai.com\n- DEFAULT_API_KEY=sk...\n- DEFAULT_MODEL=gpt-3.5-turbo\n```\n\nAPI2D 配置示例\n\n```yml\n- DEFAULT_API_URL=https://oa.api2d.net\n- DEFAULT_API_KEY=fk...\n- DEFAULT_MODEL=gpt-3.5-turbo\n```\n\n4. 设置触发词\n\n可通过以下环境变量设置触发词，设置后只有包含触发词的对话才会触发AI答复。如果测试为空，那么每一句都可以触发AI答复。\n\n```yml\nDEFAULT_API_WORD=\n```\n\n5. 对话命令和默认参数锁定\n\n设置的默认值可以通过 `/setXXX` 命令（针对每个人）进行配置，可通过 `/help` 查看。\n\n但某些参数你可能不希望用户修改，可以用以下环境变量锁定：\n\n```yml\nLOCK_API_URL=true\nLOCK_API_KEY=true\nLOCK_API_WORD=true\nLOCK_MODEL=true\n```\n\n6. 启动服务后（见后），记得将回调地址和Token回填到微信测试号设置页面。\n\n### Docker\n\n\u003e 请按上文介绍修改环境变量\n\n\u003e 确保对 `./data` 有写权限，或者挂到有权限的地方去\n\n```bash\ndocker run -d --name bot \\\n-e MP_APPID= \\\n-e MP_APPSECRET= \\\n-e MP_TOKEN= \\\n-e DEFAULT_API_URL=https://oa.api2d.net \\\n-e DEFAULT_API_WORD= \\  \n-e DEFAULT_API_KEY=fk... \\\n-e LOCK_API_URL=true \\\n-e LOCK_API_KEY=true \\\n-e LOCK_MODEL=true \\\n-e DB_TYPE=json \\\n-v ./data:/data \\\n-p 80:9000 \\\neasychen/botchan:latest\n```\n### Docker-compose\n\n\u003e 请按上文介绍修改环境变量\n\n\u003e 确保对 `./data` 有写权限，或者挂到有权限的地方去\n\n复制 `docker-compose.example.yml` 为 `docker-compose.yml`，并根据需要修改环境变量：\n\n```yml\nversion: '3'\nservices:\n  bot:\n    image: easychen/botchan\n    environment:\n      - MP_APPID=\n      - MP_APPSECRET=\n      - MP_TOKEN=\n      - DEFAULT_API_URL=https://oa.api2d.net\n      - DEFAULT_API_WORD=\n      - DEFAULT_API_KEY=fk...\n      - LOCK_API_URL=true\n      - LOCK_API_KEY=true\n      - LOCK_MODEL=true\n      - DB_TYPE=json\n    volumes:\n      - ./data:/data\n    ports:\n      - 80:9000\n```\n启动服务\n\n```bash\ndocker-compose up -d\n```\n\n\u003e nodejs 安装见最后\n\n## 回填Token和回调地址\n\n启动服务后，我们可以获得`回调地址`。如果IP/域名是`xxx.xxx.xxx.xxx:xxxx`，那么`回调地址`是`http://xxx.xxx.xxx.xxx:xxxx/wechat`\n\n\u003e 如果你在本机测试，需要使用 ngrok 等内网穿透工具创建一个公网可以访问的域名\n\n回到测试号设置页面，点击 `接口配置信息` 后的修改链接，会将 `回调地址` 和 `TOKEN`( 即 `MP_TOKEN` )  填入并保存。\n\n![](images/20231009130318.png)\n\n## 使用\n\n用微信扫描测试号设置页面的二维码，关注测试号以后，可以发送问题。也可以通过 `/setXXX` 命令进行针对个人的配置。\n\n可以通过 `/help` 查看可用命令。\n\n\u003e 你可以将测试号「发送到桌面」，作为快速进入的入口。这样就不用在微信里边到处找了\n\n## 高级配置\n\n### 模型配置\n\n可以进一步指定模型参数优化需要的答复\n\n```yml\n- MAX_TOKEN=1000 #返回的最大token数\n- TEMPERATURE=0.8 #模型的temperature\n- TIMEOUT=180 #超时秒数\n- STREAM=true #是否采用流式传输 \n```\n\n### AZURE 支持\n\nBot酱兼容 Azure 接口，将 DEFAULT_API_URL 指定为 Azure Endpoint（ https://xxx.openai.azure.com ） 即可。\n\nAzure 的 Deployment 需要和 azure 的 model name 保持一致。\n\n![](images/20231010183939.png)\n\n在通过命令或者配置文件指定模型时，请使用 OpenAI 官方的名称，如 `gpt-3.5-turbo`。\n\n### 截断设置\n\n过长文字可能导致内容无法返回，会截断为多条。\n\n\u003e 注意：受微信平台限制，每次互动，只能最多回复五条消息，因此不要将字数设置太低，可能导致超过五条\n\n```yml\n- REPLY_CHUNK_LENGHT=1000 #字数\n```\n\n\n### 使用 NodeJS 架设服务\n\n#### 包依赖安装\n\nnode18\n\n```bash\nyarn add api2d body-parser cors cross-fetch dotenv express express-xml-bodyparser knex mysql2 simple-json-db\n```\n\nnode16.13.1 \n\n```bash\nyarn add api2d body-parser cors cross-fetch dotenv express express-xml-bodyparser knex mysql2@3.0.1 simple-json-db\n```\n\n启动服务 `node index.js` 端口 `9000`\n\n### 将用户设置保存到数据库\n\n默认情况下，用户设置保存在 `/data/db.json` ，这要求运行环境可写；你也可将用户设置保存在 MySQL中。\n\n#### MySQL 表初始化\n\n```sql\nCREATE TABLE `data` (\n  `id` int NOT NULL AUTO_INCREMENT,\n  `key` varchar(255) NOT NULL,\n  `value` text,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `key` (`key`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n```\n\n#### MySQL 环境变量\n\n```yml\n- DB_HOST\n- DB_PORT\n- DB_USER\n- DB_PASSWD\n- DB_NAME\n```\n\n#### 切换用户配置保存模式\n\n```yml\n- DB_TYPE=mysql2\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasychen%2Fbotchan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feasychen%2Fbotchan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasychen%2Fbotchan/lists"}