Skip to content

Commit

Permalink
Support API changes in Message, SendMessage, and SendSticker to fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfe committed May 4, 2024
1 parent f2adfe0 commit cd826c5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/lib/api/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn escape_md(text: &str) -> String {
let escapes = vec![
let escapes = [
'_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!',
];

Expand All @@ -16,7 +16,7 @@ pub fn escape_md(text: &str) -> String {
}

pub fn escape_code(text: &str) -> String {
let escapes = vec!['`', '\\'];
let escapes = ['`', '\\'];

let mut result = String::new();

Expand Down
32 changes: 30 additions & 2 deletions src/lib/api/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct Message {

/// For replies, the original message. Note that the Message object in this field will not contain further `reply_to_message` fields even if it itself is a reply.
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to_message: Option<i64>,
pub reply_to_message: Option<serde_json::value::Value>,

/// Sticker for messages with a sticker
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -104,6 +104,34 @@ pub enum ParseMode {
Text,
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct ReplyParameters {
// Identifier of the original message
pub message_id: i64,

// Unique identifier for the target chat or username of the target channel. This
// field is an i64 OR a String, so we use serde_json::value::Value.
#[serde(skip_serializing_if = "Option::is_none")]
pub chat_id: Option<serde_json::value::Value>,

// Allow sending the message without a reply
#[serde(skip_serializing_if = "Option::is_none")]
pub allow_sending_without_reply: Option<bool>,

// Quoted part of the message to be replied to.
#[serde(skip_serializing_if = "Option::is_none")]
pub quote: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub quote_parse_mode: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub quote_entities: Option<serde_json::value::Value>,

#[serde(skip_serializing_if = "Option::is_none")]
pub quote_position: Option<i64>,
}

#[derive(Default, Debug, Serialize, Deserialize, Clone, BotRequest)]
pub struct SendMessageRequest {
/// Unique identifier for the target chat or username of the target
Expand All @@ -114,7 +142,7 @@ pub struct SendMessageRequest {

/// If the message is a reply, ID of the original message
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to_message_id: Option<i64>,
pub reply_parameters: Option<ReplyParameters>,

/// Parse mode for the message
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
10 changes: 5 additions & 5 deletions src/lib/api/sticker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mobot_derive::BotRequest;
use serde::{Deserialize, Serialize};

use super::{message::Message, API};
use super::{message::Message, ReplyParameters, API};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Sticker {
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct SendStickerRequest {

/// If the message is a reply, ID of the original message
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to_message_id: Option<i64>,
pub reply_parameters: Option<ReplyParameters>,
}

impl SendStickerRequest {
Expand All @@ -50,12 +50,12 @@ impl SendStickerRequest {
chat_id,
sticker,
disable_notification: None,
reply_to_message_id: None,
reply_parameters: None,
}
}

pub fn with_reply_to_message_id(mut self, reply_to_message_id: i64) -> Self {
self.reply_to_message_id = Some(reply_to_message_id);
pub fn with_reply_parameters(mut self, reply_parameters: ReplyParameters) -> Self {
self.reply_parameters = Some(reply_parameters);
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl FakeAPI {
let mut message = api::Message::fake(self.bot_name.as_str());
message.chat.id = req.chat_id;
message.text = Some(req.text);
message.reply_to_message = req.reply_to_message_id;
message.reply_to_message = None;

if let Some(chat) = self.chat_map.lock().await.get(&req.chat_id) {
chat.send(Update::Message(message.clone())).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum ProgressState<'a> {
/// This method generates the progress bar string out of unicode block characters.
fn progress_str(i: i64, state: ProgressState) -> String {
// Set of horizontal block characters of increasing size.
let blocks = vec![
let blocks = [
'\u{258F}', '\u{258E}', '\u{258D}', '\u{258C}', '\u{258B}', '\u{258A}', '\u{2589}',
];

Expand Down
27 changes: 12 additions & 15 deletions src/lib/update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::api::{self, PhotoSize, Document};
use crate::api::{self, Document, PhotoSize};
use anyhow::anyhow;
use std::fmt;

/// `Update` represents a new update from Telegram
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -60,17 +61,16 @@ impl From<Update> for api::CallbackQuery {
}
}
}

impl ToString for Update {
fn to_string(&self) -> String {
impl fmt::Display for Update {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Update::*;
match self {
Message(msg) => msg.text.clone().unwrap(),
EditedMessage(msg) => msg.text.clone().unwrap(),
ChannelPost(msg) => msg.text.clone().unwrap(),
EditedChannelPost(msg) => msg.text.clone().unwrap(),
CallbackQuery(query) => query.data.clone().unwrap(),
InlineQuery(query) => query.query.clone(),
Message(msg) => write!(f, "{}", msg.text.clone().unwrap()),
EditedMessage(msg) => write!(f, "{}", msg.text.clone().unwrap()),
ChannelPost(msg) => write!(f, "{}", msg.text.clone().unwrap()),
EditedChannelPost(msg) => write!(f, "{}", msg.text.clone().unwrap()),
CallbackQuery(query) => write!(f, "{}", query.data.clone().unwrap()),
InlineQuery(query) => write!(f, "{}", query.query.clone()),
Unknown => {
panic!("Bad Message::Unknown")
}
Expand Down Expand Up @@ -182,11 +182,8 @@ impl Update {
}

pub fn photo(&self) -> anyhow::Result<&Vec<PhotoSize>> {
self.message().and_then(|msg| {
msg.photo
.as_ref()
.ok_or(anyhow!("message has no photo"))
})
self.message()
.and_then(|msg| msg.photo.as_ref().ok_or(anyhow!("message has no photo")))
}

pub fn document(&self) -> anyhow::Result<&Document> {
Expand Down

0 comments on commit cd826c5

Please sign in to comment.