Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 自定义命令关键词配置 #43

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# WeChatter

</div>

<div align="center">

[![CI/CD](https://github.com/Cassius0924/WeChatter/actions/workflows/test.yml/badge.svg)](https://github.com/Cassius0924/WeChatter/actions/workflows/test.yml)
Expand Down Expand Up @@ -173,7 +171,7 @@ python3 main.py
| 配置项 | 子项 | 解释 | 备注 |
| --- | --- | --- | --- |
| `message_forwarding_enabled` | | 功能开关,是否开启消息转发 | 默认为 `False` |
| `message_forwarding_rules` | | 消息规则列表,每个规则包含三个字段:`froms`, `to_persons` 和 `to_groups` | 规则是由字典组成的JSON列表,最后的 `]` 不能单独一行 |
| `message_forwarding_rule_list` | | 消息规则列表,每个规则包含三个字段:`froms`, `to_persons` 和 `to_groups` | 规则是由字典组成的JSON列表,最后的 `]` 不能单独一行 |
| ➤➤➤ | `froms` | 消息转发来源列表,即消息发送者 | 可以填多个用户名称或群名称 |
| ➤➤➤ | `to_persons` | 消息转发目标用户列表,即消息接收用户 | 可以填多个用户名称或为空列表 |
| ➤➤➤ | `to_groups` | 消息转发目标群列表,即消息接收群 | 可以填多个群名称或为空列表 |
Expand All @@ -183,9 +181,17 @@ python3 main.py
| 配置项 | 解释 | 备注 |
| --- | --- | --- |
| `weather_cron_enabled` | 功能开关,是否开启定时天气推送 | 默认为 `False` |
| `weather_cron_rules` | 推送规则列表,每个规则包含两个字段:`cron` 和 `tasks` | 规则是由字典组成的JSON列表 |
| `weather_cron_rule_list` | 推送规则列表,每个规则包含两个字段:`cron` 和 `tasks` | |

关于 `cron` 和 `tasks` 的配置见[天气预报定时任务配置详细](docs/weather_cron_config_detail.md)

### ⚙️ Custom Command Key 配置

| 配置项 | 解释 | 备注 |
| --- | --- | --- |
| `custom_command_key_dict` | 自定义命令关键词字典,格式为 `command: [key1, key2, ...]`, 其中 `command` 为命令名称,`key1` 和 `key2` 为自定义命令关键词 | |

关于`cron`和`tasks`的配置见[Weather Cron 配置详细](docs/config_detail.md#weather-cron-配置详细)
关于命令名称可选值详见[自定义命令关键词配置详细](docs/custom_command_key_config_detail.md)

## 插件化

Expand Down
17 changes: 15 additions & 2 deletions config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ message_forwarding_enabled = True
# froms: 消息转发来源列表,即消息发送者(们)的微信名称(用户或群)
# to_persons: 消息转发目标用户列表,即消息接收者(们)的微信名称(仅用户)
# to_groups: 消息转发目标群列表,即(多个)消息接收群名称(仅群)
message_forwarding_rules = [ {
message_forwarding_rule_list = [ {
"froms": ["SenderName", "SenderGroupName"],
"to_persons": ["ReceiverName1", "ReceiverName2"],
"to_groups": ["ReceiverGroupName"]
Expand All @@ -86,7 +86,7 @@ weather_cron_enabled = True
# to_persons: 消息接收者(们)的微信名称(仅用户)
# to_groups: 消息接收群名称(仅群)
# city: 城市名称
weather_cron_rules = [ {
weather_cron_rule_list = [ {
"cron": {
"year": "*",
"month": "*",
Expand All @@ -110,3 +110,16 @@ weather_cron_rules = [ {
"to_groups": []
} ]
} ]


[custom-command-key]
# 自定义命令关键词字典
# 格式:<命令名称>: [<关键字1>, <关键字2>, ...]
# 命令名称列表查看:
# - 本地文档:docs/custom_command_key_config_detail.md
# - 在线文档:https://github.com/Cassius0924/WeChatter/blob/master/docs/custom_command_key_config_detail.md
custom_command_key_dict = {
"gpt4": [">"],
"bili-hot": ["bh"],
"weather": ["w", "温度"]
}
45 changes: 45 additions & 0 deletions docs/custom_command_key_config_detail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ⚙️ 自定义命令关键词配置详细

本篇文档仅包含配置文件中自定义命令关键词配置的详细说明,更多配置请查看项目[自述文件](../README.md#配置文件)

## 所有命令名称

- `help`: 查看命令帮助
- `weather`: 查询天气预报
- `weibo-hot`: 获取微博热搜
- `zhihu-hot`: 获取知乎热搜
- `bili-hot`: 获取 Bilibili 热搜
- `douyin-hot`: 获取抖音热搜
- `word`: 单词/词语翻译
- People Daily
- `people-daily`: 获取人民日报 PDF
- `people-daily-url`: 获取人民日报 URL
- TODO
- `todo`: 添加待办事项
- `todo-remove`: 删除待办事项
- `food-calories`: 获取食物热量
- `today-in-history`: 获取历史上的今天
- `github-trending`: 获取 GitHub 趋势
- `qrcode`: 生成二维码
- `pai-post`: 获取少数派早报
- GPT3.5
- `gpt`: 继续 GPT3.5 问答
- `gpt-chats`: 查看 GPT3.5 对话
- `gpt-record`: 查看 GPT3.5 记录
- `gpt-continue`: 继续 GPT3.5 继续
- GPT4
- `gpt4`: 进行 GPT4 问答
- `gpt4-chats`: 查看 GPT4 对话记录
- `gpt4-record`: 查看 GPT4 聊天记录
- `gpt4-continue`: 继续 GPT4 对话

## 自定义命令关键词配置

- **[示例一]** 为 `gpt4` 命令添加自定义命令关键词 `>`,为 `weather` 命令添加自定义命令关键词 `tq`, `气温`

```ini
custom_command_key_dict = {
"gpt4": [">"],
"weather": ["tq", "气温"]
}
```
22 changes: 11 additions & 11 deletions docs/config_detail.md → docs/weather_cron_config_detail.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ⚙️ 配置说明
# ⚙️ 天气预报定时任务配置详细

本篇文档仅包含配置文件中部分复杂配置的详细说明,更多配置请查看项目[自述文件](../README.md#配置文件)
本篇文档仅包含配置文件中天气预报定时任务配置的详细说明,更多配置请查看项目[自述文件](../README.md#配置文件)

## Weather Cron 配置详细
## 天气预报定时任务配置详细

- `weather_cron_enabled`: 功能开关,是否开启定时天气推送
- `weather_cron_rules`: 推送规则列表,每个规则包含两个字段:`cron` 和 `tasks`
- `weather_cron_rule_list`: 推送规则列表,每个规则包含两个字段:`cron` 和 `tasks`
- `cron`: 定时任务的时间设置,包含以下字段:
- `year`: 4位数年份,可以是任何值或者特定的年份
- `month`: 月份,可以是1-12的任何值,或者特定的月份(支持英文月份缩写 jan - dec)
Expand All @@ -31,20 +31,20 @@
| `*/a` | 任何字段 | 从最小值开始,每隔a个值触发 |
| `a-b` | 任何字段 | 在a-b范围内的任何值上触发(a必须小于b) |
| `a-b/c` | 任何字段 | 在a-b范围内,每隔c个值触发 |
| `xth y` | `day`字段 | 在月份中第x次出现的工作日y上触发 |
| `last x` | `day`字段 | 在月份中最后一次出现的工作日x上触发 |
| `last` | `day`字段 | 在月份的最后一天触发 |
| `xth y` | `day` 字段 | 在月份中第x次出现的工作日y上触发 |
| `last x` | `day` 字段 | 在月份中最后一次出现的工作日x上触发 |
| `last` | `day` 字段 | 在月份的最后一天触发 |
| `x,y,z` | 任何字段 | 在任何匹配的表达式上触发;可以组合上述表达式中的任何数量 |

### 配置示例
## 天气预报定时任务配置示例

<details>
<summary>
<b>[示例一]</b> 每天早上7点推送北京天气给张三、文件传输助手和家人群,推送广州天气给李四
</summary>

```ini
weather_cron_rules = [ {
weather_cron_rule_list = [ {
"cron": {
"year": "*",
"month": "*",
Expand Down Expand Up @@ -78,7 +78,7 @@ weather_cron_rules = [ {
</summary>

```ini
weather_cron_rules = [ {
weather_cron_rule_list = [ {
"cron": {
"year": "*",
"month": "*",
Expand Down Expand Up @@ -127,7 +127,7 @@ weather_cron_rules = [ {
</summary>

```ini
weather_cron_rules = [ {
weather_cron_rule_list = [ {
"cron": {
"year": "*",
"month": "1",
Expand Down
4 changes: 2 additions & 2 deletions wechatter/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import wechatter.app.routers as routers
import wechatter.config as config
from wechatter.config.parsers import parse_weather_cron_rules
from wechatter.config.parsers import parse_weather_cron_rule_list
from wechatter.scheduler import Scheduler

app = FastAPI()
Expand All @@ -14,7 +14,7 @@


if config.weather_cron_enabled:
cron_tasks = parse_weather_cron_rules(config.weather_cron_rules)
cron_tasks = parse_weather_cron_rule_list(config.weather_cron_rule_list)

Scheduler.add_cron_tasks(cron_tasks)

Expand Down
2 changes: 1 addition & 1 deletion wechatter/app/routers/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def recv_wechat_msg(
print("==" * 20)

if config.message_forwarding_enabled:
MessageForwarder(config.message_forwarding_rules).forward_message(message)
MessageForwarder(config.message_forwarding_rule_list).forward_message(message)

# 传入命令字典,构造消息处理器
message_handler = MessageHandler(commands)
Expand Down
3 changes: 2 additions & 1 deletion wechatter/commands/_commands/help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 获取命令帮助消息
import wechatter.config as config
from wechatter.commands import commands
from wechatter.commands.handlers import command
from wechatter.models.message import SendTo
Expand Down Expand Up @@ -27,6 +28,6 @@ def get_help_msg() -> str:
continue
cmd_msg = ""
for key in value["keys"]:
cmd_msg += "/" + key + "\n"
cmd_msg += config.command_prefix + key + "\n"
help_msg += cmd_msg + "-->「" + value["desc"] + "」\n\n"
return help_msg
4 changes: 2 additions & 2 deletions wechatter/commands/_commands/people_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@command(
command="people",
command="people-daily",
keys=["人民日报", "people", "people-daily"],
desc="获取人民日报。",
)
Expand Down Expand Up @@ -35,7 +35,7 @@ def people_daily_command_handler(to: SendTo, message: str = "") -> None:


@command(
command="people-url",
command="people-daily-url",
keys=["人民日报链接", "people-url", "people-daily-url"],
desc="获取人民日报url。",
)
Expand Down
4 changes: 2 additions & 2 deletions wechatter/commands/_commands/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def todo_command_handler(to: SendTo, message: str = "") -> None:


@command(
command="remove-todo",
keys=["删除待办事项", "remove-todo", "rmtd"],
command="todo-remove",
keys=["删除待办事项", "todo-remove", "rmtd"],
desc="删除待办事项。",
)
def remove_todo_command_handler(to: SendTo, message: str = "") -> None:
Expand Down
6 changes: 6 additions & 0 deletions wechatter/commands/handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List

import wechatter.config as config

commands = {}
"""
存储所有命令消息和其信息的及其处理函数的字典。
Expand All @@ -17,6 +19,10 @@ def command(command: str, keys: List[str], desc: str):

def decorator(func):
commands[command] = {}
# 自定义命令关键词
if config.custom_command_key_dict.get(command, None):
keys.extend(config.custom_command_key_dict[command])

commands[command]["keys"] = keys
commands[command]["desc"] = desc
commands[command]["handler"] = func
Expand Down
11 changes: 8 additions & 3 deletions wechatter/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@
message_forwarding_enabled = config_reader.getbool(
"message-forwarding", "message_forwarding_enabled"
)
message_forwarding_rules = config_reader.getlist(
"message-forwarding", "message_forwarding_rules"
message_forwarding_rule_list = config_reader.getlist(
"message-forwarding", "message_forwarding_rule_list"
)

# weather-cron 配置
weather_cron_enabled = config_reader.getbool("weather-cron", "weather_cron_enabled")
weather_cron_rules = config_reader.getlist("weather-cron", "weather_cron_rules")
weather_cron_rule_list = config_reader.getlist("weather-cron", "weather_cron_rule_list")

# custom-command-key 配置
custom_command_key_dict = config_reader.getdict(
"custom-command-key", "custom_command_key_dict"
)

logger.info(config_reader.config_dict)

Expand Down
9 changes: 9 additions & 0 deletions wechatter/config/config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def getlist(self, section: str, option: str) -> List:
"""
return json.loads(self.__cp.get(section, option))

def getdict(self, section: str, option: str) -> dict:
"""
以 dict 的形式获取配置文件中的值
:param section: 配置文件中的section
:param option: 配置文件中的配置项
:return: 配置文件中的值(dict)
"""
return json.loads(self.__cp.get(section, option))

@property
def config_dict(self) -> dict:
"""
Expand Down
4 changes: 2 additions & 2 deletions wechatter/config/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .weather_cron import parse_weather_cron_rules
from .weather_cron import parse_weather_cron_rule_list

__all__ = ["parse_weather_cron_rules"]
__all__ = ["parse_weather_cron_rule_list"]
8 changes: 4 additions & 4 deletions wechatter/config/parsers/weather_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
from wechatter.sender import Sender


def parse_weather_cron_rules(
weather_cron_rules: List,
def parse_weather_cron_rule_list(
weather_cron_rule_list: List,
) -> List[CronTask]:
"""
解析天气定时任务规则
:param weather_cron_rules: 天气定时任务规则
:param weather_cron_rule_list: 天气定时任务规则
:return cron_tasks: 天气定时任务列表
"""
cron_tasks = []
for rule in weather_cron_rules:
for rule in weather_cron_rule_list:
cron = rule["cron"]
tasks = rule["tasks"]
trigger = CronTrigger(
Expand Down
6 changes: 3 additions & 3 deletions wechatter/message_forwarder/message_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class MessageForwarder:
"""消息转发器类"""

def __init__(self, rules: List):
self.rules = rules
def __init__(self, rule_list: List):
self.rule_list = rule_list

def forward_message(self, message: Message):
"""消息转发"""
Expand All @@ -25,7 +25,7 @@ def forward_message(self, message: Message):
# TODO: 转发文件

# 判断消息是否符合转发规则
for rule in self.rules:
for rule in self.rule_list:
# 判断消息来源是否符合转发规则
if from_name in rule["froms"]:
# 构造转发消息
Expand Down
Loading
Loading