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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use {Attachment, SlackText, TryInto};
use helper::bool_to_u8;
use error::{Error, Result};
use serde::{Serialize, Serializer};
use reqwest::Url;

/// 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")]
    #[serde(with = "::url_serde")]
    pub icon_url: Option<Url>,
    /// emjoi 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)]
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() -> PayloadBuilder {
        Default::default()
    }

    /// Set the text
    pub fn text<S: Into<SlackText>>(self, text: S) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.text = Some(text.into());
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

    /// Set the channel
    pub fn channel<S: Into<String>>(self, channel: S) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.channel = Some(channel.into());
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

    /// Set the username
    pub fn username<S: Into<String>>(self, username: S) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.username = Some(username.into());
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

    /// Set the icon_emoji
    pub fn icon_emoji<S: Into<String>>(self, icon_emoji: S) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.icon_emoji = Some(icon_emoji.into());
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

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

    /// Set the attachments
    pub fn attachments(self, attachments: Vec<Attachment>) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.attachments = Some(attachments);
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

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

    /// Pass false to disable unfurling of media content
    pub fn unfurl_media(self, b: bool) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.unfurl_media = Some(b);
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => 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 keep the `bool_to_u8` fn
    // around.
    pub fn link_names(self, b: bool) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.link_names = Some(bool_to_u8(b));
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

    /// Change how messages are treated.
    pub fn parse(self, p: Parse) -> PayloadBuilder {
        match self.inner {
            Ok(mut inner) => {
                inner.parse = Some(p);
                PayloadBuilder { inner: Ok(inner) }
            }
            _ => self,
        }
    }

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