slack_hook/
payload.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{Attachment, Result, SlackText};
use reqwest::Url;
use serde::{Serialize, Serializer};

/// Payload to send to slack
/// <https://api.slack.com/incoming-webhooks>
/// <https://api.slack.com/methods/chat.postMessage>
#[derive(Serialize, Debug, Default)]
pub struct Payload {
    /// text to send
    /// despite `text` stated as required, it does not seem to be
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<SlackText>,
    /// channel to send payload to
    /// note: if not provided, this will default to channel
    /// setup in slack
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel: Option<String>,
    /// username override
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,
    /// specific url for icon
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon_url: Option<Url>,
    /// emoji for icon
    /// <https://api.slack.com/methods/emoji.list>
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon_emoji: Option<String>,
    /// attachments to send
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachments: Option<Vec<Attachment>>,
    /// whether slack will try to fetch links and create an attachment
    /// <https://api.slack.com/docs/unfurling>
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unfurl_links: Option<bool>,
    /// Pass false to disable unfurling of media content
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unfurl_media: Option<bool>,
    /// find and link channel names and usernames
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link_names: Option<u8>,
    /// Change how messages are treated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parse: Option<Parse>,
}

/// Change how messages are treated.
#[derive(Debug)]
pub enum Parse {
    /// Full
    Full,
    /// None
    None,
}

impl Serialize for Parse {
    fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let st = match *self {
            Parse::Full => "full",
            Parse::None => "none",
        };
        serializer.serialize_str(st)
    }
}
/// `PayloadBuilder` is used to build a `Payload`
#[derive(Debug)]
#[must_use]
pub struct PayloadBuilder {
    inner: Result<Payload>,
}

impl Default for PayloadBuilder {
    fn default() -> PayloadBuilder {
        PayloadBuilder {
            inner: Ok(Default::default()),
        }
    }
}

impl PayloadBuilder {
    /// Make a new `PayloadBuilder`
    pub fn new() -> Self {
        Default::default()
    }

    /// Set the text
    pub fn text<S: Into<SlackText>>(mut self, text: S) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.text = Some(text.into());
        }
        self
    }

    /// Set the channel
    pub fn channel<S: Into<String>>(mut self, channel: S) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.channel = Some(channel.into());
        }
        self
    }

    /// Set the username
    pub fn username<S: Into<String>>(mut self, username: S) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.username = Some(username.into());
        }
        self
    }

    /// Set the icon_emoji
    pub fn icon_emoji<S: Into<String>>(mut self, icon_emoji: S) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.icon_emoji = Some(icon_emoji.into());
        }
        self
    }

    url_builder_fn! {
        /// Set the icon_url
        icon_url, Self
    }

    /// Set the attachments
    pub fn attachments(mut self, attachments: Vec<Attachment>) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.attachments = Some(attachments);
        }
        self
    }

    /// whether slack will try to fetch links and create an attachment
    /// <https://api.slack.com/docs/unfurling>
    pub fn unfurl_links(mut self, b: bool) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.unfurl_links = Some(b);
        }
        self
    }

    /// Pass false to disable unfurling of media content
    pub fn unfurl_media(mut self, b: bool) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.unfurl_media = Some(b);
        }
        self
    }

    /// Find and link channel names and usernames.
    // NOTE: The Slack API doesn't seem to actually require setting `link_names` to 1, any value
    // seems to work. However, to be faithful to their spec we will stick to 0 and 1
    pub fn link_names(mut self, b: bool) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.link_names = Some(u8::from(b));
        }
        self
    }

    /// Change how messages are treated.
    pub fn parse(mut self, p: Parse) -> Self {
        if let Ok(inner) = &mut self.inner {
            inner.parse = Some(p);
        }
        self
    }

    /// Attempt to build the `Payload`
    pub fn build(self) -> Result<Payload> {
        self.inner
    }
}