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

source

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);
source

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);
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::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);
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::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);
source

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);
source

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);
source

pub fn attach(self, attachment: Attachment) -> Self

Alias of push_attachment method.

source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);

Trait Implementations§

source§

impl Clone for Message

source§

fn clone(&self) -> Message

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 Message

source§

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

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

impl Default for Message

source§

fn default() -> Message

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

impl Serialize for Message

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.