Skip to content

Commit

Permalink
Interaction response
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicoh committed Aug 30, 2024
1 parent bce7235 commit aa5011d
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.3.2][] - 2024-08-30

- https://github.com/kaicoh/slack-messaging/pull/11 Use `Message` as an interaction response.

## [0.3.1][] - 2024-04-10

- https://github.com/kaicoh/slack-messaging/pull/10 Improve date formatting.
Expand All @@ -26,6 +30,7 @@

- pre-release

[0.3.2]: https://github.com/kaicoh/slack-messaging/releases/v0.3.2
[0.3.1]: https://github.com/kaicoh/slack-messaging/releases/v0.3.1
[0.3.0]: https://github.com/kaicoh/slack-messaging/releases/v0.3.0
[0.2.2]: https://github.com/kaicoh/slack-messaging/releases/v0.2.2
Expand Down
150 changes: 150 additions & 0 deletions src/message/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct MessageBuilder {
blocks: Vec<Block>,
thread_ts: Option<String>,
mrkdwn: Option<bool>,
response_type: Option<String>,
replace_original: Option<bool>,
delete_original: Option<bool>,
}

impl MessageBuilder {
Expand Down Expand Up @@ -231,13 +234,145 @@ impl MessageBuilder {
self.set_mrkdwn(Some(mrkdwn))
}

/// Set response_type field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .set_response_type(Some("in_channel".into()))
/// .build();
///
/// let expected = serde_json::json!({
/// "response_type": "in_channel",
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn set_response_type(self, response_type: Option<String>) -> Self {
Self {
response_type,
..self
}
}

/// Set response_type field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .response_type("in_channel")
/// .build();
///
/// let expected = serde_json::json!({
/// "response_type": "in_channel",
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn response_type(self, response_type: impl Into<String>) -> Self {
self.set_response_type(Some(response_type.into()))
}

/// Set replace_original field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .set_replace_original(Some(true))
/// .build();
///
/// let expected = serde_json::json!({
/// "replace_original": true,
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn set_replace_original(self, replace_original: Option<bool>) -> Self {
Self {
replace_original,
..self
}
}

/// Set replace_original field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .replace_original(true)
/// .build();
///
/// let expected = serde_json::json!({
/// "replace_original": true,
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn replace_original(self, replace_original: bool) -> Self {
self.set_replace_original(Some(replace_original))
}

/// Set delete_original field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .set_delete_original(Some(true))
/// .build();
///
/// let expected = serde_json::json!({
/// "delete_original": true,
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn set_delete_original(self, delete_original: Option<bool>) -> Self {
Self {
delete_original,
..self
}
}

/// Set delete_original field.
///
/// ```
/// # use slack_messaging::Message;
/// let message = Message::builder()
/// .delete_original(true)
/// .build();
///
/// let expected = serde_json::json!({
/// "delete_original": true,
/// });
///
/// let json = serde_json::to_value(message).unwrap();
///
/// assert_eq!(json, expected);
/// ```
pub fn delete_original(self, delete_original: bool) -> Self {
self.set_delete_original(Some(delete_original))
}

/// Build a [`Message`] object.
pub fn build(self) -> Message {
Message {
text: self.text,
blocks: self.blocks,
thread_ts: self.thread_ts,
mrkdwn: self.mrkdwn,
response_type: self.response_type,
replace_original: self.replace_original,
delete_original: self.delete_original,
}
}

Expand All @@ -260,4 +395,19 @@ impl MessageBuilder {
pub fn get_mrkdwn(&self) -> &Option<bool> {
&self.mrkdwn
}

/// Get response_type value.
pub fn get_response_type(&self) -> &Option<String> {
&self.response_type
}

/// Get replace_original value.
pub fn get_replace_original(&self) -> &Option<bool> {
&self.replace_original
}

/// Get delete_original value.
pub fn get_delete_original(&self) -> &Option<bool> {
&self.delete_original
}
}
9 changes: 9 additions & 0 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ pub struct Message {

#[serde(skip_serializing_if = "Option::is_none")]
pub(super) mrkdwn: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
pub(super) response_type: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub(super) replace_original: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
pub(super) delete_original: Option<bool>,
}

0 comments on commit aa5011d

Please sign in to comment.