From cef44fb962fb6e21aa51cddef31c76824bbf90bf Mon Sep 17 00:00:00 2001 From: Cassius0924 <2670226747@qq.com> Date: Wed, 6 Mar 2024 20:23:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20Docker=20=E5=92=8C?= =?UTF-8?q?=20Docker=20Compose=20=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 98 ++++++++++++++++++++++++++++++ Dockerfile | 17 ++++++ README.md | 63 +++++--------------- compose_config.yaml.example | 96 ++++++++++++++++++++++++++++++ config.yaml.example | 2 +- docker-compose.yml | 23 ++++++++ docs/development.md | 115 ++++++++++++++++++++++++++++++++++++ requirements.txt | 2 +- wechatter/init_logger.py | 1 + 9 files changed, 367 insertions(+), 50 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose_config.yaml.example create mode 100644 docker-compose.yml create mode 100644 docs/development.md diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..71b0a92 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,98 @@ +# Git +.git +.gitignore +.gitattributes + + +# CI +.codeclimate.yml +.travis.yml +.taskcluster.yml + +# Docker +docker-compose.yml +Dockerfile +.docker +.dockerignore + +# Byte-compiled / optimized / DLL files +**/__pycache__/ +**/*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +/docs + +# PyBuilder +target/ + +# Virtual environment +.env +.venv/ +venv/ + +# PyCharm +.idea + +# Python mode for VIM +.ropeproject +**/.ropeproject + +# Vim swap files +**/*.swp + +# VS Code +.vscode/ + +.pre-commit-config.yaml +config.yaml.example +config.yaml +compose_config.yaml.example +/.github +/tests +.DS_Store +/.ruff_cache +README.md \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..79e6618 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.9-alpine3.18 +LABEL authors="Cassius0924" + +WORKDIR /wechatter + +ADD . /wechatter + +RUN pip install --no-cache-dir -r requirements.txt + +# 使 loguru 支持颜色输出 +ENV LOGURU_COLORIZE=True +# 设置日志级别 +ENV WECHATTER_LOG_LEVEL=INFO + +EXPOSE 4000 + +CMD ["python3", "-m", "wechatter"] \ No newline at end of file diff --git a/README.md b/README.md index d6da182..19fcb15 100644 --- a/README.md +++ b/README.md @@ -11,78 +11,45 @@ ## 介绍 -一个基于 [wechatbot-webhook](https://github.com/danni-cool/wechatbot-webhook) 的微信机器人💬,支持 GPT 问答、热搜推送、天气预报、消息转发、Webhook提醒等功能。 +一个基于 [wechatbot-webhook](https://github.com/danni-cool/wechatbot-webhook) 的微信机器人💬,支持 GPT 问答、热搜推送、天气预报、消息转发、小游戏、Webhook 提醒等功能。 [![wechatter show](docs/images/wechatter_show.png)](docs/command_show.md) ## 快速开始 -### 运行 wechatbot-webhook +WeChatter 支持 [Docker Compose 部署](#docker-compose-部署)、[Docker 部署](./docs/development.md#docker-部署)和[本地部署](./docs/development.md#本地部署)。 -1. 拉取 Docker 镜像 - -```bash -docker pull dannicool/docker-wechatbot-webhook -``` - -2. 运行 Docker - -```bash -docker run -d \ ---name wxBotWebhook \ --p 3001:3001 \ --e LOGIN_API_TOKEN="" \ --e RECVD_MSG_API="http(s)://<宿主机IP>:<接收消息端口>/receive_msg" \ -dannicool/docker-wechatbot-webhook -``` - -- ``:令牌 -- `<宿主机IP>`:填入 Docker 的宿主机地址。 -- `<接收消息端口>`:设置一个接收消息的端口,默认为 `4000`。 - -3. 登录微信 - -使用下面命令查看 Docker 日志中的微信二维码,扫码登录微信。 - -```bash -docker logs -f wxBotWebhook -``` +> [!TIP] +> 推荐使用 Docker-compose 部署。 -### 启动 WeChatter +### Docker Compose 部署 -1. 下载源代码 +1. 下载 WeChatter 配置文件 ```bash -git clone https://github.com/Cassius0924/WeChatter -cd WeChatter +mkdir WeChatter && cd WeChatter +wget -O compose_config.yaml https://cdn.jsdelivr.net/gh/cassius0924/wechatter@master/compose_config.yaml.example ``` -2. 安装依赖项 +2. 编辑 `compose_config.yaml` 配置文件 ```bash -# 如果需要,可创建虚拟环境... - -pip install -r requirements.txt +vim compose_config.yaml ``` -3. 复制并编辑配置文件 +3. 下载 docker-compose.yml ```bash -cp config.yaml.example config.yaml -vim config.yaml +wget https://cdn.jsdelivr.net/gh/cassius0924/wechatter@master/docker-compose.yml ``` -4. 启动 WeChatter +4. 运行 Docker Compose ```bash -python3 -m wechatter +docker-compose -f docker-compose.yml up -d ``` -5. 测试机器人 - -使用另一个微信给机器人发送 `/help` 指令。 - ## 支持的命令 - [x] GPT 问答(不支持定时任务) @@ -147,7 +114,7 @@ python3 -m wechatter | 配置项 | 解释 | 备注 | | --- | --- | --- | -| `wx_webhook_base_api` | 发送消息的 BaseAPI | 默认为 `localhost:3001`,即 `wxBotWebhook` Docker 的地址 | +| `wx_webhook_base_api` | 发送消息的 BaseAPI | 默认为 `http://localhost:3001`,即 `wxBotWebhook` Docker 的地址。Docker Compose 部署时默认为 `http://wxbotwebhook:3001` | | `wx_webhook_recv_api_path` | 接收消息的接口路径 | 默认为 `/receive_msg`,此路径为 Docker 参数 `RECVD_MSG_API` 的路径 | | `wx_webhook_token` | wxBotWebhook 的 Token | | diff --git a/compose_config.yaml.example b/compose_config.yaml.example new file mode 100644 index 0000000..d6a5ad9 --- /dev/null +++ b/compose_config.yaml.example @@ -0,0 +1,96 @@ +# 微信机器人配置文件 +# 配置说明:https://github.com/Cassius0924/WeChatter?tab=readme-ov-file#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6 + + +# Wechatter +wechatter_port: 4000 + + +# WX Webhook +# Docker 部署时,wx_webhook_base_api 需要配置为 wxbotwebhook,端口需要与 docker-compose.yml 中的端口一致 +wx_webhook_base_api: http://wxbotwebhook:3001 +wx_webhook_recv_api_path: /receive_msg +wx_webhook_token: "wechatter" + + +# Admin +admin_list: [ "文件传输助手", "AdminName" ] +admin_group_list: [ "AdminGroupName" ] +bark_url: your_bark_url + + +# Bot +bot_name: Cassius + + +# Chat +command_prefix: / +need_mentioned: False + + +# LLM +openai_base_api: https://api.openai.com +openai_token: sk_your_openai_token + + +# GitHub Webhook +github_webhook_enabled: True +github_webhook_api_path: /webhook/github +github_webhook_receive_person_list: [ ] +github_webhook_receive_group_list: [ ] + + +# Message Forwarding:消息转发 +message_forwarding_enabled: False +message_forwarding_rule_list: + - from_list: [ "%ALL" ] + from_list_exclude: [ "You" ] + to_person_list: [ "You" ] + to_group_list: [ ] + - from_list: [ "Jay", "Tom" ] + to_person_list: [ "Cassius" ] + to_group_list: [ "Team" ] + + +# 公众号消息提醒 +official_account_reminder_enabled: True +official_account_reminder_rule_list: + - oa_name_list: [ "央视新闻", "人民日报" ] + to_person_list: [ "You" ] + to_group_list: [ "Team" ] + + +# Task Cron:定时任务 +# 配置说明:https://github.com/Cassius0924/WeChatter/blob/master/docs/task_cron_config_detail.md +all_task_cron_enabled: True +task_cron_list: + - task: "每天早上8点发送天气预报和知乎热搜" + enabled: True + cron: + hour: "8" + minute: "0" + second: "0" + timezone: "Asia/Shanghai" + commands: + - cmd: "weather" + args: [ "广州" ] + to_person_list: [ "You" ] + - cmd: "zhihu-hot" + to_group_list: [ "Team" ] + + +# Custom Command Key: 自定义命令关键词 +# 配置说明:https://github.com/Cassius0924/WeChatter/blob/master/docs/custom_command_key_config_detail.md +custom_command_key_dict: + gpt4: [ ">" ] + bili-hot: [ "bh" ] + play: [ "p" ] + weather: [ "w", "温度" ] + + +# Discord Message Forwarding:Discord 消息转发 +discord_message_forwarding_enabled: False +discord_message_forwarding_rule_list: + - from_list: [ "%ALL" ] + from_list_exclude: [ "" ] + webhook_url: "your_discord_webhook_url" diff --git a/config.yaml.example b/config.yaml.example index 24830a3..d2270ee 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -88,7 +88,7 @@ custom_command_key_dict: # Discord Message Forwarding:Discord 消息转发 -discord_message_forwarding_enabled: True +discord_message_forwarding_enabled: False discord_message_forwarding_rule_list: - from_list: [ "%ALL" ] from_list_exclude: [ "" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..eb6e667 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3.8" +services: + wxbotwebhook: + image: dannicool/docker-wechatbot-webhook + container_name: wxbotwebhook + ports: + - "3001:3001" + environment: + - LOGIN_API_TOKEN=wechatter + - ACCEPT_RECVD_MSG_MYSELF=true + - RECVD_MSG_API=http://wechatter:4000/receive_msg + + wechatter: + image: cassius0924/wechatter:v0.5.10 + container_name: wechatter + ports: + - "4000:4000" + volumes: + - ./compose_config.yaml.example:/wechatter/config.yaml + environment: + - WECHATTER_LOG_LEVEL=INFO + depends_on: + - wxbotwebhook diff --git a/docs/development.md b/docs/development.md new file mode 100644 index 0000000..095ae29 --- /dev/null +++ b/docs/development.md @@ -0,0 +1,115 @@ +# 本地部署和 Docker 部署 + +本文档介绍了 WeChatter 的本地部署和 Docker 部署方法。Docker Compose 部署方法请参考[自述文件](../README.md#快速开始)。 + +## 目录 + +- [本地部署](#本地部署) +- [Docker 部署](#docker-部署) + +## 本地部署 + +### 运行 wechatbot-webhook Docker + +1. 拉取 wechatbot-webhook 镜像 + +```bash +docker pull dannicool/docker-wechatbot-webhook +``` + +2. 运行 wechatbot-webhook + +```bash +docker run -d \ +--name wxBotWebhook \ +-p 3001:3001 \ +-e LOGIN_API_TOKEN="" \ +-e RECVD_MSG_API="http(s)://<宿主机IP>:<接收消息端口>/receive_msg" \ +dannicool/docker-wechatbot-webhook +``` + +- ``:令牌 +- `<宿主机IP>`:填入 Docker 的宿主机地址。 +- `<接收消息端口>`:设置一个接收消息的端口,默认为 `4000`。 + +3. 登录微信 + +使用下面命令查看 wechatbot-webhook 日志中的微信登录二维码,扫码登录微信。 + +或访问 `http://localhost:3001/login?token=` 查看微信登录二维码。 + +```bash +docker logs -f wxBotWebhook +``` + +### 启动 WeChatter + +1. 下载源代码 + +```bash +git clone https://github.com/Cassius0924/WeChatter +cd WeChatter +``` + +2. 安装依赖项 + +```bash +# 如果需要,可创建虚拟环境... + +pip install -r requirements.txt +``` + +3. 复制并编辑配置文件 + +```bash +cp config.yaml.example config.yaml +vim config.yaml +``` + +4. 启动 WeChatter + +```bash +python3 -m wechatter +``` + +5. 测试机器人 + +使用另一个微信给机器人发送 `/help` 指令。 + +### Docker 部署 + +### 运行 wechatbot-webhook Docker + +Docker 部署同上述[本地部署](#本地部署),需要运行 `wechatbot-webhook` Docker 容器。 + +### 运行 WeChatter Docker + +1. 拉取 WeChatter 镜像 + +```bash +docker pull cassius0924/wechatter +``` + +2. 下载 WeChatter 配置文件 + +```bash +mkdir WeChatter && cd WeChatter +wget -O config.yaml https://cdn.jsdelivr.net/gh/cassius0924/wechatter@master/config.yaml.exmaple +``` + +3. 编辑配置文件 + +```bash +vim config.yaml +``` + +4. 运行 WeChatter + +```bash +docker run -d \ +--name WeChatter \ +-p 4000:4000 \ +-v ./config.yaml:/wechatter/config.yaml \ +-e WECHATTER_LOG_LEVEL=INFO \ +cassius0924/wechatter +``` diff --git a/requirements.txt b/requirements.txt index 7e0d901..70516c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,4 @@ loguru==0.7.2 tenacity==8.2.3 pydantic==2.5.3 sqlalchemy==2.0.25 -PyYAML==6.0 \ No newline at end of file +PyYAML~=6.0 \ No newline at end of file diff --git a/wechatter/init_logger.py b/wechatter/init_logger.py index d7b7d35..8d922d7 100644 --- a/wechatter/init_logger.py +++ b/wechatter/init_logger.py @@ -71,4 +71,5 @@ def init_logger(log_level: str = ""): rotation="00:00", encoding="utf-8", format=LOGURU_FORMAT, + colorize=True, )