Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box message-kind types to avoid stack overflow #4

Merged
merged 7 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ input_file = { path = "./examples/input_file" }
finite_state_machine = { path = "./examples/finite_state_machine" }
router_tree = { path = "./examples/router_tree" }
bot_http_client = { path = "./examples/bot_http_client" }
axum_and_echo_bot = { path = "./examples/axum_and_echo_bot" }
axum_and_echo_bot = { path = "./examples/axum_and_echo_bot" }
79 changes: 79 additions & 0 deletions src/client/session/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ pub trait Session: Send + Sync {
err
})?;

event!(
Level::TRACE,
content = response.content,
status_code = response.status_code.as_u16(),
"Got response. Parsing it...",
);

let telegram_response =
method
.build_response(response.content.as_ref())
Expand All @@ -253,6 +260,8 @@ pub trait Session: Send + Sync {
err
})?;

event!(Level::TRACE, "Response parsed successfully",);

self.check_response(&telegram_response, &response.status_code)
.map_err(|err| {
event!(
Expand Down Expand Up @@ -299,3 +308,73 @@ pub trait Session: Send + Sync {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::methods::SendMessage;

use serde_json::json;

#[test]
fn build_response() {
let method = SendMessage::new(810646651, "Hello, abc!");

let content = json!(
{
"ok": true,
"result": {
"message_id": 423,
"from": {
"id": 1 as i64,
"is_bot": true,
"first_name": "test",
"username": "test"
},
"chat": {
"id": 1,
"first_name": "test",
"username": "test",
"type": "private",
},
"date": 1706267365,
"reply_to_message": {
"message_id": 422,
"from": {
"id": 1,
"is_bot": false,
"first_name": "test",
"username": "test",
"language_code": "ru",
"is_premium": true,
},
"chat":{
"id": 1,
"first_name": "test",
"username": "test",
"type": "private",
},
"date": 1,
"text": "/start",
"entities":[
{
"offset": 0,
"length": 6,
"type": "bot_command",
},
],
},
"text": "test",
},
"statud_code": 200,
});

let result = method
.build_response(&content.to_string())
.unwrap()
.result
.unwrap();

assert_eq!(result.id(), 423);
}
}
4 changes: 2 additions & 2 deletions src/event/telegram/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ mod tests {
let request = Request::new(
request.bot,
Arc::new(Update {
kind: UpdateKind::Message(Message::Text(MessageText {
kind: UpdateKind::Message(Message::Text(Box::new(MessageText {
text: "/start".to_owned().into(),
..Default::default()
})),
}))),
..Default::default()
}),
request.context,
Expand Down
1 change: 0 additions & 1 deletion src/methods/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
};

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json;

/// This object represents a request to Telegram API
pub struct Request<'a, T>
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/outer/user_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ mod tests {
let bot = Bot::<Reqwest>::default();
let context = Context::new();
let update = Update {
kind: UpdateKind::Message(Message::Text(MessageText {
kind: UpdateKind::Message(Message::Text(Box::new(MessageText {
from: Some(User::default()),
thread_id: Some(1),
..Default::default()
})),
}))),
..Default::default()
};

Expand Down
8 changes: 4 additions & 4 deletions src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Private {
/// `true`, if the privacy settings of the other party restrict sending voice and video note messages. Returned only in [`GetChat`](crate::methods::GetChat).
pub has_restricted_voice_and_video_messages: Option<bool>,
/// The most recent pinned message (by sending date). Returned only in [`GetChat`](crate::methods::GetChat).
pub pinned_message: Option<Box<Message>>,
pub pinned_message: Option<Message>,
/// The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in [`GetChat`](crate::methods::GetChat).
pub message_auto_delete_time: Option<i64>,
}
Expand All @@ -65,7 +65,7 @@ pub struct Group {
/// Primary invite link. Returned only in [`GetChat`](crate::methods::GetChat).
pub invite_link: Option<Box<str>>,
/// The most recent pinned message (by sending date). Returned only in [`GetChat`](crate::methods::GetChat).
pub pinned_message: Option<Box<Message>>,
pub pinned_message: Option<Message>,
/// Default chat member permissions. Returned only in [`GetChat`](crate::methods::GetChat).
pub permissions: Option<ChatPermissions>,
/// The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in [`GetChat`](crate::methods::GetChat).
Expand Down Expand Up @@ -115,7 +115,7 @@ pub struct Supergroup {
/// Primary invite link. Returned only in [`GetChat`](crate::methods::GetChat).
pub invite_link: Option<Box<str>>,
/// The most recent pinned message (by sending date). Returned only in [`GetChat`](crate::methods::GetChat).
pub pinned_message: Option<Box<Message>>,
pub pinned_message: Option<Message>,
/// Default chat member permissions. Returned only in [`GetChat`](crate::methods::GetChat).
pub permissions: Option<ChatPermissions>,
/// The minimum allowed delay between consecutive messages sent by each unpriviledged user; in seconds. Returned only in [`GetChat`](crate::methods::GetChat).
Expand Down Expand Up @@ -171,7 +171,7 @@ pub struct Channel {
/// Primary invite link. Returned only in [`GetChat`](crate::methods::GetChat).
pub invite_link: Option<Box<str>>,
/// The most recent pinned message (by sending date). Returned only in [`GetChat`](crate::methods::GetChat).
pub pinned_message: Option<Box<Message>>,
pub pinned_message: Option<Message>,
/// The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in [`GetChat`](crate::methods::GetChat).
pub message_auto_delete_time: Option<i64>,
/// `true`, if non-administrators can only get the list of bots and administrators in the chat. Returned only in [`GetChat`](crate::methods::GetChat).
Expand Down
2 changes: 1 addition & 1 deletion src/types/giveaway_completed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pub struct GiveawayCompleted {
/// Number of undistributed prizes
pub unclaimed_prize_count: Option<i64>,
/// Message with the giveaway that was completed, if it wasn't deleted
pub giveaway_message: Option<Box<Message>>,
pub giveaway_message: Option<Message>,
}
Loading
Loading