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 141)
131async fn receive_product_selection(
132 bot: Bot,
133 dialogue: MyDialogue,
134 full_name: String, // Available from `State::ReceiveProductChoice`.
135 q: CallbackQuery,
136) -> HandlerResult {
137 if let Some(product) = &q.data {
138 bot.send_message(format!(
139 "{full_name}, product '{product}' has been purchased successfully!"
140 ))
141 .chat_id(dialogue.chat_id())
142 .await?;
143 dialogue.exit().await?;
144 }
145
146 Ok(())
147}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("Let's start! What's your full name?").chat_id(msg.chat.id).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("How old are you?").chat_id(msg.chat.id).await?;
70 dialogue.update(State::ReceiveAge { full_name: text.into() }).await?;
71 }
72 None => {
73 bot.send_message("Send me plain text.").chat_id(msg.chat.id).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("What's your location?").chat_id(msg.chat.id).await?;
89 dialogue.update(State::ReceiveLocation { full_name, age }).await?;
90 }
91 _ => {
92 bot.send_message("Send me a number.").chat_id(msg.chat.id).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("Let's start! What's your full name?").chat_id(msg.chat.id).await?;
89 dialogue.update(State::ReceiveFullName).await?;
90 Ok(())
91}
92
93async fn help(bot: Bot, msg: Message) -> HandlerResult {
94 bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?;
95 Ok(())
96}
97
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99 bot.send_message("Cancelling the dialogue.").chat_id(msg.chat.id).await?;
100 dialogue.exit().await?;
101 Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105 bot.send_message("Unable to handle the message. Type /help to see the usage.")
106 .chat_id(msg.chat.id)
107 .await?;
108 Ok(())
109}
110
111async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
112 match msg.text().map(ToOwned::to_owned) {
113 Some(full_name) => {
114 let products = ["Apple", "Banana", "Orange", "Potato"]
115 .map(|product| InlineKeyboardButton::callback(product, product));
116
117 bot.send_message("Select a product:")
118 .chat_id(msg.chat.id)
119 .reply_markup(InlineKeyboardMarkup::new([products]))
120 .await?;
121 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
122 }
123 None => {
124 bot.send_message("Please, send me your full name.").chat_id(msg.chat.id).await?;
125 }
126 }
127
128 Ok(())
129}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(format!("Remembered number {n}. Now use /get or /reset."))
69 .chat_id(msg.chat.id)
70 .await?;
71 }
72 _ => {
73 bot.send_message("Please, send me a number.").chat_id(msg.chat.id).await?;
74 }
75 }
76
77 Ok(())
78}examples/deep_linking.rs (line 88)
65pub async fn start(
66 bot: Bot,
67 dialogue: MyDialogue,
68 msg: Message,
69 start: String, // Available from `case![StartCommand::Start(start)]`
70 me: BotFullInfo,
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(format!(
75 "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
76 me.tme_url().ok_or("The bot needs a public username for deep links")?,
77 msg.chat.id
78 ))
79 .chat_id(msg.chat.id)
80 .await?;
81 dialogue.exit().await?;
82 } else {
83 // And this means that the link is like this: https://t.me/some_bot?start=123456789,
84 // or a /start 123456789 command
85 match start.parse::<i64>() {
86 Ok(id) => {
87 bot.send_message("Send your message:").chat_id(msg.chat.id).await?;
88 dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
89 }
90 Err(_) => {
91 bot.send_message("Bad link!").chat_id(msg.chat.id).await?;
92 dialogue.exit().await?;
93 }
94 }
95 }
96 Ok(())
97}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 92)
80async fn got_number(
81 bot: Bot,
82 dialogue: MyDialogue,
83 num: i32, // Available from `State::GotNumber`.
84 msg: Message,
85 cmd: Command,
86) -> HandlerResult {
87 match cmd {
88 Command::Get => {
89 bot.send_message(format!("Here is your number: {num}.")).chat_id(msg.chat.id).await?;
90 }
91 Command::Reset => {
92 dialogue.reset().await?;
93 bot.send_message("Number reset.").chat_id(msg.chat.id).await?;
94 }
95 }
96 Ok(())
97}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("Cancelling the dialogue.").chat_id(msg.chat.id).await?;
100 dialogue.exit().await?;
101 Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105 bot.send_message("Unable to handle the message. Type /help to see the usage.")
106 .chat_id(msg.chat.id)
107 .await?;
108 Ok(())
109}
110
111async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
112 match msg.text().map(ToOwned::to_owned) {
113 Some(full_name) => {
114 let products = ["Apple", "Banana", "Orange", "Potato"]
115 .map(|product| InlineKeyboardButton::callback(product, product));
116
117 bot.send_message("Select a product:")
118 .chat_id(msg.chat.id)
119 .reply_markup(InlineKeyboardMarkup::new([products]))
120 .await?;
121 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
122 }
123 None => {
124 bot.send_message("Please, send me your full name.").chat_id(msg.chat.id).await?;
125 }
126 }
127
128 Ok(())
129}
130
131async fn receive_product_selection(
132 bot: Bot,
133 dialogue: MyDialogue,
134 full_name: String, // Available from `State::ReceiveProductChoice`.
135 q: CallbackQuery,
136) -> HandlerResult {
137 if let Some(product) = &q.data {
138 bot.send_message(format!(
139 "{full_name}, product '{product}' has been purchased successfully!"
140 ))
141 .chat_id(dialogue.chat_id())
142 .await?;
143 dialogue.exit().await?;
144 }
145
146 Ok(())
147}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(report).chat_id(msg.chat.id).await?;
109 dialogue.exit().await?;
110 }
111 None => {
112 bot.send_message("Send me plain text.").chat_id(msg.chat.id).await?;
113 }
114 }
115
116 Ok(())
117}examples/deep_linking.rs (line 81)
65pub async fn start(
66 bot: Bot,
67 dialogue: MyDialogue,
68 msg: Message,
69 start: String, // Available from `case![StartCommand::Start(start)]`
70 me: BotFullInfo,
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(format!(
75 "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
76 me.tme_url().ok_or("The bot needs a public username for deep links")?,
77 msg.chat.id
78 ))
79 .chat_id(msg.chat.id)
80 .await?;
81 dialogue.exit().await?;
82 } else {
83 // And this means that the link is like this: https://t.me/some_bot?start=123456789,
84 // or a /start 123456789 command
85 match start.parse::<i64>() {
86 Ok(id) => {
87 bot.send_message("Send your message:").chat_id(msg.chat.id).await?;
88 dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
89 }
90 Err(_) => {
91 bot.send_message("Bad link!").chat_id(msg.chat.id).await?;
92 dialogue.exit().await?;
93 }
94 }
95 }
96 Ok(())
97}
98
99pub async fn send_message(
100 bot: Bot,
101 id: ChatId, // Available from `State::WriteToSomeone`
102 msg: Message,
103 dialogue: MyDialogue,
104 me: BotFullInfo,
105) -> HandlerResult {
106 match msg.text() {
107 Some(text) => {
108 // Trying to send a message to the user
109 let sent_result = bot
110 .send_message(format!("You have a new message!\n\n<i>{text}</i>"))
111 .chat_id(id)
112 .parse_mode(ParseMode::Html)
113 .await;
114
115 // And if no error is returned, success!
116 if sent_result.is_ok() {
117 bot.send_message(format!(
118 "Message sent!\n\nYour link is: {}?start={}",
119 me.tme_url().ok_or("The bot needs a public username for deep links")?,
120 msg.chat.id
121 ))
122 .chat_id(msg.chat.id)
123 .await?;
124 } else {
125 bot.send_message("Error sending message! Maybe user blocked the bot?")
126 .chat_id(msg.chat.id)
127 .await?;
128 }
129 dialogue.exit().await?;
130 }
131 None => {
132 bot.send_message("This bot can send only text.").chat_id(msg.chat.id).await?;
133 }
134 };
135 Ok(())
136}Trait Implementations§
Auto Trait Implementations§
impl<D, S> Forget for Dialogue<D, S>where
S: ?Sized,
impl<D, S> Freeze for Dialogue<D, S>where
*const ArcInner<S> is !null: Freeze,
PhantomData<ArcInner<S>>: Freeze,
PhantomData<D>: Freeze,
S: ?Sized,
impl<D, S> Move 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> UnsafeUnpin for Dialogue<D, S>where
*const ArcInner<S> is !null: UnsafeUnpin,
PhantomData<ArcInner<S>>: UnsafeUnpin,
PhantomData<D>: UnsafeUnpin,
S: ?Sized,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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