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
use super::base::{Request, TelegramMethod};

use crate::{
    client::Bot,
    types::{ChatIdKind, ReactionType},
};

use serde::Serialize;
use serde_with::skip_serializing_none;

/// Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. In albums, bots must react to the first message.
/// # Documentation
/// <https://core.telegram.org/bots/api#setmessagereaction>
/// # Returns
/// Returns `true` on success
#[skip_serializing_none]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize)]
pub struct SetMessageReaction {
    /// Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)
    pub chat_id: ChatIdKind,
    /// Identifier of the target message
    pub message_id: i64,
    /// New list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators.
    pub reaction: Option<Vec<ReactionType>>,
    /// Pass `true` to set the reaction with a big animation
    pub is_big: Option<bool>,
}

impl SetMessageReaction {
    #[must_use]
    pub fn new(chat_id: impl Into<ChatIdKind>, message_id: i64) -> Self {
        Self {
            chat_id: chat_id.into(),
            message_id,
            reaction: None,
            is_big: None,
        }
    }

    #[must_use]
    pub fn chat_id(self, val: impl Into<ChatIdKind>) -> Self {
        Self {
            chat_id: val.into(),
            ..self
        }
    }

    #[must_use]
    pub fn message_id(self, val: i64) -> Self {
        Self {
            message_id: val,
            ..self
        }
    }

    #[must_use]
    pub fn reaction(self, val: impl Into<ReactionType>) -> Self {
        Self {
            reaction: Some(
                self.reaction
                    .unwrap_or_default()
                    .into_iter()
                    .chain(Some(val.into()))
                    .collect(),
            ),
            ..self
        }
    }

    #[must_use]
    pub fn reactions<T, I>(self, val: I) -> Self
    where
        T: Into<ReactionType>,
        I: IntoIterator<Item = T>,
    {
        Self {
            reaction: Some(
                self.reaction
                    .unwrap_or_default()
                    .into_iter()
                    .chain(val.into_iter().map(Into::into))
                    .collect(),
            ),
            ..self
        }
    }

    #[must_use]
    pub fn is_big(self, val: bool) -> Self {
        Self {
            is_big: Some(val),
            ..self
        }
    }
}

impl SetMessageReaction {
    #[must_use]
    pub fn reaction_option(self, val: Option<impl Into<ReactionType>>) -> Self {
        Self {
            reaction: val.map(|val| {
                self.reaction
                    .unwrap_or_default()
                    .into_iter()
                    .chain(Some(val.into()))
                    .collect()
            }),
            ..self
        }
    }

    #[must_use]
    pub fn reactions_option<T, I>(self, val: Option<I>) -> Self
    where
        T: Into<ReactionType>,
        I: IntoIterator<Item = T>,
    {
        Self {
            reaction: val.map(|val| {
                self.reaction
                    .unwrap_or_default()
                    .into_iter()
                    .chain(val.into_iter().map(Into::into))
                    .collect()
            }),
            ..self
        }
    }

    #[must_use]
    pub fn is_big_option(self, val: Option<bool>) -> Self {
        Self {
            is_big: val,
            ..self
        }
    }
}

impl TelegramMethod for SetMessageReaction {
    type Method = Self;
    type Return = bool;

    fn build_request<Client>(&self, _bot: &Bot<Client>) -> Request<Self::Method> {
        Request::new("setMessageReaction", self, None)
    }
}

impl AsRef<SetMessageReaction> for SetMessageReaction {
    fn as_ref(&self) -> &Self {
        self
    }
}