6 releases (3 stable)

1.2.2 Apr 12, 2024
1.2.1 Apr 10, 2024
0.2.1 Feb 2, 2024
0.2.0 Jan 26, 2024
0.1.0 Jan 17, 2024

#310 in Network programming

Download history 1/week @ 2024-01-14 4/week @ 2024-01-21 8/week @ 2024-01-28 20/week @ 2024-02-18 39/week @ 2024-02-25 17/week @ 2024-03-03 1/week @ 2024-03-10 27/week @ 2024-03-31 226/week @ 2024-04-07

253 downloads per month

Custom license

93KB
2K SLoC

Static Badge Static Badge Crates.io

safe-vk

Simple library with simple API for creating your own VK bot for conversations in Rust 🦀

Current State

This library is new and is not maintained every day with 1000 commits, so there is only few functions right now:

  • Has send_message for sending messages to the chat, see more in examples
  • Also there is command and watch methods, the first one is for listening your custom command, and watch is listening all updates from VK.
  • Only Long Poll is supported right now
  • Provides a get_users function for fetching detailed information about users, and a get_members function for retrieving a list of conversation participants
  • But these functions are enough to create a simple bot

Future planning

  • Improving asynchronous code
  • Adding more functions
  • More tests
  • Documenting code
  • Simplifying and cleaning up the code, as it's somewhat disorganized at the moment

Prerequisites

Ensure you have Rust stable version 1.75.0 or nightly version 1.77.0 installed. This library is tested and compatible with these versions

Overview

Installation

$ cargo add safe-vk
$ cargo update

Greeting

use safe_vk::{extract::Ctx, responses::Message, util::Filter, SafeVk};
use std::env;

async fn greeting(update: Ctx<Message>) {
    let user_id = update.message.from_id;
    let user = update.get_users(&[user_id]).await.unwrap();

    update
        .send_message(format!(
            "@id{}(Hello {} {}!)",
            user_id, user[0].first_name, user[0].last_name
        ))
        .await
        .unwrap();
}

#[tokio::main]
async fn main() {
    let group_id: u32 = env::var("GROUP_ID")
        .unwrap_or_else(|_| "0".into())
        .parse()
        .expect("GROUP_ID must be a valid u32");

    let token = env::var("TOKEN").expect("TOKEN environment variable not set");

    let bot = SafeVk::new().command("/hello", greeting, Filter::Sensitive);

    safe_vk::start_polling(&token, group_id, bot).await.unwrap();
}

More Examples

For more, see examples. To run an examples, use the following commands in your terminal:

$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example reply
$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example keyboard
$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example members
$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example state

But don't forget to include your token and group ID

Motivation

My primary goal with this project is to learn how to work with asynchronous code. This project is heavily inspired by the axum crate, and I am also eager to delve deeper into Rust's strong, safe type system. While I'm not actively seeking broad contributions at this time, I warmly welcome bug fixes and creative ideas to enhance the project. Updates will be made as I have motivation and free time.

License

safe-vk is available under the MIT license. See the MIT License file for more details

Dependencies

~9–24MB
~349K SLoC