slack_hook/attachment.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 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
use crate::error::{Error, Result};
use crate::{HexColor, SlackText, SlackTime};
use chrono::NaiveDateTime;
use reqwest::Url;
use serde::Serialize;
use std::convert::TryInto;
/// Slack allows for attachments to be added to messages. See
/// <https://api.slack.com/docs/attachments> for more information.
#[derive(Serialize, Debug, Default, Clone, PartialEq)]
pub struct Attachment {
/// Required text for attachment.
/// Slack will use this text to display on devices that don't support markup.
pub fallback: SlackText,
/// Optional text for other devices, markup supported
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<SlackText>,
/// Optional text that appears above attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub pretext: Option<SlackText>,
/// Optional color of attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub color: Option<HexColor>,
/// Actions as array
#[serde(skip_serializing_if = "Option::is_none")]
pub actions: Option<Vec<Action>>,
/// Fields are defined as an array, and hashes contained within it will be
/// displayed in a table inside the message attachment.
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<Vec<Field>>,
/// Optional small text used to display the author's name.
#[serde(skip_serializing_if = "Option::is_none")]
pub author_name: Option<SlackText>,
/// Optional URL that will hyperlink the `author_name` text mentioned above. Will only
/// work if `author_name` is present.
#[serde(skip_serializing_if = "Option::is_none")]
pub author_link: Option<Url>,
/// Optional URL that displays a small 16x16px image to the left of
/// the `author_name` text. Will only work if `author_name` is present.
#[serde(skip_serializing_if = "Option::is_none")]
pub author_icon: Option<Url>,
/// Optional larger, bolder text above the main body
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<SlackText>,
/// Optional URL to link to from the title
#[serde(skip_serializing_if = "Option::is_none")]
pub title_link: Option<Url>,
/// Optional URL to an image that will be displayed in the body
#[serde(skip_serializing_if = "Option::is_none")]
pub image_url: Option<Url>,
/// Optional URL to an image that will be displayed as a thumbnail to the
/// right of the body
#[serde(skip_serializing_if = "Option::is_none")]
pub thumb_url: Option<Url>,
/// Optional text that will appear at the bottom of the attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub footer: Option<SlackText>,
/// Optional URL to an image that will be displayed at the bottom of the
/// attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub footer_icon: Option<Url>,
/// Optional timestamp to be displayed with the attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub ts: Option<SlackTime>,
/// Optional sections formatted as markdown.
#[serde(skip_serializing_if = "Option::is_none")]
pub mrkdwn_in: Option<Vec<Section>>,
/// Optional callback_id for actions
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_id: Option<SlackText>,
}
/// Sections define parts of an attachment.
#[derive(Eq, PartialEq, Copy, Clone, Serialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Section {
/// The pretext section.
Pretext,
/// The text section.
Text,
/// The fields.
Fields,
}
/// Actions are defined as an array, and values contained within it will
/// be displayed with the message.
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Action {
/// Action type, renamed to 'type'
#[serde(rename = "type")]
pub action_type: String,
/// Text for action
pub text: String,
/// Name of action
pub name: String,
/// Action style, ie: primary, danger, etc
#[serde(skip_serializing_if = "Option::is_none")]
pub style: Option<String>,
/// Value of action
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
}
impl Action {
/// Construct a new field
pub fn new<S: Into<String>>(
action_type: S,
text: S,
name: S,
style: Option<String>,
value: Option<String>,
) -> Action {
Action {
action_type: action_type.into(),
text: text.into(),
name: name.into(),
style,
value,
}
}
}
/// Fields are defined as an array, and hashes contained within it will
/// be displayed in a table inside the message attachment.
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Field {
/// Shown as a bold heading above the value text.
/// It cannot contain markup and will be escaped for you.
pub title: String,
/// The text value of the field. It may contain standard message markup
/// and must be escaped as normal. May be multi-line.
pub value: SlackText,
/// An optional flag indicating whether the value is short enough to be
/// displayed side-by-side with other values.
#[serde(skip_serializing_if = "Option::is_none")]
pub short: Option<bool>,
}
impl Field {
/// Construct a new field
pub fn new<S: Into<String>, ST: Into<SlackText>>(
title: S,
value: ST,
short: Option<bool>,
) -> Field {
Field {
title: title.into(),
value: value.into(),
short,
}
}
}
/// `AttachmentBuilder` is used to build a `Attachment`
#[derive(Debug)]
#[must_use]
pub struct AttachmentBuilder {
inner: Result<Attachment>,
}
impl AttachmentBuilder {
/// Make a new `AttachmentBuilder`
///
/// Fallback is the only required field which is a plain-text summary of the attachment.
// FIXME(cosmic): there's a bit of a miss-match where `fallback` is the only required field,
// but an `Attachment` can still be constructed with purely default values :thinking_face:
pub fn new<S: Into<SlackText>>(fallback: S) -> Self {
Self {
inner: Ok(Attachment {
fallback: fallback.into(),
..Default::default()
}),
}
}
/// Optional text that appears within the attachment
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 color of the attachment
///
/// The color can be one of:
///
/// 1. `String`s: `good`, `warning`, `danger`
/// 2. The built-in enums: `SlackColor::Good`, etc.
/// 3. Any valid hex color code: e.g. `#b13d41` or `#000`.
///
/// hex color codes will be checked to ensure a valid hex number is provided
pub fn color<C: TryInto<HexColor, Error = Error>>(mut self, color: C) -> Self {
if let Ok(inner) = &mut self.inner {
match color.try_into() {
Ok(c) => inner.color = Some(c),
Err(err) => self.inner = Err(err),
}
}
self
}
/// Optional text that appears above the attachment block
pub fn pretext<S: Into<SlackText>>(mut self, pretext: S) -> Self {
if let Ok(inner) = &mut self.inner {
inner.pretext = Some(pretext.into());
}
self
}
/// Actions are defined as an array, and hashes contained within it will be
/// displayed in a table inside the message attachment.
pub fn actions(mut self, actions: Vec<Action>) -> Self {
if let Ok(inner) = &mut self.inner {
inner.actions = Some(actions);
}
self
}
/// Fields are defined as an array, and hashes contained within it will be
/// displayed in a table inside the message attachment.
pub fn fields(mut self, fields: Vec<Field>) -> Self {
if let Ok(inner) = &mut self.inner {
inner.fields = Some(fields);
}
self
}
/// Optional small text used to display the author's name.
pub fn author_name<S: Into<SlackText>>(mut self, author_name: S) -> Self {
if let Ok(inner) = &mut self.inner {
inner.author_name = Some(author_name.into());
}
self
}
url_builder_fn! {
/// Optional URL that will hyperlink the `author_name`.
author_link, Self
}
url_builder_fn! {
/// Optional URL that displays a small 16x16px image to the left of the `author_name` text.
author_icon, Self
}
/// Optional larger, bolder text above the main body
pub fn title<S: Into<SlackText>>(mut self, title: S) -> Self {
if let Ok(inner) = &mut self.inner {
inner.title = Some(title.into());
}
self
}
/// Optional larger, bolder text above the main body
pub fn callback_id<S: Into<SlackText>>(mut self, callback_id: S) -> Self {
if let Ok(inner) = &mut self.inner {
inner.callback_id = Some(callback_id.into());
}
self
}
url_builder_fn! {
/// Optional URL to link to from the title
title_link, Self
}
url_builder_fn! {
/// Optional URL to an image that will be displayed in the body
image_url, Self
}
url_builder_fn! {
/// Optional URL to an image that will be displayed as a thumbnail to the right of the body
thumb_url, Self
}
/// Optional text that will appear at the bottom of the attachment
pub fn footer<S: Into<SlackText>>(mut self, footer: S) -> Self {
if let Ok(inner) = &mut self.inner {
inner.footer = Some(footer.into());
}
self
}
url_builder_fn! {
/// Optional URL to an image that will be displayed at the bottom of the attachment
footer_icon, Self
}
/// Optional timestamp to be displayed with the attachment
pub fn ts(mut self, time: &NaiveDateTime) -> Self {
if let Ok(inner) = &mut self.inner {
inner.ts = Some(SlackTime::new(time));
}
self
}
/// Optional sections formatted as markdown.
pub fn markdown_in<'a, I: IntoIterator<Item = &'a Section>>(mut self, sections: I) -> Self {
if let Ok(inner) = &mut self.inner {
inner.mrkdwn_in = Some(sections.into_iter().cloned().collect());
}
self
}
/// Attempt to build the `Attachment`
pub fn build(mut self) -> Result<Attachment> {
// set text to equal fallback if text wasn't specified
if let Ok(inner) = &mut self.inner {
if inner.text.is_none() {
inner.text = Some(inner.fallback.clone());
}
}
self.inner
}
}