telexide_fork/client/
builder.rs

1use super::{APIConnector, Client, EventHandlerFunc, RawEventHandlerFunc, WebhookOptions};
2use crate::{
3    api::{types::UpdateType, APIClient},
4    framework::Framework,
5};
6
7use parking_lot::RwLock;
8use std::sync::Arc;
9use typemap::ShareMap;
10
11/// A builder for the [`Client`] object to make customisation easier
12pub struct ClientBuilder {
13    hyper_client: Option<hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>>,
14    api_client: Option<Arc<Box<APIConnector>>>,
15    webhook: Option<WebhookOptions>,
16    framework: Option<Arc<Framework>>,
17    token: Option<String>,
18    allowed_updates: Vec<UpdateType>,
19    event_handler_funcs: Vec<EventHandlerFunc>,
20    raw_event_handler_funcs: Vec<RawEventHandlerFunc>,
21}
22
23impl ClientBuilder {
24    /// Creates a bare builder
25    #[allow(clippy::new_without_default)]
26    pub fn new() -> Self {
27        Self {
28            api_client: None,
29            hyper_client: None,
30            webhook: None,
31            framework: None,
32            token: None,
33            allowed_updates: Vec::new(),
34            event_handler_funcs: Vec::new(),
35            raw_event_handler_funcs: Vec::new(),
36        }
37    }
38
39    /// sets the webhook url for the [`Client`] to listen to
40    pub fn set_webhook(&mut self, webhook: &WebhookOptions) -> &mut Self {
41        self.webhook = Some(webhook.clone());
42        self
43    }
44
45    /// Sets the framework for your bot to use, please use the
46    /// [`create_framework`] macro for creating it
47    ///
48    /// [`create_framework`]: ../macro.create_framework.html
49    pub fn set_framework(&mut self, framework: Arc<Framework>) -> &mut Self {
50        self.framework = Some(framework);
51        self
52    }
53
54    /// Sets the token to be used in authorizing the API requests of your bot
55    pub fn set_token(&mut self, token: &str) -> &mut Self {
56        self.token = Some(token.to_owned());
57        self
58    }
59
60    /// Sets the custom hyper client for the `APIClient` to use
61    pub fn set_hyper_client(
62        &mut self,
63        client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
64    ) -> &mut Self {
65        self.hyper_client = Some(client);
66        self
67    }
68
69    /// Sets the custom API client
70    pub fn set_api_client(&mut self, client: Arc<Box<APIConnector>>) -> &mut Self {
71        self.api_client = Some(client);
72        self
73    }
74
75    /// Set the list of update types you want your update handlers to handle
76    pub fn set_allowed_updates(&mut self, allowed: Vec<UpdateType>) -> &mut Self {
77        self.allowed_updates = allowed;
78        self
79    }
80
81    /// Add an update type to the list of update types you want your update
82    /// handlers to handle
83    pub fn add_allowed_updates(&mut self, allowed: UpdateType) -> &mut Self {
84        self.allowed_updates.push(allowed);
85        self
86    }
87
88    /// Remove an update type from the list of update types you want your update
89    /// handlers to handle
90    pub fn remove_allowed_updates(&mut self, denied: &UpdateType) -> &mut Self {
91        self.allowed_updates.retain(|t| t != denied);
92        self
93    }
94
95    /// Adds an [`EventHandlerFunc`] function for handling incoming updates
96    pub fn add_handler_func(&mut self, handler: EventHandlerFunc) -> &mut Self {
97        self.event_handler_funcs.push(handler);
98        self
99    }
100
101    /// Adds an [`RawEventHandlerFunc`] function for handling incoming updates
102    pub fn add_raw_handler_func(&mut self, handler: RawEventHandlerFunc) -> &mut Self {
103        self.raw_event_handler_funcs.push(handler);
104        self
105    }
106
107    /// Creates the [`Client`] object from the settings set in the
108    /// [`ClientBuilder`] object
109    pub fn build(&mut self) -> Client {
110        if self.framework.is_some() && !self.allowed_updates.contains(&UpdateType::Message) {
111            self.allowed_updates.push(UpdateType::Message);
112        }
113
114        self.api_client.clone().map_or_else(
115            || Client {
116                api_client: Arc::new(Box::new(APIClient::new(
117                    self.hyper_client.clone(),
118                    self.token
119                        .as_ref()
120                        .expect("A token must be provided for the telegram bot to work"),
121                ))),
122                event_handlers: self.event_handler_funcs.clone(),
123                raw_event_handlers: self.raw_event_handler_funcs.clone(),
124                data: Arc::new(RwLock::new(ShareMap::custom())),
125                framework: self.framework.clone(),
126                webhook_opts: self.webhook.clone(),
127                allowed_updates: self.allowed_updates.clone(),
128            },
129            |c| Client {
130                api_client: c,
131                event_handlers: self.event_handler_funcs.clone(),
132                webhook_opts: self.webhook.clone(),
133                raw_event_handlers: self.raw_event_handler_funcs.clone(),
134                data: Arc::new(RwLock::new(ShareMap::custom())),
135                framework: self.framework.clone(),
136                allowed_updates: self.allowed_updates.clone(),
137            },
138        )
139    }
140}