use serde::Serialize;
use crate::{
api::{Form, Method, Payload, PayloadError, WriteForm},
types::{
ChatId,
InlineKeyboardMarkup,
InputMedia,
InputMediaData,
InputText,
InputTextCaption,
Integer,
LinkPreviewOptions,
},
};
#[derive(Clone, Debug, Serialize)]
pub struct EphemeralMessageIdentity {
chat_id: ChatId,
receiver_user_id: Integer,
ephemeral_message_id: Integer,
}
impl<T> From<(T, Integer, Integer)> for EphemeralMessageIdentity
where
T: Into<ChatId>,
{
fn from((chat_id, receiver_user_id, ephemeral_message_id): (T, Integer, Integer)) -> Self {
Self {
chat_id: chat_id.into(),
receiver_user_id,
ephemeral_message_id,
}
}
}
#[derive(Clone, Debug)]
pub struct DeleteEphemeralMessage {
identity: EphemeralMessageIdentity,
}
impl<T> From<T> for DeleteEphemeralMessage
where
T: Into<EphemeralMessageIdentity>,
{
fn from(value: T) -> Self {
Self { identity: value.into() }
}
}
impl Method for DeleteEphemeralMessage {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("deleteEphemeralMessage", self.identity)
}
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct EditEphemeralMessageCaption {
#[serde(flatten)]
identity: EphemeralMessageIdentity,
#[serde(flatten)]
caption: Option<InputTextCaption>,
reply_markup: Option<InlineKeyboardMarkup>,
}
impl<T> From<T> for EditEphemeralMessageCaption
where
T: Into<EphemeralMessageIdentity>,
{
fn from(value: T) -> Self {
Self {
identity: value.into(),
caption: None,
reply_markup: None,
}
}
}
impl EditEphemeralMessageCaption {
pub fn with_caption<T>(mut self, value: T) -> Self
where
T: Into<InputTextCaption>,
{
self.caption = Some(value.into());
self
}
pub fn with_reply_markup<T>(mut self, value: T) -> Self
where
T: Into<InlineKeyboardMarkup>,
{
self.reply_markup = Some(value.into());
self
}
}
impl Method for EditEphemeralMessageCaption {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("editEphemeralMessageCaption", self)
}
}
#[derive(Debug)]
pub struct EditEphemeralMessageMedia {
identity: EphemeralMessageIdentity,
media: InputMedia,
reply_markup: Option<InlineKeyboardMarkup>,
}
impl EditEphemeralMessageMedia {
pub fn new<A, B>(identity: A, media: B) -> Self
where
A: Into<EphemeralMessageIdentity>,
B: Into<InputMedia>,
{
Self {
identity: identity.into(),
media: media.into(),
reply_markup: None,
}
}
pub fn with_reply_markup<T>(mut self, value: T) -> Self
where
T: Into<InlineKeyboardMarkup>,
{
self.reply_markup = Some(value.into());
self
}
}
#[serde_with::skip_serializing_none]
#[derive(Debug, Serialize)]
struct EditEphemeralMessageMediaParameters {
#[serde(flatten)]
identity: EphemeralMessageIdentity,
media: InputMediaData,
reply_markup: Option<InlineKeyboardMarkup>,
}
impl Method for EditEphemeralMessageMedia {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
let Self {
identity,
media,
reply_markup,
} = self;
let mut form = Form::default();
let parameters = EditEphemeralMessageMediaParameters {
identity,
media: media.write(&mut form),
reply_markup,
};
parameters.serialize(&mut form)?;
Payload::form("editEphemeralMessageMedia", form)
}
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct EditEphemeralMessageReplyMarkup {
#[serde(flatten)]
identity: EphemeralMessageIdentity,
reply_markup: Option<InlineKeyboardMarkup>,
}
impl<T> From<T> for EditEphemeralMessageReplyMarkup
where
T: Into<EphemeralMessageIdentity>,
{
fn from(value: T) -> Self {
Self {
identity: value.into(),
reply_markup: None,
}
}
}
impl EditEphemeralMessageReplyMarkup {
pub fn with_reply_markup<T>(mut self, value: T) -> Self
where
T: Into<InlineKeyboardMarkup>,
{
self.reply_markup = Some(value.into());
self
}
}
impl Method for EditEphemeralMessageReplyMarkup {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("editEphemeralMessageReplyMarkup", self)
}
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct EditEphemeralMessageText {
#[serde(flatten)]
identity: EphemeralMessageIdentity,
#[serde(flatten)]
text: InputText,
link_preview_options: Option<LinkPreviewOptions>,
reply_markup: Option<InlineKeyboardMarkup>,
}
impl EditEphemeralMessageText {
pub fn new<A, B>(identity: A, text: B) -> Self
where
A: Into<EphemeralMessageIdentity>,
B: Into<InputText>,
{
Self {
identity: identity.into(),
text: text.into(),
link_preview_options: None,
reply_markup: None,
}
}
pub fn with_link_preview_options(mut self, value: LinkPreviewOptions) -> Self {
self.link_preview_options = Some(value);
self
}
pub fn with_reply_markup<T>(mut self, value: T) -> Self
where
T: Into<InlineKeyboardMarkup>,
{
self.reply_markup = Some(value.into());
self
}
}
impl Method for EditEphemeralMessageText {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("editEphemeralMessageText", self)
}
}