use serde_json::{json, Value};
use super::path_seg;
use crate::error::ImError;
use crate::outbound::registry::require_str;
bot_call_cmd!(
read BotAgentInfoCommand,
BOT_AGENT_INFO_REG,
"im_bot_agent_info",
|_args, _cmd| { Ok(("bot-agent/info", json!({}))) }
);
bot_call_cmd!(
BotAgentSendDirectCommand,
BOT_AGENT_SEND_DIRECT_REG,
"im_bot_agent_send_direct",
|args, cmd| {
let mut b = send_message_common(args, cmd)?;
b["userId"] = json!(require_str(args, "user_id", cmd)?);
Ok(("bot-agent/messages/direct", b))
}
);
bot_call_cmd!(
BotAgentSendChannelCommand,
BOT_AGENT_SEND_CHANNEL_REG,
"im_bot_agent_send_channel",
|args, cmd| {
let mut b = send_message_common(args, cmd)?;
b["channelId"] = json!(require_str(args, "channel_id", cmd)?);
Ok(("bot-agent/messages/channel", b))
}
);
bot_call_dyn_cmd!(
read BotAgentTeamMembersCommand,
BOT_AGENT_TEAM_MEMBERS_REG,
"im_bot_agent_team_members",
"bot-agent/teams",
|args, cmd| {
let team_id = path_seg(args, "team_id", cmd)?;
let page = args.get("page").and_then(Value::as_i64).unwrap_or(0);
let per_page = args.get("per_page").and_then(Value::as_i64).unwrap_or(0);
Ok(format!(
"bot-agent/teams/{team_id}/members?page={page}&per_page={per_page}"
))
},
|_args, _cmd| Ok(json!({}))
);
bot_call_dyn_cmd!(
read BotAgentGetUserCommand,
BOT_AGENT_GET_USER_REG,
"im_bot_agent_get_user",
"bot-agent/teams",
|args, cmd| Ok(format!(
"bot-agent/teams/{}/users/{}",
path_seg(args, "team_id", cmd)?,
path_seg(args, "user_id", cmd)?
)),
|_args, _cmd| Ok(json!({}))
);
bot_call_dyn_cmd!(
read BotAgentTeamChannelCommand,
BOT_AGENT_TEAM_CHANNEL_REG,
"im_bot_agent_team_channel",
"bot-agent/teams",
|args, cmd| Ok(format!(
"bot-agent/teams/{}/channel",
path_seg(args, "team_id", cmd)?
)),
|_args, _cmd| Ok(json!({}))
);
fn send_message_common(args: &Value, cmd: &str) -> Result<Value, ImError> {
let team_id = require_str(args, "team_id", cmd)?;
let message = require_str(args, "message", cmd)?;
let mut b = json!({ "teamId": team_id, "message": message });
if let Some(create_channel) = args.get("create_channel").and_then(Value::as_bool) {
b["createChannel"] = json!(create_channel);
}
if let Some(props) = args.get("props").filter(|v| v.is_object()) {
b["props"] = props.clone();
}
if let Some(snapshot) = args.get("user_snapshot").filter(|v| v.is_object()) {
b["userSnapshot"] = snapshot.clone();
}
Ok(b)
}