pub struct Attachment { /* private fields */ }
Expand description

Secondary message attachment representation.

Example

See also Context, Section and any other blocks to know how to build these blocks.

use slack_messaging::Attachment;
use slack_messaging::blocks::{Context, Section};
use slack_messaging::blocks::elements::{Image, Text};
use serde_json::json;

let author = Context::new()
    .push_element(
        Image::new()
            .set_image_url("https://placeimg.com/16/16/people")
            .set_alt_text("author")
    )
    .push_element(
        Text::mrkdwn("*<http://flickr.com/bobby/|author>*")
    );

let content = Section::new()
    .set_text_mrkdwn("Optional `text` that appears within the attachment")
    .push_field_mrkdwn("*A 1st field's title*\nA 1st field's value")
    .push_field_mrkdwn("*A 2nd field's title*\nA 2nd field's value")
    .set_accessory(
        Image::new()
            .set_image_url("http://placekitten.com/g/200/200")
            .set_alt_text("cute kitten")
    );

let footer = Context::new()
    .push_element(
        Image::new()
            .set_image_url("https://platform.slack-edge.com/img/default_application_icon.png")
            .set_alt_text("footer icon")
    )
    .push_element(
        Text::mrkdwn("footer | <!date^1392734382^{date} at {time}|February 18th, 2014 at 6:39 AM PST>")
    );

let attachment = Attachment::new()
    .set_color("#36a64f")
    .push_block(author)
    .push_block(content)
    .push_block(footer);

let expected = json!({
    "color": "#36a64f",
    "blocks": [
        {
            "type": "context",
            "elements": [
                {
                    "type": "image",
                    "image_url": "https://placeimg.com/16/16/people",
                    "alt_text": "author"
                },
                {
                    "type": "mrkdwn",
                    "text": "*<http://flickr.com/bobby/|author>*"
                }
            ]
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Optional `text` that appears within the attachment"
            },
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*A 1st field's title*\nA 1st field's value"
                },
                {
                    "type": "mrkdwn",
                    "text": "*A 2nd field's title*\nA 2nd field's value"
                }
            ],
            "accessory": {
                "type": "image",
                "image_url": "http://placekitten.com/g/200/200",
                "alt_text": "cute kitten"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "image",
                    "image_url": "https://platform.slack-edge.com/img/default_application_icon.png",
                    "alt_text": "footer icon"
                },
                {
                    "type": "mrkdwn",
                    "text": "footer | <!date^1392734382^{date} at {time}|February 18th, 2014 at 6:39 AM PST>"
                }
            ]
        }
    ]
});

let attachment_json = serde_json::to_value(attachment).unwrap();

assert_eq!(attachment_json, expected);

Implementations§

source§

impl Attachment

source

pub fn new() -> Self

Constructs an Attachment.

use slack_messaging::Attachment;
use serde_json::json;

let attachment = Attachment::new();
let expected = json!({});
let attachment_json = serde_json::to_value(attachment).unwrap();

assert_eq!(attachment_json, expected);
source

pub fn set_color<T: Into<String>>(self, color: T) -> Self

Sets color field.

use slack_messaging::Attachment;
use serde_json::json;

let attachment = Attachment::new().set_color("#FFFFFF");
let expected = json!({
    "color": "#FFFFFF"
});
let attachment_json = serde_json::to_value(attachment).unwrap();

assert_eq!(attachment_json, expected);
source

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::Attachment;
use slack_messaging::blocks::{Context, Section};
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let attachment = Attachment::new()
    .set_blocks(
        vec![
            Context::new().push_element(Text::mrkdwn("*title*")).into(),
            Section::new().set_text_mrkdwn("content").into()
        ]
    );

let expected = json!({
    "blocks": [
        {
            "type": "context",
            "elements": [
                {
                    "type": "mrkdwn",
                    "text": "*title*"
                }
            ]
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "content"
            }
        }
    ]
});

let attachment_json = serde_json::to_value(attachment).unwrap();

assert_eq!(attachment_json, expected);
source

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::Attachment;
use slack_messaging::blocks::{Context, Section};
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let attachment = Attachment::new()
    .push_block(Context::new().push_element(Text::mrkdwn("*title*")))
    .push_block(Section::new().set_text_mrkdwn("content"));

let expected = json!({
    "blocks": [
        {
            "type": "context",
            "elements": [
                {
                    "type": "mrkdwn",
                    "text": "*title*"
                }
            ]
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "content"
            }
        }
    ]
});

let attachment_json = serde_json::to_value(attachment).unwrap();

assert_eq!(attachment_json, expected);

Trait Implementations§

source§

impl Clone for Attachment

source§

fn clone(&self) -> Attachment

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Attachment

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Attachment

source§

fn default() -> Attachment

Returns the “default value” for a type. Read more
source§

impl Serialize for Attachment

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.