telers 1.0.0-beta.2

An asynchronous framework for Telegram Bot API written in Rust
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use super::{Error, Storage, StorageKey};

use deadpool_redis::{Config, ConfigError, Connection, CreatePoolError, Pool, PoolError, Runtime};
use redis::{IntoConnectionInfo, RedisError};
use serde::{de::DeserializeOwned, Serialize};
use std::{
    collections::HashMap,
    fmt::{self, Debug, Display, Formatter},
};
use tracing::{event, field, instrument, Level, Span};

const DEFAULT_PREFIX: &str = "fsm";
const DEFAULT_SEPARATOR: &str = ":";

#[derive(Debug)]
pub enum Part {
    States,
    Data,
}

impl Part {
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Part::States => "states",
            Part::Data => "data",
        }
    }
}

impl Display for Part {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

pub trait KeyBuilder: Send + Sync {
    /// Build redis key for specified key and part
    /// # Arguments
    /// * `key` - Specified key to build key
    /// * `part` - Specified part to build key
    /// # Returns
    /// Redis key for specified key and part
    #[must_use]
    fn build(&self, key: &StorageKey, part: Part) -> Box<str>;
}

/// This is a default key builder implementation
/// # Notes
/// By default, this key builder will use `fsm` as prefix and `:` as separator,
/// if you want to set custom prefix and separator,
/// then you can use methods like [`KeyBuilderImpl::with_prefix`] and [`KeyBuilderImpl::with_separator`].
#[derive(Debug, Clone)]
pub struct KeyBuilderImpl {
    prefix: &'static str,
    separator: &'static str,
    with_bot_id: bool,
    with_destiny: bool,
}

impl KeyBuilderImpl {
    #[inline]
    #[must_use]
    pub fn new(
        prefix: &'static str,
        separator: &'static str,
        with_bot_id: bool,
        with_destiny: bool,
    ) -> Self {
        Self {
            prefix,
            separator,
            with_bot_id,
            with_destiny,
        }
    }

    #[inline]
    #[must_use]
    pub fn with_prefix(self, prefix: &'static str) -> Self {
        Self {
            prefix,
            ..self
        }
    }

    #[inline]
    #[must_use]
    pub fn with_separator(self, separator: &'static str) -> Self {
        Self {
            separator,
            ..self
        }
    }

    #[inline]
    #[must_use]
    pub fn with_bot_id(self, with_bot_id: bool) -> Self {
        Self {
            with_bot_id,
            ..self
        }
    }

    #[inline]
    #[must_use]
    pub fn with_destiny(self, with_destiny: bool) -> Self {
        Self {
            with_destiny,
            ..self
        }
    }
}

impl Default for KeyBuilderImpl {
    #[inline]
    fn default() -> Self {
        Self::new(DEFAULT_PREFIX, DEFAULT_SEPARATOR, true, true)
    }
}

impl KeyBuilder for KeyBuilderImpl {
    fn build(&self, key: &StorageKey, part: Part) -> Box<str> {
        let bot_id = key.bot_id.to_string();
        let chat_id = key.chat_id.to_string();
        let user_id = key.user_id.to_string();
        let message_thread_id = key
            .message_thread_id
            .map(|message_thread_id| message_thread_id.to_string());

        let mut parts = vec![];

        parts.push(self.prefix);
        if self.with_destiny {
            parts.push(key.destiny);
        }
        if self.with_bot_id {
            parts.push(&bot_id);
        }

        parts.push(&chat_id);
        if let Some(message_thread_id) = &message_thread_id {
            parts.push(message_thread_id);
        }
        if let Some(ref business_connection_id) = key.business_connection_id {
            parts.push(business_connection_id);
        }

        parts.push(&user_id);
        parts.push(part.as_str());

        parts.join(self.separator).into_boxed_str()
    }
}

/// This is a thread-safe storage implementation for redis
#[derive(Clone)]
pub struct Redis<K = KeyBuilderImpl> {
    pool: Pool,
    key_builder: K,
}

impl<K: KeyBuilder> Redis<K> {
    /// # Notes
    /// This method will use custom key builder,
    /// if you want to use default key builder, then you can use [`Redis::new`] method instead
    /// # Errors
    /// This method will return error if config is invalid
    pub fn new_with_key_builder<T>(connection_info: T, key_builder: K) -> Result<Self, RedisError>
    where
        T: IntoConnectionInfo,
    {
        let config = Config::from_connection_info(connection_info.into_connection_info()?);
        let pool = match config.create_pool(Some(Runtime::Tokio1)) {
            Ok(pool) => pool,
            Err(err) => match err {
                CreatePoolError::Config(err) => match err {
                    ConfigError::UrlAndConnectionSpecified => unreachable!(
                        "This error should not be occurred because we use `IntoConnectionInfo` \
                         where it will use only one of them.If you see this error, then report it \
                         to the library maintainer."
                    ),
                    ConfigError::Redis(err) => return Err(err),
                },
                CreatePoolError::Build(_) => unreachable!(
                    "This error should not be occurred because we specify runtime in \
                     `create_pool` method.If you see this error, then report it to the library \
                     maintainer."
                ),
            },
        };

        Ok(Self {
            pool,
            key_builder,
        })
    }

    #[inline]
    #[must_use]
    pub fn key_builder(self, key_builder: K) -> Self {
        Self {
            key_builder,
            ..self
        }
    }
}

impl Redis {
    /// # Notes
    /// By default, this method will use [`KeyBuilderImpl`] as key builder,
    /// if you want to use custom key builder, then you can use [`Redis::new_with_key_builder`] method instead
    /// or you can use [`Redis::key_builder`] method to set custom key builder
    /// # Errors
    /// This method will return error if config is invalid
    pub fn new<T>(connection_info: T) -> Result<Self, RedisError>
    where
        T: IntoConnectionInfo,
    {
        Self::new_with_key_builder(connection_info, KeyBuilderImpl::default())
    }
}

impl<K> Redis<K> {
    async fn get_connection(&self) -> Result<Connection, PoolError> {
        self.pool.get().await
    }
}

impl<K: KeyBuilder + Clone> Storage for Redis<K> {
    type Error = Error;

    /// Set state for specified key
    /// # Arguments
    /// * `key` - Specified key to set state
    /// * `state` - State for specified key
    #[instrument(skip(self, key, state), fields(key, state))]
    async fn set_state<S>(&self, key: &StorageKey, state: S) -> Result<(), Self::Error>
    where
        S: AsRef<str> + Send,
    {
        let key = self.key_builder.build(key, Part::States);

        Span::current()
            .record("key", key.as_ref())
            .record("state", state.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("RPUSH")
            .arg(key.as_ref())
            .arg(state.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to set state");

                Error::new(format!("Failed to set state. Storage key: {key}"), err)
            })
    }

    /// Set previous state as current state
    /// # Arguments
    /// * `key` - Specified key to set previous state
    /// # Notes
    /// States stack is used to store states history,
    /// when user set new state, then current state will be push to the states stack,
    /// so you can use this method to back to the previous state
    #[instrument(skip(self, key), fields(key))]
    async fn set_previous_state(&self, key: &StorageKey) -> Result<(), Self::Error> {
        let key = self.key_builder.build(key, Part::States);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("RPOP")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to remove state");

                Error::new(format!("Failed to remove state. Storage key: {key}"), err)
            })
    }

    /// Get state for specified key
    /// # Arguments
    /// * `key` - Specified key to get state
    /// # Returns
    /// State for specified key, if state is no exists, then `None` will be return
    #[instrument(skip(self, key), fields(key))]
    async fn get_state(&self, key: &StorageKey) -> Result<Option<Box<str>>, Self::Error> {
        let key = self.key_builder.build(key, Part::States);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("LINDEX")
            .arg(key.as_ref())
            .arg(-1)
            .query_async::<Option<String>>(&mut connection)
            .await
            .map(|state| state.map(Into::into))
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to get state");

                Error::new(format!("Failed to get state. Storage key: {key}"), err)
            })
    }

    /// Get states stack for specified key
    /// # Arguments
    /// * `key` - Specified key to get states stack
    /// # Note
    /// States stack is used to store states history,
    /// when user set new state, then current state will be push to the states stack,
    /// so you can use this method to get states history or back to the previous state
    /// # Returns
    /// States stack for specified key, if states stack is no exists, then empty slice will be return
    #[instrument(skip(self, key), fields(key))]
    async fn get_states(&self, key: &StorageKey) -> Result<Box<[Box<str>]>, Self::Error> {
        let key = self.key_builder.build(key, Part::States);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("LRANGE")
            .arg(key.as_ref())
            .arg(0)
            .arg(-1)
            .query_async::<Vec<String>>(&mut connection)
            .await
            .map(|states| states.into_iter().map(Into::into).collect())
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to get states");

                Error::new(format!("Failed to get states. Storage key: {key}"), err)
            })
    }

    /// Remove states stack for specified key
    /// # Arguments
    /// * `key` - Specified key to remove states stack
    /// # Note
    /// States stack is used to store states history,
    /// when user set new state, then current state will be push to the states stack,
    /// so you can use this method to clear states history
    #[instrument(skip(self, key), fields(key))]
    async fn remove_states(&self, key: &StorageKey) -> Result<(), Self::Error> {
        let key = self.key_builder.build(key, Part::States);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("DEL")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to remove states");

                Error::new(format!("Failed to remove states. Storage key: {key}"), err)
            })
    }

    /// Set data for specified key
    /// # Arguments
    /// * `key` - Specified key to set data
    /// * `data` - Data for specified key, if empty, then data will be clear
    #[instrument(skip(self, key, data), fields(key))]
    async fn set_data<Key, Value>(
        &self,
        key: &StorageKey,
        data: HashMap<Key, Value>,
    ) -> Result<(), Self::Error>
    where
        Value: Serialize + Send,
        Key: AsRef<str> + Send,
    {
        let key = self.key_builder.build(key, Part::Data);

        Span::current().record("key", key.as_ref());

        let plain_json = serde_json::to_string(
            &data
                .into_iter()
                .map(|(k, v)| (k.as_ref().to_owned(), v))
                .collect::<HashMap<_, _>>(),
        )
        .map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to serialize data");

            Error::new(format!("Failed to serialize data. Storage key: {key}"), err)
        })?;
        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("SET")
            .arg(key.as_ref())
            .arg(plain_json)
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to set data");

                Error::new(format!("Failed to set data. Storage key: {key}"), err)
            })
    }

    /// Set value to the data for specified key and value key
    /// # Arguments
    /// * `key` - Specified key to set data
    /// * `value_key` - Specified value key to set value to the data
    /// * `value` - Value for specified key and value key
    #[instrument(skip(self, key, value_key, value), fields(key, value_key, data))]
    async fn set_value<Key, Value>(
        &self,
        key: &StorageKey,
        value_key: Key,
        value: Value,
    ) -> Result<(), Self::Error>
    where
        Value: Serialize + Send,
        Key: AsRef<str> + Send,
    {
        let key = self.key_builder.build(key, Part::Data);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        let plain_json: Option<String> = redis::cmd("GET")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to get data");

                Error::new(format!("Failed to get data. Storage key: {key}"), err)
            })?;

        let mut data = match plain_json {
            Some(ref plain_json) => serde_json::from_str(plain_json).map_err(|err| {
                event!(
                    Level::ERROR,
                    error = %err,
                    json = %plain_json,
                    "Failed to deserialize data",
                );

                Error::new(
                    format!("Failed to deserialize data. Storage key: {key}"),
                    err,
                )
            })?,
            None => HashMap::with_capacity(1),
        };

        Span::current().record("value_key", value_key.as_ref());

        data.insert(
            value_key.as_ref(),
            serde_json::to_value(value).map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to convert value to `serde_json::Value`");

                Error::new(
                    format!("Failed to convert value to `serde_json::Value`. Storage key: {key}"),
                    err,
                )
            })?,
        );

        Span::current().record("data", field::debug(&data));

        let plain_json = serde_json::to_string(&data).map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to serialize data");

            Error::new(format!("Failed to serialize data. Storage key: {key}"), err)
        })?;

        redis::cmd("SET")
            .arg(key.as_ref())
            .arg(plain_json)
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to set data");

                Error::new(format!("Failed to set data. Storage key: {key}"), err)
            })
    }

    /// Get data for specified key
    /// # Arguments
    /// * `key` - Specified key to get data
    /// # Returns
    /// Data for specified key, if data is no exists, then empty [`HashMap`] will be return
    #[instrument(skip(self, key))]
    async fn get_data<Value>(
        &self,
        key: &StorageKey,
    ) -> Result<HashMap<Box<str>, Value>, Self::Error>
    where
        Value: DeserializeOwned,
    {
        let key = self.key_builder.build(key, Part::Data);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        let plain_json: Option<String> = redis::cmd("GET")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to get data");

                Error::new(format!("Failed to get data. Storage key: {key}"), err)
            })?;

        match plain_json {
            Some(ref plain_json) => serde_json::from_str(plain_json).map_err(|err| {
                event!(
                    Level::ERROR,
                    error = %err,
                    json = %plain_json,
                    "Failed to deserialize data",
                );

                Error::new(
                    format!("Failed to deserialize data. Storage key: {key}"),
                    err,
                )
            }),
            None => Ok(HashMap::default()),
        }
    }

    /// Get value from the data for specified key and value key
    /// # Arguments
    /// * `key` - Specified key to get data
    /// * `value_key` - Specified value key to get value from the data
    /// # Returns
    /// Value for specified key and value key, if value is no exists, then `None` will be return
    #[instrument(skip(self, key, value_key), fields(key))]
    async fn get_value<Key, Value>(
        &self,
        key: &StorageKey,
        value_key: Key,
    ) -> Result<Option<Value>, Self::Error>
    where
        Value: DeserializeOwned,
        Key: AsRef<str> + Send,
    {
        let key = self.key_builder.build(key, Part::Data);

        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        let plain_json: Option<String> = redis::cmd("GET")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to get data");

                Error::new(format!("Failed to get data. Storage key: {key}"), err)
            })?;

        match plain_json {
            Some(ref plain_json) => {
                let mut data: HashMap<Box<str>, serde_json::Value> =
                    serde_json::from_str(plain_json).map_err(|err| {
                        event!(
                            Level::ERROR,
                            error = %err,
                            json = %plain_json,
                            "Failed to deserialize data",
                        );

                        Error::new(
                            format!("Failed to deserialize data. Storage key: {key}"),
                            err,
                        )
                    })?;

                match data.remove(value_key.as_ref()) {
                    Some(value) => {
                        let value_str = value.to_string();
                        let res = serde_json::from_value(value)
                            .map_err(|err| {
                                event!(
                                    Level::ERROR,
                                    error = %err,
                                    value = %value_str,
                                    "Failed to convert `serde_json::Value` to value",
                                );

                                Error::new(
                                    format!(
                                        "Failed to convert `serde_json::Value` to value. Storage \
                                         key: {key}"
                                    ),
                                    err,
                                )
                            })
                            .map(Some);
                        res
                    }
                    None => Ok(None),
                }
            }
            None => Ok(None),
        }
    }

    /// Remove data for specified key
    /// # Arguments
    /// * `key` - Specified key to remove data
    #[instrument(skip(self, key), fields(key))]
    async fn remove_data(&self, key: &StorageKey) -> Result<(), Self::Error> {
        let key = self.key_builder.build(key, Part::Data);
        Span::current().record("key", key.as_ref());

        let mut connection = self.get_connection().await.map_err(|err| {
            event!(Level::ERROR, error = %err, "Failed to get redis connection");

            Error::new(
                format!("Failed to get redis connection. Storage key: {key}"),
                err,
            )
        })?;

        redis::cmd("DEL")
            .arg(key.as_ref())
            .query_async(&mut connection)
            .await
            .map_err(|err| {
                event!(Level::ERROR, error = %err, "Failed to remove data");

                Error::new(format!("Failed to remove data. Storage key: {key}"), err)
            })
    }
}