telegram_bot_raw/requests/
pin_chat_message.rs

1use std::ops::Not;
2
3use crate::requests::*;
4use crate::types::*;
5
6/// Use this method to pin a message in a supergroup or a channel.
7/// The bot must be an administrator in the chat for this to work
8/// and must have the ‘can_pin_messages’ admin right in the supergroup
9/// or ‘can_edit_messages’ admin right in the channel.
10#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
11#[must_use = "requests do nothing unless sent"]
12pub struct PinChatMessage {
13    chat_id: ChatRef,
14    message_id: MessageId,
15    #[serde(skip_serializing_if = "Not::not")]
16    disable_notification: bool,
17}
18
19impl Request for PinChatMessage {
20    type Type = JsonRequestType<Self>;
21    type Response = JsonTrueToUnitResponse;
22
23    fn serialize(&self) -> Result<HttpRequest, Error> {
24        Self::Type::serialize(RequestUrl::method("pinChatMessage"), self)
25    }
26}
27
28impl PinChatMessage {
29    fn new<C, M>(chat: C, message: M) -> Self
30    where
31        C: ToChatRef,
32        M: ToMessageId,
33    {
34        Self {
35            chat_id: chat.to_chat_ref(),
36            message_id: message.to_message_id(),
37            disable_notification: false,
38        }
39    }
40
41    pub fn disable_notification(&mut self) -> &mut Self {
42        self.disable_notification = true;
43        self
44    }
45}
46
47pub trait CanPinMessage {
48    fn pin(&self) -> PinChatMessage;
49}
50
51impl<M> CanPinMessage for M
52where
53    M: ToMessageId + ToSourceChat,
54{
55    fn pin(&self) -> PinChatMessage {
56        PinChatMessage::new(self.to_source_chat(), self.to_message_id())
57    }
58}