Skip to content

Commit

Permalink
优化定时器 prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
KroMiose committed Apr 3, 2024
1 parent 9130a22 commit f0d71ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
11 changes: 10 additions & 1 deletion docs/extension_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,16 @@ proxy: null

### 简介 <!-- {docsify-ignore} -->

赋予 bot 预定时间的能力,到时自动推送消息
赋予 bot 预定时间的能力,到时自动推送消息触发 bot

### 配置 <!-- {docsify-ignore} -->

请根据下方示例配置中的注释来编辑你的扩展配置

```yml
# 创建时是否禁用推送提醒
no_alert: false
```

<hr />

Expand Down
32 changes: 29 additions & 3 deletions extensions/ext_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
# 扩展的描述信息,用于提示ai理解扩展的功能 *必填* 尽量简短 使用英文更节省token
# 如果bot无法理解扩展的功能,可适当添加使用示例 格式: /#扩展名&参数1&...&参数n#/
"description": 'Push an event at a specified time. The description of the event must be described in detail in the form of "who should do what" (usage in response: /#timer&2023/3/10 10:00:00&it\'s time to ...#/ )',
"description": 'Push an event at a specified time. The description of the event must be described in detail in the form of **"who should do what"** (usage in response: /#timer&2023/3/10 10:00:00&it\'s time to ...#/ In addition, you can also use it to awaken yourself again)',
# 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
"refer_word": [],
# 每次消息回复中最大调用次数,不填则默认为99
Expand All @@ -26,14 +26,16 @@


class CustomExtension(Extension):
async def call(self, arg_dict: dict, _: dict) -> dict:
async def call(self, arg_dict: dict, custom_config: dict) -> dict:
"""当扩展被调用时执行的函数 *由扩展自行实现*
参数:
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值}
"""
# custom_config: dict = self.get_custom_config() # 获取yaml中的配置信息

no_alert = custom_config.get("no_alert", False)

target_time = arg_dict.get("time", None)
event = arg_dict.get("event", None)

Expand All @@ -47,7 +49,7 @@ async def call(self, arg_dict: dict, _: dict) -> dict:

# 返回的信息将会被发送到会话中
return {
"text": f"[ext_timer] 已设置定时器,将在 {int(time_diff)} 秒后触发",
"text": "" if no_alert else f"[ext_timer] 已设置定时器,将在 {time_diff2str(int(time_diff))} 秒后触发",
"timer": time_diff,
"notify": {
"sender": "[system timer]",
Expand All @@ -58,3 +60,27 @@ async def call(self, arg_dict: dict, _: dict) -> dict:

def __init__(self, custom_config: dict):
super().__init__(ext_config.copy(), custom_config)


def time_diff2str(time_diff: int) -> str:
"""将时间差转换为距离时间字符串
Args:
time_diff: float, 时间差
Returns:
str, 距离时间字符串 (天, 小时, 分钟, 秒)
"""
days = int(time_diff // 86400)
hours = int((time_diff % 86400) // 3600)
minutes = int((time_diff % 3600) // 60)
seconds = int(time_diff % 60)
res_str = ""
if days > 0:
res_str += f"{days} 天 "
if hours > 0:
res_str += f"{hours} 小时 "
if minutes > 0:
res_str += f"{minutes} 分 "
if seconds > 0:
res_str += f"{seconds} 秒 "
return res_str.strip()

0 comments on commit f0d71ae

Please sign in to comment.