Skip to main content

slack_morphism/api/
agents.rs

1//!
2//! Support for Slack agents.sessions.* methods
3//!
4
5use rsb_derive::Builder;
6use serde::{Deserialize, Serialize};
7use serde_with::skip_serializing_none;
8use url::Url;
9
10use crate::models::*;
11use crate::ratectl::*;
12use crate::SlackClientSession;
13use crate::{ClientResult, SlackClientHttpConnector};
14
15impl<'a, SCHC> SlackClientSession<'a, SCHC>
16where
17    SCHC: SlackClientHttpConnector + Send,
18{
19    ///
20    /// https://docs.slack.dev/reference/methods/agents.sessions.setStatus
21    ///
22    pub async fn agents_sessions_set_status(
23        &self,
24        req: &SlackApiAgentsSessionsSetStatusRequest,
25    ) -> ClientResult<SlackApiAgentsSessionsSetStatusResponse> {
26        self.http_session_api
27            .http_post(
28                "agents.sessions.setStatus",
29                req,
30                Some(&SLACK_TIER3_METHOD_CONFIG),
31            )
32            .await
33    }
34
35    ///
36    /// https://docs.slack.dev/reference/methods/agents.sessions.rename
37    ///
38    pub async fn agents_sessions_rename(
39        &self,
40        req: &SlackApiAgentsSessionsRenameRequest,
41    ) -> ClientResult<SlackApiAgentsSessionsRenameResponse> {
42        self.http_session_api
43            .http_post(
44                "agents.sessions.rename",
45                req,
46                Some(&SLACK_TIER3_METHOD_CONFIG),
47            )
48            .await
49    }
50}
51
52#[skip_serializing_none]
53#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
54pub struct SlackApiAgentsSessionsSetStatusRequest {
55    pub status: SlackAgentSessionStatus,
56    pub channel_id: Option<SlackChannelId>,
57    pub thread_ts: Option<SlackTs>,
58    pub title: Option<String>,
59    pub initiator_user_id: Option<SlackUserId>,
60    pub icon_emoji: Option<SlackEmoji>,
61    pub icon_url: Option<Url>,
62    pub username: Option<String>,
63}
64
65#[skip_serializing_none]
66#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
67pub struct SlackApiAgentsSessionsSetStatusResponse {
68    pub status: Option<SlackAgentSessionStatus>,
69    pub agent_status: Option<SlackAgentSessionStatus>,
70    pub title: Option<String>,
71}
72
73#[skip_serializing_none]
74#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
75pub struct SlackApiAgentsSessionsRenameRequest {
76    /// Required together with `thread_ts` for thread sessions in DMs/channels;
77    /// must be omitted for session channels.
78    pub channel_id: Option<SlackChannelId>,
79    /// See `channel_id`.
80    pub thread_ts: Option<SlackTs>,
81    pub title: String,
82}
83
84#[skip_serializing_none]
85#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
86pub struct SlackApiAgentsSessionsRenameResponse {
87    pub title: Option<String>,
88}
89
90/// Agent session status for `agents.sessions.setStatus` and `chat.stopStream`'s `session_status`.
91/// https://docs.slack.dev/reference/methods/agents.sessions.setStatus#arg_status
92#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
93#[serde(rename_all = "snake_case")]
94pub enum SlackAgentSessionStatus {
95    Active,
96    Processing,
97    Suspended,
98    Closed,
99    /// A value this crate does not model yet, carried verbatim so a new status
100    /// does not fail the response.
101    #[serde(untagged)]
102    Other(String),
103}
104
105#[cfg(test)]
106mod test {
107    use super::*;
108
109    #[test]
110    fn test_slack_api_agents_session_status_round_trip() {
111        let known: SlackAgentSessionStatus = serde_json::from_str(r#""processing""#).unwrap();
112        assert_eq!(known, SlackAgentSessionStatus::Processing);
113        assert_eq!(serde_json::to_string(&known).unwrap(), r#""processing""#);
114
115        let other: SlackAgentSessionStatus = serde_json::from_str(r#""something_new""#).unwrap();
116        assert_eq!(
117            other,
118            SlackAgentSessionStatus::Other("something_new".into())
119        );
120        assert_eq!(serde_json::to_string(&other).unwrap(), r#""something_new""#);
121    }
122}