回答
hamibot.config.json 详细解释
hamibot.config.json 是 Hamibot 脚本的配置文件,用于定义脚本运行时的参数和设置。通过这个配置文件,你可以为脚本创建用户界面表单,使脚本更加灵活和易于使用。
基本结构
hamibot.config.json 是一个 JSON 格式的文件,其基本结构是一个包含表单项的数组:
[
{
"key": "msg",
"type": "input",
"label": "消息内容",
"props": {
"placeholder": "请输入要显示的消息"
}
}
]
配置文件组成
配置文件中的每个表单项包含以下关键属性:
- key:配置项的唯一标识符,用于在脚本中访问该配置值
- type:表单组件类型(如 input、select、switch 等)
- label:表单项的显示名称
- props:组件的属性配置
常用表单类型
Hamibot 支持多种表单组件类型:
- input:文本输入框
- textarea:多行文本输入框
- select:下拉选择框
- switch:开关
- number:数字输入框
- checkbox:复选框
- radio:单选框
- date:日期选择器
- time:时间选择器
- color:颜色选择器
配置文件示例
[
{
"key": "msg",
"type": "input",
"label": "欢迎消息",
"props": {
"placeholder": "请输入欢迎语",
"default": "Hello Hamibot!"
}
},
{
"key": "interval",
"type": "number",
"label": "执行间隔(秒)",
"props": {
"min": 1,
"max": 60,
"default": 5
}
},
{
"key": "enabled",
"type": "switch",
"label": "启用功能",
"props": {
"default": true
}
},
{
"key": "theme",
"type": "select",
"label": "主题颜色",
"props": {
"options": [
{"label": "蓝色", "value": "blue"},
{"label": "绿色", "value": "green"},
{"label": "红色", "value": "red"}
],
"default": "blue"
}
}
]
在脚本中使用配置
配置文件中定义的所有输入值都会被保存在 hamibot.env 对象中,你可以通过解构方式获取:
// 文档地址 https://docs.hamibot.com/
const { msg, interval, enabled, theme } = hamibot.env;
if (enabled) {
toastLog(`消息: ${msg}`);
log(`执行间隔: ${interval}秒`);
log(`主题颜色: ${theme}`);
} else {
toastLog("功能已禁用");
}
配置文件限制
- 配置文件必须是有效的 JSON 数组
- 文件大小不能超过 3MB
- 需要使用
application/json的 MIME type 上传 - 一个脚本最多只能关联一个配置文件
上传配置文件
你可以通过 API 上传配置文件:
// 文档地址 https://docs.hamibot.com/
http.request('https://api.hamibot.com/v1/devscripts/你的脚本ID/files', {
method: 'PUT',
headers: {
authorization: '你的令牌(hmp 开头)',
},
files: {
'data': {
filename: 'config.json',
contentType: 'application/json',
data: JSON.stringify([
{
"key": "msg",
"type": "input",
"label": "消息内容",
"props": {
"default": "✅ Hamibot 机能正常"
}
}
])
}
}
});
通过精心设计配置文件,你可以让脚本更加用户友好,适应不同场景的需求。
ℹ️ 本回答由 AI 生成,内容仅供参考