keyboard/
keyboard.rs

1use safe_vk::{Button, ButtonAbstraction, KeyboardColor, Method, Methods, SafeVkBot};
2use serde::{Deserialize, Serialize};
3use std::{env, sync::Arc};
4
5const PAYLOAD: Payload = Payload { button: 1 };
6const SNACKBAR: SnackBar = SnackBar {
7    r#type: "show_snackbar",
8    text: "hello?",
9};
10
11#[derive(Serialize, Deserialize, PartialEq)]
12pub struct Payload {
13    button: u8,
14}
15
16#[derive(Serialize)]
17pub struct SnackBar {
18    r#type: &'static str,
19    text: &'static str,
20}
21
22async fn alert(ctx: Arc<Methods>) {
23    let button = Button::callback("Hello, world!", PAYLOAD, KeyboardColor::Primary);
24    ctx.keyboard("Press me!", false, true, &[[button]])
25        .await
26        .expect("Unable to send keyboard");
27    ctx.reply("a").await;
28}
29
30async fn changes(ctx: Arc<Methods>) {
31    if let Ok(Some(payload)) = ctx.event_answer(SNACKBAR, PAYLOAD).await {
32        if payload.button == 1 {
33            ctx.reply("You clicked on me, thank you!").await;
34        }
35    }
36}
37
38#[tokio::main]
39async fn main() {
40    let group_id: u32 = env::var("GROUP_ID")
41        .unwrap_or_else(|_| "0".into())
42        .parse()
43        .expect("GROUP_ID must be a valid u32");
44
45    let token = env::var("TOKEN").expect("TOKEN environment variable not set");
46
47    let bot = SafeVkBot::create(&token);
48
49    bot.watch(changes)
50        .command("$alert", alert)
51        .start_polling(group_id)
52        .await;
53}