Repository navigation
Expand file tree
/
Copy pathdemo_bot.rs
More file actions
110 lines (95 loc) · 4.08 KB
/
Copy pathdemo_bot.rs
File metadata and controls
110 lines (95 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use vaegram::prelude::*;
use vaegram::types::{InlineQueryResultArticle, InputMessageContent};
fn menu() -> InlineKeyboardMarkup {
InlineKeyboardMarkup::new([
vec![
InlineKeyboardButton::callback("Yes", "yes").style(ButtonStyle::Success),
InlineKeyboardButton::callback("No", "no").style(ButtonStyle::Danger),
],
vec![InlineKeyboardButton::callback("Show an alert", "alert").style(ButtonStyle::Primary)],
vec![
InlineKeyboardButton::switch_inline_query_current_chat("Try inline", "vaegram"),
InlineKeyboardButton::url("Source", "https://github.com/rjriajul/vaegram"),
],
])
}
#[tokio::main]
async fn main() -> Result<()> {
let env = |k: &str| std::env::var(k).unwrap_or_else(|_| panic!("set {k}"));
let app = Client::builder("demo_bot", env("VAEGRAM_API_ID").parse().unwrap(), env("VAEGRAM_API_HASH"))
.bot_token(env("VAEGRAM_BOT_TOKEN"))
.start()
.await?;
app.on_message(filters::command("start"), |msg| async move {
println!("start from {:?}", msg.from_user);
msg.answer("<b>vaegram demo</b>\nTap a button:").html().reply_markup(menu()).await?;
Ok(())
});
app.on_message(filters::command("keyboard"), |msg| async move {
let keyboard =
ReplyKeyboardMarkup::new([vec![KeyboardButton::new("Hello"), KeyboardButton::new("Remove keyboard")]])
.placeholder("Pick one");
msg.answer("Here is a reply keyboard.").reply_markup(keyboard).await?;
Ok(())
});
app.on_message(filters::command("react"), |msg| async move {
msg.react("👍").await?;
Ok(())
});
app.on_message(filters::text() & filters::create(|m: &Message| m.text() == "Remove keyboard"), |msg| async move {
msg.answer("Keyboard removed.").reply_markup(ReplyKeyboardRemove::default()).await?;
Ok(())
});
app.on_message(filters::text() & filters::private(), |msg| async move {
println!("message: {:?}", msg.text());
msg.reply(Text::new().plain("You said: ").bold(msg.text())).await?;
Ok(())
});
app.on_edited_message(filters::text(), |msg| async move {
println!("edited: {:?}", msg.text());
msg.reply(format!("You edited it to: {}", msg.text())).await?;
Ok(())
});
app.on_callback_query(filters::data("alert"), |query| async move {
println!("alert from {}", query.from_user.first_name);
query.answer().text("This is an alert from vaegram").alert().await?;
Ok(())
});
app.on_callback_query(filters::all(), |query| async move {
println!("pressed {:?}", query.data);
query.answer().text(format!("You pressed {}", query.data)).await?;
query
.edit_message_text(format!("You pressed <b>{}</b>. Tap again:", query.data))
.html()
.reply_markup(menu())
.await?;
Ok(())
});
app.on_inline_query(filters::all(), |query| async move {
println!("inline query: {:?}", query.query);
if query.query.is_empty() {
return Ok(());
}
let results = [
InlineQueryResultArticle::new("Plain", format!("You searched: {}", query.query)).description("plain text"),
InlineQueryResultArticle::new("Bold", InputMessageContent::html(format!("<b>{}</b>", query.query)))
.description("HTML"),
];
query.answer(results).cache_time(0).await?;
Ok(())
});
app.on_message_reaction(filters::all(), |update| async move {
println!("reactions on {} changed to {:?}", update.message_id, update.new_reaction);
Ok(())
});
app.on_my_chat_member(filters::all(), |update| async move {
println!("my membership in {}: {:?}", update.chat.id, update.new_chat_member.map(|m| m.status));
Ok(())
});
app.on_chat_member_updated(filters::all(), |update| async move {
println!("member update in {}: {:?}", update.chat.id, update.new_chat_member.map(|m| m.status));
Ok(())
});
println!("demo bot is running");
app.run().await
}