use serde_json::{json, Value};
use crate::error::ImError;
use crate::outbound::registry::require_str;
fn str_array(args: &Value, key: &str) -> Option<Value> {
args.get(key)
.filter(|v| v.as_array().is_some_and(|a| a.iter().all(Value::is_string)))
.cloned()
}
bot_cmd!(
SetWebhookConfigCommand,
SET_WEBHOOK_CONFIG_REG,
"im_webhook_config_set",
|args, cmd| {
let target_url = require_str(args, "target_url", cmd)?;
let secret = require_str(args, "secret", cmd)?;
let trigger_mode = require_str(args, "trigger_mode", cmd)?;
let bot_user_ids = str_array(args, "bot_user_ids")
.ok_or_else(|| ImError::Parse(format!("{cmd}: 缺/坏 bot_user_ids(字符串数组)")))?;
let mut b = json!({
"targetUrl": target_url,
"secret": secret,
"botUserIds": bot_user_ids,
"triggerMode": trigger_mode,
});
if let Some(channel_filter) = str_array(args, "channel_filter") {
b["channelFilter"] = channel_filter;
}
Ok(("webhook/config", b))
}
);
bot_cmd!(
read GetWebhookConfigCommand,
GET_WEBHOOK_CONFIG_REG,
"im_webhook_config_get",
|_args, _cmd| { Ok(("webhook/config", json!({}))) }
);
bot_cmd!(
TestWebhookCommand,
TEST_WEBHOOK_REG,
"im_webhook_test",
|args, _cmd| {
let mut b = json!({});
if let Some(message) = args.get("message").and_then(Value::as_str) {
b["message"] = json!(message);
}
Ok(("webhook/test", b))
}
);