use std::fmt::{Display, Formatter, Result as FmtResult};
use serde::Serialize;
use serde_json::Value;
use crate::network::NetworkTask;
#[derive(Debug, Clone, Serialize)]
pub struct TextMessage {
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_markup: Option<String>,
}
impl TextMessage {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
reply_markup: None,
}
}
pub fn with_reply_markup(mut self, markup: impl Into<String>) -> Self {
self.reply_markup = Some(markup.into());
self
}
pub fn to_json_value(&self, chat_id: String) -> Value {
let mut value = serde_json::to_value(self)
.expect("Failed to serialize TextMessage");
if let Some(obj) = value.as_object_mut() {
obj.entry("parse_mode")
.or_insert_with(|| "MarkdownV2".into());
obj.entry("chat_id").or_insert_with(|| chat_id.into());
}
value
}
pub fn into_task(self, chat_id: String) -> NetworkTask {
NetworkTask::RequestJson(self.to_json_value(chat_id))
}
}
impl Display for TextMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "text={}", self.text)
}
}