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
use super::{APIConnector, Client, EventHandlerFunc, RawEventHandlerFunc, WebhookOptions};
use crate::{
api::{types::UpdateType, APIClient},
framework::Framework,
};
use parking_lot::RwLock;
use std::sync::Arc;
use typemap::ShareMap;
pub struct ClientBuilder {
hyper_client: Option<hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>>,
api_client: Option<Arc<Box<APIConnector>>>,
webhook: Option<WebhookOptions>,
framework: Option<Arc<Framework>>,
token: Option<String>,
allowed_updates: Vec<UpdateType>,
event_handler_funcs: Vec<EventHandlerFunc>,
raw_event_handler_funcs: Vec<RawEventHandlerFunc>,
}
impl ClientBuilder {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
api_client: None,
hyper_client: None,
webhook: None,
framework: None,
token: None,
allowed_updates: Vec::new(),
event_handler_funcs: Vec::new(),
raw_event_handler_funcs: Vec::new(),
}
}
pub fn set_webhook(&mut self, webhook: &WebhookOptions) -> &mut Self {
self.webhook = Some(webhook.clone());
self
}
pub fn set_framework(&mut self, framework: Arc<Framework>) -> &mut Self {
self.framework = Some(framework);
self
}
pub fn set_token(&mut self, token: &str) -> &mut Self {
self.token = Some(token.to_owned());
self
}
pub fn set_hyper_client(
&mut self,
client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
) -> &mut Self {
self.hyper_client = Some(client);
self
}
pub fn set_api_client(&mut self, client: Arc<Box<APIConnector>>) -> &mut Self {
self.api_client = Some(client);
self
}
pub fn set_allowed_updates(&mut self, allowed: Vec<UpdateType>) -> &mut Self {
self.allowed_updates = allowed;
self
}
pub fn add_allowed_updates(&mut self, allowed: UpdateType) -> &mut Self {
self.allowed_updates.push(allowed);
self
}
pub fn remove_allowed_updates(&mut self, denied: &UpdateType) -> &mut Self {
self.allowed_updates.retain(|t| t != denied);
self
}
pub fn add_handler_func(&mut self, handler: EventHandlerFunc) -> &mut Self {
self.event_handler_funcs.push(handler);
self
}
pub fn add_raw_handler_func(&mut self, handler: RawEventHandlerFunc) -> &mut Self {
self.raw_event_handler_funcs.push(handler);
self
}
pub fn build(&mut self) -> Client {
if self.framework.is_some() && !self.allowed_updates.contains(&UpdateType::Message) {
self.allowed_updates.push(UpdateType::Message)
}
if let Some(c) = self.api_client.clone() {
Client {
api_client: c,
event_handlers: self.event_handler_funcs.clone(),
raw_event_handlers: self.raw_event_handler_funcs.clone(),
data: Arc::new(RwLock::new(ShareMap::custom())),
framework: self.framework.clone(),
webhook_opts: self.webhook.clone(),
allowed_updates: self.allowed_updates.clone(),
}
} else {
Client {
api_client: Arc::new(Box::new(APIClient::new(
self.hyper_client.clone(),
self.token
.clone()
.expect("A token must be provided for the telegram bot to work"),
))),
event_handlers: self.event_handler_funcs.clone(),
webhook_opts: self.webhook.clone(),
raw_event_handlers: self.raw_event_handler_funcs.clone(),
data: Arc::new(RwLock::new(ShareMap::custom())),
framework: self.framework.clone(),
allowed_updates: self.allowed_updates.clone(),
}
}
}
}