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
use client::GroupmeClient;
use error::GroupmeError;
use bot::Bot;

use std::rc::Rc;

/// Builder to create Bots with configuration and add them to Groupme.
///
/// # Examples
///
/// ```rust,no_run
/// use groupme_bot::{Groupme, Bot};
///
/// let groupme: Groupme = Groupme::new(Some("Required API Token"));
/// let bot: Bot = groupme
///     .create_bot("My Bot", "a group id")
///     .unwrap()
///     .with_avatar_url("some url")
///     .with_callback_url("some other url")
///     .with_dm_notification(true)
///     .create()
///     .unwrap();
/// ```
#[derive(Debug)]
pub struct BotBuilder {
    name: String,
    group_id: String,
    client: Rc<GroupmeClient>,
    token: String,

    avatar_url: Option<String>,
    callback_url: Option<String>,
    dm_notification: Option<bool>,
}

impl BotBuilder {
    pub(super) fn new(
        name: &str,
        group_id: &str,
        client: Rc<GroupmeClient>,
        token: &str,
    ) -> BotBuilder {
        BotBuilder {
            name: name.to_string(),
            group_id: group_id.to_string(),
            client,
            token: token.to_string(),

            avatar_url: None,
            callback_url: None,
            dm_notification: None,
        }
    }

    /// Sets the avatar_url for a new Bot.
    ///
    /// Groupme will only accept image urls from their [Image Service](https://dev.groupme.com/docs/image_service).
    pub fn with_avatar_url(mut self, avatar_url: &str) -> Self {
        self.avatar_url = Some(avatar_url.to_string());
        self
    }

    /// Sets the callback_url for a new Bot.
    pub fn with_callback_url(mut self, callback_url: &str) -> Self {
        self.callback_url = Some(callback_url.to_string());
        self
    }

    /// Sets the dm_notificaion for a new Bot.
    pub fn with_dm_notification(mut self, dm_notification: bool) -> Self {
        self.dm_notification = Some(dm_notification);
        self
    }

    /// Builds a `Bot` with the set values and adds it to a group.
    pub fn create(self) -> Result<Bot, GroupmeError> {
        let gm_client: &GroupmeClient = &self.client;
        let bot_id = gm_client.create(
            &self.token,
            &self.name,
            &self.group_id,
            self.avatar_url.as_ref().map(String::as_str),
            self.callback_url.as_ref().map(String::as_str),
            self.dm_notification,
        )?;
        Ok(Bot {
            bot_id,
            client: self.client.clone(),
        })
    }
}