pub struct Dialogue<D, S>where
S: ?Sized,{ /* private fields */ }Expand description
A handle for controlling dialogue state.
Implementations§
Source§impl<D, S> Dialogue<D, S>
impl<D, S> Dialogue<D, S>
Sourcepub fn new(storage: Arc<S>, chat_id: ChatId) -> Self
pub fn new(storage: Arc<S>, chat_id: ChatId) -> Self
Constructs a new dialogue with storage (where dialogues are stored)
and chat_id of a current dialogue.
Sourcepub fn chat_id(&self) -> ChatId
pub fn chat_id(&self) -> ChatId
Returns a chat ID associated with this dialogue.
Examples found in repository?
examples/purchase.rs (line 137)
129async fn receive_product_selection(
130 bot: Bot,
131 dialogue: MyDialogue,
132 full_name: String, // Available from `State::ReceiveProductChoice`.
133 q: CallbackQuery,
134) -> HandlerResult {
135 if let Some(product) = &q.data {
136 bot.send_message(
137 dialogue.chat_id(),
138 format!("{full_name}, product '{product}' has been purchased successfully!"),
139 )
140 .await?;
141 dialogue.exit().await?;
142 }
143
144 Ok(())
145}Sourcepub async fn get(&self) -> Result<Option<D>, S::Error>
pub async fn get(&self) -> Result<Option<D>, S::Error>
Retrieves the current state of the dialogue or None if there is no
dialogue.
Sourcepub async fn get_or_default(&self) -> Result<D, S::Error>where
D: Default,
pub async fn get_or_default(&self) -> Result<D, S::Error>where
D: Default,
Like Dialogue::get but returns a default value if there is no
dialogue.
Sourcepub async fn update<State>(&self, state: State) -> Result<(), S::Error>where
D: From<State>,
pub async fn update<State>(&self, state: State) -> Result<(), S::Error>where
D: From<State>,
Updates the dialogue state.
The dialogue type D must implement From<State> to allow implicit
conversion from State to D.
Examples found in repository?
examples/dialogue.rs (line 62)
60async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
61 bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
62 dialogue.update(State::ReceiveFullName).await?;
63 Ok(())
64}
65
66async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
67 match msg.text() {
68 Some(text) => {
69 bot.send_message(msg.chat.id, "How old are you?").await?;
70 dialogue.update(State::ReceiveAge { full_name: text.into() }).await?;
71 }
72 None => {
73 bot.send_message(msg.chat.id, "Send me plain text.").await?;
74 }
75 }
76
77 Ok(())
78}
79
80async fn receive_age(
81 bot: Bot,
82 dialogue: MyDialogue,
83 full_name: String, // Available from `State::ReceiveAge`.
84 msg: Message,
85) -> HandlerResult {
86 match msg.text().map(|text| text.parse::<u8>()) {
87 Some(Ok(age)) => {
88 bot.send_message(msg.chat.id, "What's your location?").await?;
89 dialogue.update(State::ReceiveLocation { full_name, age }).await?;
90 }
91 _ => {
92 bot.send_message(msg.chat.id, "Send me a number.").await?;
93 }
94 }
95
96 Ok(())
97}More examples
examples/purchase.rs (line 89)
87async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
88 bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
89 dialogue.update(State::ReceiveFullName).await?;
90 Ok(())
91}
92
93async fn help(bot: Bot, msg: Message) -> HandlerResult {
94 bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
95 Ok(())
96}
97
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99 bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
100 dialogue.exit().await?;
101 Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105 bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
106 .await?;
107 Ok(())
108}
109
110async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
111 match msg.text().map(ToOwned::to_owned) {
112 Some(full_name) => {
113 let products = ["Apple", "Banana", "Orange", "Potato"]
114 .map(|product| InlineKeyboardButton::callback(product, product));
115
116 bot.send_message(msg.chat.id, "Select a product:")
117 .reply_markup(InlineKeyboardMarkup::new([products]))
118 .await?;
119 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
120 }
121 None => {
122 bot.send_message(msg.chat.id, "Please, send me your full name.").await?;
123 }
124 }
125
126 Ok(())
127}examples/db_remember.rs (line 67)
64async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
65 match msg.text().map(|text| text.parse::<i32>()) {
66 Some(Ok(n)) => {
67 dialogue.update(State::GotNumber(n)).await?;
68 bot.send_message(
69 msg.chat.id,
70 format!("Remembered number {n}. Now use /get or /reset."),
71 )
72 .await?;
73 }
74 _ => {
75 bot.send_message(msg.chat.id, "Please, send me a number.").await?;
76 }
77 }
78
79 Ok(())
80}examples/deep_linking.rs (line 90)
65pub async fn start(
66 bot: Bot,
67 dialogue: MyDialogue,
68 msg: Message,
69 start: String, // Available from `case![StartCommand::Start(start)]`
70 me: Me,
71) -> HandlerResult {
72 if start.is_empty() {
73 // This means that it is just a regular link like https://t.me/some_bot, or a /start command
74 bot.send_message(
75 msg.chat.id,
76 format!(
77 "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
78 me.tme_url(),
79 msg.chat.id
80 ),
81 )
82 .await?;
83 dialogue.exit().await?;
84 } else {
85 // And this means that the link is like this: https://t.me/some_bot?start=123456789,
86 // or a /start 123456789 command
87 match start.parse::<i64>() {
88 Ok(id) => {
89 bot.send_message(msg.chat.id, "Send your message:").await?;
90 dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
91 }
92 Err(_) => {
93 bot.send_message(msg.chat.id, "Bad link!").await?;
94 dialogue.exit().await?;
95 }
96 }
97 }
98 Ok(())
99}Sourcepub async fn reset(&self) -> Result<(), S::Error>where
D: Default,
pub async fn reset(&self) -> Result<(), S::Error>where
D: Default,
Updates the dialogue with a default value.
Examples found in repository?
examples/db_remember.rs (line 94)
82async fn got_number(
83 bot: Bot,
84 dialogue: MyDialogue,
85 num: i32, // Available from `State::GotNumber`.
86 msg: Message,
87 cmd: Command,
88) -> HandlerResult {
89 match cmd {
90 Command::Get => {
91 bot.send_message(msg.chat.id, format!("Here is your number: {num}.")).await?;
92 }
93 Command::Reset => {
94 dialogue.reset().await?;
95 bot.send_message(msg.chat.id, "Number reset.").await?;
96 }
97 }
98 Ok(())
99}Sourcepub async fn exit(&self) -> Result<(), S::Error>
pub async fn exit(&self) -> Result<(), S::Error>
Removes the dialogue from the storage provided to Dialogue::new.
Examples found in repository?
examples/purchase.rs (line 100)
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99 bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
100 dialogue.exit().await?;
101 Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105 bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
106 .await?;
107 Ok(())
108}
109
110async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
111 match msg.text().map(ToOwned::to_owned) {
112 Some(full_name) => {
113 let products = ["Apple", "Banana", "Orange", "Potato"]
114 .map(|product| InlineKeyboardButton::callback(product, product));
115
116 bot.send_message(msg.chat.id, "Select a product:")
117 .reply_markup(InlineKeyboardMarkup::new([products]))
118 .await?;
119 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
120 }
121 None => {
122 bot.send_message(msg.chat.id, "Please, send me your full name.").await?;
123 }
124 }
125
126 Ok(())
127}
128
129async fn receive_product_selection(
130 bot: Bot,
131 dialogue: MyDialogue,
132 full_name: String, // Available from `State::ReceiveProductChoice`.
133 q: CallbackQuery,
134) -> HandlerResult {
135 if let Some(product) = &q.data {
136 bot.send_message(
137 dialogue.chat_id(),
138 format!("{full_name}, product '{product}' has been purchased successfully!"),
139 )
140 .await?;
141 dialogue.exit().await?;
142 }
143
144 Ok(())
145}More examples
examples/dialogue.rs (line 109)
99async fn receive_location(
100 bot: Bot,
101 dialogue: MyDialogue,
102 (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
103 msg: Message,
104) -> HandlerResult {
105 match msg.text() {
106 Some(location) => {
107 let report = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
108 bot.send_message(msg.chat.id, report).await?;
109 dialogue.exit().await?;
110 }
111 None => {
112 bot.send_message(msg.chat.id, "Send me plain text.").await?;
113 }
114 }
115
116 Ok(())
117}examples/deep_linking.rs (line 83)
65pub async fn start(
66 bot: Bot,
67 dialogue: MyDialogue,
68 msg: Message,
69 start: String, // Available from `case![StartCommand::Start(start)]`
70 me: Me,
71) -> HandlerResult {
72 if start.is_empty() {
73 // This means that it is just a regular link like https://t.me/some_bot, or a /start command
74 bot.send_message(
75 msg.chat.id,
76 format!(
77 "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
78 me.tme_url(),
79 msg.chat.id
80 ),
81 )
82 .await?;
83 dialogue.exit().await?;
84 } else {
85 // And this means that the link is like this: https://t.me/some_bot?start=123456789,
86 // or a /start 123456789 command
87 match start.parse::<i64>() {
88 Ok(id) => {
89 bot.send_message(msg.chat.id, "Send your message:").await?;
90 dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
91 }
92 Err(_) => {
93 bot.send_message(msg.chat.id, "Bad link!").await?;
94 dialogue.exit().await?;
95 }
96 }
97 }
98 Ok(())
99}
100
101pub async fn send_message(
102 bot: Bot,
103 id: ChatId, // Available from `State::WriteToSomeone`
104 msg: Message,
105 dialogue: MyDialogue,
106 me: Me,
107) -> HandlerResult {
108 match msg.text() {
109 Some(text) => {
110 // Trying to send a message to the user
111 let sent_result = bot
112 .send_message(id, format!("You have a new message!\n\n<i>{text}</i>"))
113 .parse_mode(ParseMode::Html)
114 .await;
115
116 // And if no error is returned, success!
117 if sent_result.is_ok() {
118 bot.send_message(
119 msg.chat.id,
120 format!(
121 "Message sent!\n\nYour link is: {}?start={}",
122 me.tme_url(),
123 msg.chat.id
124 ),
125 )
126 .await?;
127 } else {
128 bot.send_message(msg.chat.id, "Error sending message! Maybe user blocked the bot?")
129 .await?;
130 }
131 dialogue.exit().await?;
132 }
133 None => {
134 bot.send_message(msg.chat.id, "This bot can send only text.").await?;
135 }
136 };
137 Ok(())
138}Trait Implementations§
Auto Trait Implementations§
impl<D, S> Freeze for Dialogue<D, S>where
S: ?Sized,
impl<D, S> RefUnwindSafe for Dialogue<D, S>
impl<D, S> Send for Dialogue<D, S>
impl<D, S> Sync for Dialogue<D, S>
impl<D, S> Unpin for Dialogue<D, S>
impl<D, S> UnwindSafe for Dialogue<D, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Erasable for T
impl<T> Erasable for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more