slack_morphism/socket_mode/
callbacks.rs

1use crate::errors::*;
2use crate::events::*;
3use crate::listener::{SlackClientEventsUserState, UserCallbackFunction};
4use crate::models::events::{SlackCommandEvent, SlackCommandEventResponse};
5use crate::models::socket_mode::SlackSocketModeHelloEvent;
6use crate::{AnyStdResult, SlackClient, SlackClientHttpConnector, UserCallbackResult};
7use futures::future::BoxFuture;
8use std::future::Future;
9use std::sync::Arc;
10use tracing::*;
11
12pub trait SlackSocketModeListenerCallback<SCHC, RQ, RS>
13where
14    SCHC: SlackClientHttpConnector + Send + Sync,
15    RQ: Send + Sync + 'static,
16    RS: Send + Sync + 'static,
17{
18    fn call(
19        &self,
20        ev: RQ,
21        client: Arc<SlackClient<SCHC>>,
22        state_storage: SlackClientEventsUserState,
23    ) -> BoxFuture<'static, RS>;
24}
25
26impl<T, F, SCHC, RQ, RS> SlackSocketModeListenerCallback<SCHC, RQ, RS> for T
27where
28    T: Send + Sync + Fn(RQ, Arc<SlackClient<SCHC>>, SlackClientEventsUserState) -> F,
29    F: Future<Output = RS> + Send + 'static,
30    SCHC: SlackClientHttpConnector + Send + Sync,
31    RQ: Send + Sync + 'static,
32    RS: Send + Sync + 'static,
33{
34    fn call(
35        &self,
36        ev: RQ,
37        client: Arc<SlackClient<SCHC>>,
38        state_storage: SlackClientEventsUserState,
39    ) -> BoxFuture<'static, RS> {
40        Box::pin(self(ev, client, state_storage))
41    }
42}
43
44pub struct SlackSocketModeListenerCallbacks<SCHC>
45where
46    SCHC: SlackClientHttpConnector + Send + Sync,
47{
48    pub hello_callback:
49        Box<dyn SlackSocketModeListenerCallback<SCHC, SlackSocketModeHelloEvent, ()> + Send + Sync>,
50
51    pub command_callback: Box<
52        dyn SlackSocketModeListenerCallback<
53                SCHC,
54                SlackCommandEvent,
55                UserCallbackResult<SlackCommandEventResponse>,
56            > + Send
57            + Sync,
58    >,
59    pub interaction_callback: Box<
60        dyn SlackSocketModeListenerCallback<SCHC, SlackInteractionEvent, UserCallbackResult<()>>
61            + Send
62            + Sync,
63    >,
64    pub push_events_callback: Box<
65        dyn SlackSocketModeListenerCallback<SCHC, SlackPushEventCallback, UserCallbackResult<()>>
66            + Send
67            + Sync,
68    >,
69}
70
71impl<SCHC> SlackSocketModeListenerCallbacks<SCHC>
72where
73    SCHC: SlackClientHttpConnector + Send + Sync + 'static,
74{
75    pub fn new() -> Self {
76        Self {
77            hello_callback: Box::new(Self::empty_hello_callback),
78            command_callback: Box::new(Self::empty_command_events_callback),
79            interaction_callback: Box::new(Self::empty_interaction_events_callback),
80            push_events_callback: Box::new(Self::empty_push_events_callback),
81        }
82    }
83
84    pub fn with_hello_events<F>(
85        mut self,
86        hello_events_fn: UserCallbackFunction<SlackSocketModeHelloEvent, F, SCHC>,
87    ) -> Self
88    where
89        F: Future<Output = ()> + Send + 'static,
90    {
91        self.hello_callback = Box::new(hello_events_fn);
92        self
93    }
94
95    async fn empty_hello_callback(
96        event: SlackSocketModeHelloEvent,
97        _client: Arc<SlackClient<SCHC>>,
98        _states: SlackClientEventsUserState,
99    ) {
100        debug!("Received Slack hello for socket mode: {:?}", event);
101    }
102
103    pub fn with_command_events<F>(
104        mut self,
105        command_events_fn: UserCallbackFunction<SlackCommandEvent, F, SCHC>,
106    ) -> Self
107    where
108        F: Future<Output = UserCallbackResult<SlackCommandEventResponse>> + Send + 'static,
109    {
110        self.command_callback = Box::new(command_events_fn);
111        self
112    }
113
114    async fn empty_command_events_callback(
115        event: SlackCommandEvent,
116        _client: Arc<SlackClient<SCHC>>,
117        _states: SlackClientEventsUserState,
118    ) -> AnyStdResult<SlackCommandEventResponse> {
119        warn!("No callback is specified for a command event: {:?}", event);
120        Err(Box::new(SlackClientError::SystemError(
121            SlackClientSystemError::new()
122                .with_message("No callback is specified for a command event".to_string()),
123        )))
124    }
125
126    pub fn with_interaction_events<F>(
127        mut self,
128        interaction_events_fn: UserCallbackFunction<SlackInteractionEvent, F, SCHC>,
129    ) -> Self
130    where
131        F: Future<Output = UserCallbackResult<()>> + Send + 'static,
132    {
133        self.interaction_callback = Box::new(interaction_events_fn);
134        self
135    }
136
137    async fn empty_interaction_events_callback(
138        event: SlackInteractionEvent,
139        _client: Arc<SlackClient<SCHC>>,
140        _states: SlackClientEventsUserState,
141    ) -> UserCallbackResult<()> {
142        warn!(
143            "No callback is specified for interactive events: {:?}",
144            event
145        );
146        Err(Box::new(SlackClientError::SystemError(
147            SlackClientSystemError::new()
148                .with_message("No callback is specified for interactive events".to_string()),
149        )))
150    }
151
152    pub fn with_push_events<F>(
153        mut self,
154        push_events_fn: UserCallbackFunction<SlackPushEventCallback, F, SCHC>,
155    ) -> Self
156    where
157        F: Future<Output = UserCallbackResult<()>> + Send + 'static,
158    {
159        self.push_events_callback = Box::new(push_events_fn);
160        self
161    }
162
163    async fn empty_push_events_callback(
164        event: SlackPushEventCallback,
165        _client: Arc<SlackClient<SCHC>>,
166        _states: SlackClientEventsUserState,
167    ) -> UserCallbackResult<()> {
168        warn!("No callback is specified for a push event: {:?}", event);
169
170        Err(Box::new(SlackClientError::SystemError(
171            SlackClientSystemError::new()
172                .with_message("No callback is specified for push events".to_string()),
173        )))
174    }
175}