use std::fmt::{Display, Formatter, Result as FmtResult};
use serde::Serialize;
use serde_json::{Map, 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 object = Map::with_capacity(4);
object.insert("text".to_string(), Value::String(self.text.clone()));
object.insert(
"parse_mode".to_string(),
Value::String("MarkdownV2".to_string()),
);
object.insert("chat_id".to_string(), Value::String(chat_id));
if let Some(reply_markup) = &self.reply_markup {
object.insert(
"reply_markup".to_string(),
Value::String(reply_markup.clone()),
);
}
Value::Object(object)
}
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)
}
}