Struct slack_messaging::blocks::Section
source · pub struct Section { /* private fields */ }Expand description
Section block representation.
Example
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Image;
use serde_json::json;
let section = Section::new()
.set_block_id("section_1")
.set_text_mrkdwn("A message *with some bold text* and _some italicized text_.")
.push_field_mrkdwn("High")
.push_field_plain("String")
.set_accessory(
Image::new()
.set_image_url("http://placekitten.com/700/500")
.set_alt_text("Multiple cute kittens")
);
let expected = json!({
"type": "section",
"block_id": "section_1",
"text": {
"type": "mrkdwn",
"text": "A message *with some bold text* and _some italicized text_."
},
"fields": [
{
"type": "mrkdwn",
"text": "High"
},
{
"type": "plain_text",
"text": "String",
"emoji": true
}
],
"accessory": {
"type": "image",
"image_url": "http://placekitten.com/700/500",
"alt_text": "Multiple cute kittens"
}
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);Implementations§
source§impl Section
impl Section
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a Section block.
use slack_messaging::blocks::Section;
use serde_json::json;
let section = Section::new();
let expected = json!({
"type": "section",
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_text(self, text: Text) -> Self
pub fn set_text(self, text: Text) -> Self
Sets text field.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let section = Section::new()
.set_text(Text::mrkdwn("A message *with some bold text* and _some italicized text_."));
let expected = json!({
"type": "section",
"text": {
"type": "mrkdwn",
"text": "A message *with some bold text* and _some italicized text_."
},
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_text_plain<T: Into<String>>(self, text: T) -> Self
pub fn set_text_plain<T: Into<String>>(self, text: T) -> Self
Sets text field as plain text object.
use slack_messaging::blocks::Section;
use serde_json::json;
let section = Section::new()
.set_text_plain("A message *with some bold text* and _some italicized text_.");
let expected = json!({
"type": "section",
"text": {
"type": "plain_text",
"text": "A message *with some bold text* and _some italicized text_.",
"emoji": true
},
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_text_mrkdwn<T: Into<String>>(self, text: T) -> Self
pub fn set_text_mrkdwn<T: Into<String>>(self, text: T) -> Self
Sets text field as mrkdwn text object.
use slack_messaging::blocks::Section;
use serde_json::json;
let section = Section::new()
.set_text_mrkdwn("A message *with some bold text* and _some italicized text_.");
let expected = json!({
"type": "section",
"text": {
"type": "mrkdwn",
"text": "A message *with some bold text* and _some italicized text_."
},
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_block_id<T: Into<String>>(self, block_id: T) -> Self
pub fn set_block_id<T: Into<String>>(self, block_id: T) -> Self
Sets block_id field.
use slack_messaging::blocks::Section;
use serde_json::json;
let section = Section::new()
.set_block_id("section_1");
let expected = json!({
"type": "section",
"block_id": "section_1"
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_fields(self, fields: Vec<Text>) -> Self
pub fn set_fields(self, fields: Vec<Text>) -> Self
Sets fields field directly.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let section = Section::new()
.set_fields(
vec![
Text::plain("hello"),
Text::mrkdwn("*world*")
]
);
let expected = json!({
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "hello",
"emoji": true
},
{
"type": "mrkdwn",
"text": "*world*"
},
]
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn push_field(self, field: Text) -> Self
pub fn push_field(self, field: Text) -> Self
Adds Text object to fields field.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let section = Section::new()
.push_field(
Text::plain("hello world")
);
let expected = json!({
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "hello world",
"emoji": true
}
]
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn push_field_plain<T: Into<String>>(self, field: T) -> Self
pub fn push_field_plain<T: Into<String>>(self, field: T) -> Self
Adds plain_text Text object to fields field.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let section = Section::new()
.push_field_plain("hello world");
let expected = json!({
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "hello world",
"emoji": true
}
]
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn push_field_mrkdwn<T: Into<String>>(self, field: T) -> Self
pub fn push_field_mrkdwn<T: Into<String>>(self, field: T) -> Self
Adds mrkdwn Text object to fields field.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;
let section = Section::new()
.push_field_mrkdwn("hello world");
let expected = json!({
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "hello world"
}
]
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);sourcepub fn set_accessory<T: Into<Accessory>>(self, accessory: T) -> Self
pub fn set_accessory<T: Into<Accessory>>(self, accessory: T) -> Self
Sets object to accessory field. The argument is an any object that can transform into the enum Accessory.
use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Button;
use serde_json::json;
let section = Section::new()
.set_accessory(
Button::new()
.text("Click Me")
.set_action_id("button-0")
.set_value("click_me_123")
.set_primary()
);
let expected = json!({
"type": "section",
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Click Me",
"emoji": true
},
"value": "click_me_123",
"action_id": "button-0",
"style": "primary"
}
});
let section_json = serde_json::to_value(section).unwrap();
assert_eq!(section_json, expected);