mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Fixed review
This commit is contained in:
parent
a4e4558eb1
commit
1181092bd5
1 changed files with 26 additions and 20 deletions
|
@ -1,3 +1,15 @@
|
||||||
|
//! This example demonstrates how to use deep linking in Telegram
|
||||||
|
//! by making a simple anonymous message bot.
|
||||||
|
//!
|
||||||
|
//! Deep linking (links like https://t.me/some_bot?start=123456789)
|
||||||
|
//! is handled by telegram in the same way as just sending /start {argument}.
|
||||||
|
//! So, in the StartCommand enum we need to write Start(String) to get the argument,
|
||||||
|
//! just as in command.rs example.
|
||||||
|
//!
|
||||||
|
//! Also, deep linking is only supported with /start command!
|
||||||
|
//! "https://t.me/some_bot?argument=123456789" will not work
|
||||||
|
//!
|
||||||
|
//! https://core.telegram.org/bots/features#deep-linking
|
||||||
use dptree::{case, deps};
|
use dptree::{case, deps};
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
dispatching::dialogue::{self, InMemStorage},
|
dispatching::dialogue::{self, InMemStorage},
|
||||||
|
@ -14,7 +26,7 @@ pub enum State {
|
||||||
#[default]
|
#[default]
|
||||||
Start,
|
Start,
|
||||||
WriteToSomeone {
|
WriteToSomeone {
|
||||||
id: i64,
|
id: ChatId,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,24 +34,20 @@ pub enum State {
|
||||||
#[command(rename_rule = "lowercase")]
|
#[command(rename_rule = "lowercase")]
|
||||||
pub enum StartCommand {
|
pub enum StartCommand {
|
||||||
#[command()]
|
#[command()]
|
||||||
Start(String), /* Because deep linking (links like https://t.me/some_bot?start=123456789)
|
Start(String),
|
||||||
* is the same as sending "/start 123456789",
|
|
||||||
* we can treat it as just an argument to a command
|
|
||||||
*
|
|
||||||
* https://core.telegram.org/bots/features#deep-linking */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting dialogue bot...");
|
log::info!("Starting deep linking bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
let handler = dialogue::enter::<Update, InMemStorage<State>, State, _>()
|
let handler = dialogue::enter::<Update, InMemStorage<State>, State, _>()
|
||||||
.branch(
|
.branch(
|
||||||
Update::filter_message()
|
Update::filter_message()
|
||||||
.filter_command::<StartCommand>() // Nessary to get cmd as an argument
|
.filter_command::<StartCommand>()
|
||||||
.branch(case![StartCommand::Start(start)].endpoint(start)),
|
.branch(case![StartCommand::Start(start)].endpoint(start)),
|
||||||
)
|
)
|
||||||
.branch(
|
.branch(
|
||||||
|
@ -59,30 +67,28 @@ pub async fn start(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
cmd: StartCommand,
|
start: String, // Available from `case![StartCommand::Start(start)]`
|
||||||
me: Me,
|
me: Me,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
// If you have multiple commands, this will need to become a match
|
if start.is_empty() {
|
||||||
let StartCommand::Start(arg) = cmd;
|
|
||||||
|
|
||||||
if arg.is_empty() {
|
|
||||||
// This means that it is just a regular link like https://t.me/some_bot, or a /start command
|
// This means that it is just a regular link like https://t.me/some_bot, or a /start command
|
||||||
bot.send_message(
|
bot.send_message(
|
||||||
msg.chat.id,
|
msg.chat.id,
|
||||||
format!(
|
format!(
|
||||||
"Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
|
"Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
|
||||||
me.tme_url(),
|
me.tme_url(),
|
||||||
msg.chat.id.0
|
msg.chat.id
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
} else {
|
} else {
|
||||||
// And this means that the link is like this: https://t.me/some_bot?start=123456789
|
// And this means that the link is like this: https://t.me/some_bot?start=123456789,
|
||||||
match arg.parse::<i64>() {
|
// or a /start 123456789 command
|
||||||
|
match start.parse::<i64>() {
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
bot.send_message(msg.chat.id, "Send your message:").await?;
|
bot.send_message(msg.chat.id, "Send your message:").await?;
|
||||||
dialogue.update(State::WriteToSomeone { id }).await?;
|
dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
bot.send_message(msg.chat.id, "Bad link!").await?;
|
bot.send_message(msg.chat.id, "Bad link!").await?;
|
||||||
|
@ -95,7 +101,7 @@ pub async fn start(
|
||||||
|
|
||||||
pub async fn send_message(
|
pub async fn send_message(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
id: i64, // Available from `State::WriteToSomeone`.
|
id: ChatId, // Available from `State::WriteToSomeone`
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
me: Me,
|
me: Me,
|
||||||
|
@ -104,7 +110,7 @@ pub async fn send_message(
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
// Trying to send a message to the user
|
// Trying to send a message to the user
|
||||||
let sent_result = bot
|
let sent_result = bot
|
||||||
.send_message(ChatId(id), format!("You have a new message!\n\n<i>{text}</i>"))
|
.send_message(id, format!("You have a new message!\n\n<i>{text}</i>"))
|
||||||
.parse_mode(teloxide::types::ParseMode::Html)
|
.parse_mode(teloxide::types::ParseMode::Html)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -115,7 +121,7 @@ pub async fn send_message(
|
||||||
format!(
|
format!(
|
||||||
"Message sent!\n\nYour link is: {}?start={}",
|
"Message sent!\n\nYour link is: {}?start={}",
|
||||||
me.tme_url(),
|
me.tme_url(),
|
||||||
msg.chat.id.0
|
msg.chat.id
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
Loading…
Reference in a new issue