Struct slack_messaging::Message
source · pub struct Message { /* private fields */ }Expand description
Message payload representation.
Example
See also Header, Section and any other blocks to know how to build these blocks.
use slack_messaging::Message;
use slack_messaging::blocks::{Header, Section};
use serde_json::json;
let message = Message::new()
.set_text("New Paid Time Off request from Fred Enriquez")
.push_block(Header::new().text("New request"))
.push_block(
Section::new()
.push_field_mrkdwn("*Type:*\nPaid Time Off")
.push_field_mrkdwn("*Created by:*\n<example.com|Fred Enriquez>")
)
.push_block(
Section::new()
.push_field_mrkdwn("*When:*\nAug 10 - Aug 13")
)
.push_block(
Section::new()
.set_text_mrkdwn("<https://example.com|View request>")
);
let expected = json!({
"text": "New Paid Time Off request from Fred Enriquez",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "New request",
"emoji": true
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Type:*\nPaid Time Off"
},
{
"type": "mrkdwn",
"text": "*Created by:*\n<example.com|Fred Enriquez>"
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*When:*\nAug 10 - Aug 13"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<https://example.com|View request>"
}
}
]
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);Implementations§
source§impl Message
impl Message
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a Message.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new();
let expected = json!({});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_text<T: Into<String>>(self, text: T) -> Self
pub fn set_text<T: Into<String>>(self, text: T) -> Self
Sets text field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new()
.set_text("New Paid Time Off request from Fred Enriquez");
let expected = json!({
"text": "New Paid Time Off request from Fred Enriquez",
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_blocks(self, blocks: Vec<Block>) -> Self
pub fn set_blocks(self, blocks: Vec<Block>) -> Self
Sets blocks field directly. The argument is a vector composed from any objects that can transform into the enum Block.
use slack_messaging::Message;
use slack_messaging::blocks::{Header, Section};
use serde_json::json;
let message = Message::new()
.set_blocks(
vec![
Header::new().text("New request").into(),
Section::new().set_text_mrkdwn("<https://example.com|View request>").into()
]
);
let expected = json!({
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "New request",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<https://example.com|View request>"
}
}
]
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn push_block<T: Into<Block>>(self, block: T) -> Self
pub fn push_block<T: Into<Block>>(self, block: T) -> Self
Adds an object to blocks field. The argument is an any object that can transform into the enum Block.
use slack_messaging::Message;
use slack_messaging::blocks::{Header, Section};
use serde_json::json;
let message = Message::new()
.push_block(Header::new().text("New request"))
.push_block(Section::new().set_text_mrkdwn("<https://example.com|View request>"));
let expected = json!({
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "New request",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<https://example.com|View request>"
}
}
]
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_attachments(self, attachments: Vec<Attachment>) -> Self
pub fn set_attachments(self, attachments: Vec<Attachment>) -> Self
Sets attachments field directly. See also Attachment to know how to build an Attachment.
use slack_messaging::{Attachment, Message};
use slack_messaging::blocks::{Context, Section};
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let message = Message::new()
.set_attachments(
vec![
Attachment::new()
.set_color("#36a64f")
.push_block(Context::new().push_element(Text::mrkdwn("*title*")))
.push_block(Section::new().set_text_mrkdwn("content"))
]
);
let expected = json!({
"attachments": [
{
"color": "#36a64f",
"blocks": [
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*title*"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "content"
}
}
]
}
]
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn push_attachment(self, attachment: Attachment) -> Self
pub fn push_attachment(self, attachment: Attachment) -> Self
Adds an attachment to attachments field. See also Attachment to know how to build an Attachment.
use slack_messaging::{Attachment, Message};
use slack_messaging::blocks::{Context, Section};
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let message = Message::new()
.push_attachment(
Attachment::new()
.set_color("#36a64f")
.push_block(Context::new().push_element(Text::mrkdwn("*title*")))
.push_block(Section::new().set_text_mrkdwn("content"))
);
let expected = json!({
"attachments": [
{
"color": "#36a64f",
"blocks": [
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*title*"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "content"
}
}
]
}
]
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn attach(self, attachment: Attachment) -> Self
pub fn attach(self, attachment: Attachment) -> Self
Alias of push_attachment method.
sourcepub fn set_thread_ts<T: Into<String>>(self, thread_ts: T) -> Self
pub fn set_thread_ts<T: Into<String>>(self, thread_ts: T) -> Self
Sets thread_ts field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new()
.set_thread_ts("some ts value");
let expected = json!({
"thread_ts": "some ts value",
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_mrkdwn(self, mrkdwn: bool) -> Self
pub fn set_mrkdwn(self, mrkdwn: bool) -> Self
Sets mrkdwn field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().set_mrkdwn(true);
let expected = json!({
"mrkdwn": true,
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_channel<T: Into<String>>(self, channel: T) -> Self
pub fn set_channel<T: Into<String>>(self, channel: T) -> Self
Sets channel field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().set_channel("CHANNEL ID");
let expected = json!({
"channel": "CHANNEL ID",
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_response_type(self, response_type: ResponseType) -> Self
pub fn set_response_type(self, response_type: ResponseType) -> Self
Sets response_type field.
use slack_messaging::{Message, ResponseType};
use serde_json::json;
let message = Message::new()
.set_response_type(ResponseType::InChannel);
let expected = json!({
"response_type": "in_channel",
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_replace_original(self, replace: bool) -> Self
pub fn set_replace_original(self, replace: bool) -> Self
Sets replace_original field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().set_replace_original(true);
let expected = json!({
"replace_original": true,
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn replace_original(self) -> Self
pub fn replace_original(self) -> Self
Sets true to replace_original field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().replace_original();
let expected = json!({
"replace_original": true,
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn set_delete_original(self, delete: bool) -> Self
pub fn set_delete_original(self, delete: bool) -> Self
Sets delete_original field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().set_delete_original(true);
let expected = json!({
"delete_original": true,
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);sourcepub fn delete_original(self) -> Self
pub fn delete_original(self) -> Self
Sets true to delete_original field.
use slack_messaging::Message;
use serde_json::json;
let message = Message::new().delete_original();
let expected = json!({
"delete_original": true,
});
let message_json = serde_json::to_value(message).unwrap();
assert_eq!(message_json, expected);