Struct slack_messaging::blocks::Context
source · pub struct Context { /* private fields */ }Expand description
Context block representation.
Example
The following is reproduction of the sample context.
use slack_messaging::blocks::Context;
use slack_messaging::blocks::elements::{Image, Text};
use serde_json::json;
let context = Context::new()
.push_element(
Image::new()
.set_image_url("https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg")
.set_alt_text("images")
)
.push_element(
Text::mrkdwn("Location: **Dogpatch**")
);
let expected = json!({
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg",
"alt_text": "images"
},
{
"type": "mrkdwn",
"text": "Location: **Dogpatch**"
}
]
});
let context_json = serde_json::to_value(context).unwrap();
assert_eq!(context_json, expected);Implementations§
source§impl Context
impl Context
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a Context block.
use slack_messaging::blocks::Context;
use serde_json::json;
let context = Context::new();
let expected = json!({
"type": "context",
"elements": []
});
let context_json = serde_json::to_value(context).unwrap();
assert_eq!(context_json, expected);sourcepub fn set_elements(self, elements: Vec<ContextElement>) -> Self
pub fn set_elements(self, elements: Vec<ContextElement>) -> Self
Sets elements field directly. The argument is a vector composed from any objects that can transform into the enum ContextElement.
use slack_messaging::blocks::Context;
use slack_messaging::blocks::elements::{Image, Text};
use serde_json::json;
let context = Context::new()
.set_elements(
vec![
Image::new()
.set_image_url("https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg")
.set_alt_text("images")
.into(),
Text::mrkdwn("Location: **Dogpatch**").into()
]
);
let expected = json!({
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg",
"alt_text": "images"
},
{
"type": "mrkdwn",
"text": "Location: **Dogpatch**"
}
]
});
let context_json = serde_json::to_value(context).unwrap();
assert_eq!(context_json, expected);sourcepub fn push_element<T: Into<ContextElement>>(self, element: T) -> Self
pub fn push_element<T: Into<ContextElement>>(self, element: T) -> Self
Adds an object to elements field. The argument is an any object that can transform into the enum ContextElement.
use slack_messaging::blocks::Context;
use slack_messaging::blocks::elements::{Image, Text};
use serde_json::json;
let context = Context::new()
.push_element(
Image::new()
.set_image_url("https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg")
.set_alt_text("images")
)
.push_element(
Text::mrkdwn("Location: **Dogpatch**")
);
let expected = json!({
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg",
"alt_text": "images"
},
{
"type": "mrkdwn",
"text": "Location: **Dogpatch**"
}
]
});
let context_json = serde_json::to_value(context).unwrap();
assert_eq!(context_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::Context;
use serde_json::json;
let context = Context::new().set_block_id("context_block_1");
let expected = json!({
"type": "context",
"elements": [],
"block_id": "context_block_1"
});
let context_json = serde_json::to_value(context).unwrap();
assert_eq!(context_json, expected);