Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplifies the abstraction for Events API messages #18

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions examples/events_app_mention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;

use async_trait::async_trait;
use slack::chat::post_message::{post_message, PostMessageRequest};
use slack::event_api::event::{Event, EventCallbackType};
use slack::event_api::event::{Event, EventCallback};
use slack::http_client::{default_client, SlackWebAPIClient};
use slack::socket::event::{EventsAPI, HelloEvent};
use slack::socket::socket_mode::{ack, EventHandler, SocketMode, Stream};
Expand Down Expand Up @@ -50,39 +50,37 @@ where
.await
.expect("socket mode ack error.");

match e.payload {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::AppMention {
text,
channel,
ts,
thread_ts,
..
} => {
let (reply_thread_ts, reply_text) = if let Some(thread_ts) = thread_ts {
(thread_ts, "Hello again!".to_string())
} else {
(ts, "Hello!".to_string())
};
match e.payload.event {
EventCallback::AppMention {
text,
channel,
ts,
thread_ts,
..
} => {
let (reply_thread_ts, reply_text) = if let Some(thread_ts) = thread_ts {
(thread_ts, "Hello again!".to_string())
} else {
(ts, "Hello!".to_string())
};

let request = PostMessageRequest {
channel: socket_mode
.option_parameter
.get("SLACK_CHANNEL_ID")
.unwrap()
.to_string(),
thread_ts: Some(reply_thread_ts),
text: Some(reply_text),
..Default::default()
};
let response =
post_message(&socket_mode.api_client, &request, &socket_mode.bot_token)
.await
.expect("post message api error.");
log::info!("post message api response: {:?}", response);
}
_ => {}
},
let request = PostMessageRequest {
channel: socket_mode
.option_parameter
.get("SLACK_CHANNEL_ID")
.unwrap()
.to_string(),
thread_ts: Some(reply_thread_ts),
text: Some(reply_text),
..Default::default()
};
let response =
post_message(&socket_mode.api_client, &request, &socket_mode.bot_token)
.await
.expect("post message api error.");
log::info!("post message api response: {:?}", response);
}
_ => {}
}
}
}
144 changes: 23 additions & 121 deletions src/event_api/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,115 +10,21 @@ use serde_with::skip_serializing_none;
/// [Event API](https://api.slack.com/events?filter=Events)
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(tag = "type")]
pub enum Event {
/// Event callback
#[serde(rename = "event_callback")]
EventCallback(EventCallback),
}

impl Event {
pub fn block_type(&self) -> EventType {
match self {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::AppHomeOpened { .. } => EventType::AppHomeOpened,
EventCallbackType::AppMention { .. } => EventType::AppMention,
EventCallbackType::AppRateLimited { .. } => EventType::AppRateLimited,
EventCallbackType::AppRequested { .. } => EventType::AppRequested,
EventCallbackType::AppUninstalled { .. } => EventType::AppUninstalled,
EventCallbackType::ChannelArchive { .. } => EventType::ChannelArchive,
EventCallbackType::ChannelCreated { .. } => EventType::ChannelCreated,
EventCallbackType::ChannelDeleted { .. } => EventType::ChannelDeleted,
EventCallbackType::ChannelHistoryChanged { .. } => EventType::ChannelHistoryChanged,
EventCallbackType::ChannelIDChanged { .. } => EventType::ChannelIDChanged,
EventCallbackType::ChannelLeft { .. } => EventType::ChannelLeft,
EventCallbackType::ChannelRename { .. } => EventType::ChannelRename,
EventCallbackType::ChannelShared { .. } => EventType::ChannelShared,
EventCallbackType::ChannelUnarchive { .. } => EventType::ChannelUnarchive,
EventCallbackType::ChannelUnshared { .. } => EventType::ChannelUnshared,
EventCallbackType::EmojiChanged { .. } => EventType::EmojiChanged,
EventCallbackType::GridMigrationFinished { .. } => EventType::GridMigrationFinished,
EventCallbackType::GridMigrationStarted { .. } => EventType::GridMigrationStarted,
EventCallbackType::GroupArchive { .. } => EventType::GroupArchive,
EventCallbackType::GroupClose { .. } => EventType::GroupClose,
EventCallbackType::GroupDeleted { .. } => EventType::GroupDeleted,
EventCallbackType::GroupHistoryChanged { .. } => EventType::GroupHistoryChanged,
EventCallbackType::GroupLeft { .. } => EventType::GroupLeft,
EventCallbackType::GroupOpen { .. } => EventType::GroupOpen,
EventCallbackType::GroupRename { .. } => EventType::GroupRename,
EventCallbackType::GroupUnarchive { .. } => EventType::GroupUnarchive,
EventCallbackType::ImClose { .. } => EventType::ImClose,
EventCallbackType::ImCreated { .. } => EventType::ImCreated,
EventCallbackType::ImHistoryChanged { .. } => EventType::ImHistoryChanged,
EventCallbackType::ImOpen { .. } => EventType::ImOpen,
EventCallbackType::InviteRequested { .. } => EventType::InviteRequested,
EventCallbackType::LinkShared => EventType::LinkShared,
EventCallbackType::MemberJoinedChannel { .. } => EventType::MemberJoinedChannel,
EventCallbackType::MemberLeftChannel { .. } => EventType::MemberLeftChannel,
EventCallbackType::Message { .. } => EventType::Message,
EventCallbackType::Other => EventType::Other,
},
}
}
}

/// [Event API Type](https://api.slack.com/events?filter=Events)
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum EventType {
AppHomeOpened,
AppMention,
AppRateLimited,
AppRequested,
AppUninstalled,
ChannelArchive,
ChannelCreated,
ChannelDeleted,
ChannelHistoryChanged,
ChannelIDChanged,
ChannelLeft,
ChannelRename,
ChannelShared,
ChannelUnarchive,
ChannelUnshared,
EmojiChanged,
GridMigrationFinished,
GridMigrationStarted,
GroupArchive,
GroupClose,
GroupDeleted,
GroupHistoryChanged,
GroupLeft,
GroupOpen,
GroupRename,
GroupUnarchive,
ImClose,
ImCreated,
ImHistoryChanged,
ImOpen,
InviteRequested,
LinkShared,
MemberJoinedChannel,
MemberLeftChannel,
Message,
#[serde(other)]
Other,
}

#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct EventCallback {
pub struct Event {
pub token: String,
pub team_id: String,
pub api_app_id: String,
pub event: EventCallbackType,
pub event: EventCallback,
pub event_id: String,
pub event_time: i32,
#[serde(rename = "type")]
pub _type: String,
}

#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[skip_serializing_none]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum EventCallbackType {
pub enum EventCallback {
/// User clicked into your App Home
#[serde(rename = "app_home_opened")]
AppHomeOpened {
Expand Down Expand Up @@ -326,23 +232,21 @@ mod test {
}
}"##;
let event = serde_json::from_str::<Event>(json).unwrap();
match event {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::AppHomeOpened {
user,
channel,
event_ts,
tab,
view,
} => {
assert_eq!(user, "U061F7AUR");
assert_eq!(channel, "D0LAN2Q65");
assert_eq!(event_ts, "1515449522000016");
assert_eq!(tab, "home");
assert_eq!(view.id.unwrap(), "VPASKP233");
}
_ => panic!("Event callback deserialize into incorrect variant"),
},
match event.event {
EventCallback::AppHomeOpened {
user,
channel,
event_ts,
tab,
view,
} => {
assert_eq!(user, "U061F7AUR");
assert_eq!(channel, "D0LAN2Q65");
assert_eq!(event_ts, "1515449522000016");
assert_eq!(tab, "home");
assert_eq!(view.id.unwrap(), "VPASKP233");
}
_ => panic!("Event callback deserialize into incorrect variant"),
}
}

Expand All @@ -361,11 +265,9 @@ mod test {
}"##;

let event = serde_json::from_str::<Event>(json).unwrap();
match event {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::Other => assert!(true, "true"),
_ => panic!("Event callback deserialize into incorrect variant"),
},
match event.event {
EventCallback::Other => assert!(true, "true"),
_ => panic!("Event callback deserialize into incorrect variant"),
}
}
}
15 changes: 7 additions & 8 deletions src/socket/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ pub struct AcknowledgeMessage<'s> {
#[cfg(test)]
mod test {
use super::*;
use crate::{event_api::event::EventCallbackType, payloads::interactive::InteractiveEventType};
use crate::event_api::event::*;
use crate::payloads::interactive::InteractiveEventType;

#[test]
fn deserialize_hello_event() {
Expand Down Expand Up @@ -200,13 +201,11 @@ mod test {
assert_eq!(envelope_id, "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545");
assert!(!accepts_response_payload, "false");

match payload {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::AppHomeOpened { user, .. } => {
assert_eq!(user, "U061F7AUR");
}
_ => panic!("Event callback deserialize into incorrect variant"),
},
match payload.event {
EventCallback::AppHomeOpened { user, .. } => {
assert_eq!(user, "U061F7AUR");
}
_ => panic!("Event callback deserialize into incorrect variant"),
}
}
_ => panic!("Event deserialize into incorrect variant"),
Expand Down
14 changes: 6 additions & 8 deletions src/socket/socket_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub async fn connector_for_ca_file(ca_file_path: &str) -> Result<TlsConnector, E

#[cfg(test)]
mod test {
use crate::event_api::event::{Event, EventCallbackType};
use crate::event_api::event::*;
use crate::http_client::{MockSlackWebAPIClient, SlackWebAPIClient};
use crate::payloads::interactive::InteractiveEventType;
use crate::socket::event::{
Expand Down Expand Up @@ -238,13 +238,11 @@ mod test {
assert_eq!(e.envelope_id, "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545");
assert!(!e.accepts_response_payload, "false");

match e.payload {
Event::EventCallback(event_callback) => match event_callback.event {
EventCallbackType::AppHomeOpened { user, .. } => {
assert_eq!(user, "U061F7AUR");
}
_ => panic!("Event callback deserialize into incorrect variant"),
},
match e.payload.event {
EventCallback::AppHomeOpened { user, .. } => {
assert_eq!(user, "U061F7AUR");
}
_ => panic!("Event callback deserialize into incorrect variant"),
}
log::info!("success on_events_api test");
}
Expand Down