Skip to content

Commit

Permalink
make from optional in message as it should be
Browse files Browse the repository at this point in the history
expose ErrorKind
  • Loading branch information
kworr committed Aug 19, 2024
1 parent 11dfca1 commit 1c79703
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ async fn main() -> Result<(), Error> {
if let UpdateKind::Message(message) = update.kind {
if let MessageKind::Text { ref data, .. } = message.kind {
// Print received text message to stdout.
println!("<{}>: {}", &message.from.first_name, data);
if let Some(from) = &message.from {
println!("<{}>: {}", from.first_name, data);
} else {
println!(": {}", data);
}

// Answer message with "Hi".
api.send(message.text_reply(format!(
Expand Down
38 changes: 25 additions & 13 deletions lib/examples/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ async fn test_reply(api: Api, message: Message) -> Result<(), Error> {
api.send(message.text_reply("Reply to message")).await?;
api.send(message.chat.text("Text to message chat")).await?;

api.send(message.from.text("Private text")).await?;
if let Some(from) = &message.from {
api.send(from.text("Private text")).await?;
}
Ok(())
}

async fn test_forward(api: Api, message: Message) -> Result<(), Error> {
api.send(message.forward(&message.chat)).await?;

api.send(message.forward(&message.from)).await?;
if let Some(from) = &message.from {
api.send(message.forward(from)).await?;
}
Ok(())
}

Expand Down Expand Up @@ -82,20 +86,28 @@ async fn test_get_chat_members_count(api: Api, message: Message) -> Result<(), E
}

async fn test_get_chat_member(api: Api, message: Message) -> Result<(), Error> {
let member = api.send(message.chat.get_member(&message.from)).await?;
let first_name = member.user.first_name.clone();
let status = member.status;
api.send(message.text_reply(format!("Member {}, status {:?}", first_name, status)))
.await?;
Ok(())
if let Some(from) = &message.from {
let member = api.send(message.chat.get_member(from)).await?;
let first_name = member.user.first_name.clone();
let status = member.status;
api.send(message.text_reply(format!("Member {}, status {:?}", first_name, status)))
.await?;
Ok(())
} else {
panic!("missing 'from' field")
}
}

async fn test_get_user_profile_photos(api: Api, message: Message) -> Result<(), Error> {
let photos = api.send(message.from.get_user_profile_photos()).await?;

api.send(message.text_reply(format!("Found photos: {}", photos.total_count)))
.await?;
Ok(())
if let Some(from) = &message.from {
let photos = api.send(from.get_user_profile_photos()).await?;

api.send(message.text_reply(format!("Found photos: {}", photos.total_count)))
.await?;
Ok(())
} else {
panic!("missing 'from' field")
}
}

async fn test_leave(api: Api, message: Message) -> Result<(), Error> {
Expand Down
4 changes: 3 additions & 1 deletion lib/examples/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ async fn run_test(api: Api, message: Message) -> Result<(), Error> {
.await?;
api.send(chat.document(&file).caption("Direct to chat"))
.await?;
api.send(message.from.document(&file).caption("Send to user"))
if let Some(from) = &message.from {
api.send(from.document(&file).caption("Send to user"))
.await?;
}

// With custom thumbnail
api.send(
Expand Down
18 changes: 11 additions & 7 deletions lib/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ async fn main() -> Result<(), Error> {
if let UpdateKind::Message(message) = update.kind {
if let MessageKind::Text { ref data, .. } = message.kind {
// Print received text message to stdout.
println!("<{}>: {}", &message.from.first_name, data);
if let Some(from) = &message.from {
println!("<{}>: {}", from.first_name, data);

// Answer message with "Hi".
api.send(message.text_reply(format!(
"Hi, {}! You just wrote '{}'",
&message.from.first_name, data
)))
.await?;
// Answer message with "Hi".
api.send(message.text_reply(format!(
"Hi, {}! You just wrote '{}'",
from.first_name, data
)))
.await?;
} else {
println!(": {}", data);
}
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions lib/examples/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ async fn main() -> Result<(), Error> {
let update = update?;
if let UpdateKind::Message(message) = update.kind {
if let MessageKind::Text { ref data, .. } = message.kind {
api.send(message.text_reply(format!(
"Hi, {}! You just wrote '{}'",
&message.from.first_name, data
)))
.await?;
if let Some(from) = &message.from {
api.send(message.text_reply(format!(
"Hi, {}! You just wrote '{}'",
from.first_name, data
)))
.await?;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod types;
pub mod util;

pub use self::api::Api;
pub use self::errors::Error;
pub use self::errors::{Error, ErrorKind};
pub use prelude::*;
pub use stream::UpdatesStream;
pub use types::*;
7 changes: 2 additions & 5 deletions raw/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Message {
/// Unique message identifier inside this chat.
pub id: MessageId,
/// Sender, can be empty for messages sent to channels.
pub from: User,
pub from: Option<User>,
/// Date the message was sent in Unix time.
pub date: Integer,
/// Conversation the message belongs to.
Expand Down Expand Up @@ -221,10 +221,7 @@ pub enum MessageKind {
impl Message {
fn from_raw_message(raw: RawMessage) -> Result<Self, String> {
let id = raw.message_id;
let from = match raw.from.clone() {
Some(from) => from,
None => return Err("Missing `from` field for Message".into()),
};
let from = raw.from.clone();
let date = raw.date;
let chat = match raw.chat.clone() {
Chat::Private(x) => MessageChat::Private(x),
Expand Down

0 comments on commit 1c79703

Please sign in to comment.