4 releases (2 breaking)

1.2.2 Apr 12, 2024
1.2.1 Apr 10, 2024
0.3.0-alpha May 15, 2024
0.2.1 Feb 2, 2024
0.1.0 Jan 17, 2024

#327 in Network programming

Download history 1/week @ 2024-01-17 4/week @ 2024-01-24 8/week @ 2024-01-31 6/week @ 2024-02-14 22/week @ 2024-02-21 36/week @ 2024-02-28 13/week @ 2024-03-06 12/week @ 2024-03-27 95/week @ 2024-04-03 270/week @ 2024-04-10 5/week @ 2024-04-17

355 downloads per month

Custom license

125KB
2.5K 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 doesn't include all VK API methods. For now, it can handle simple tasks such as sending messages, getting users from a conversation, and sending photos.

Future planning

  • Improving asynchronous code
  • Support more VK API methods
  • More tests
  • Documenting code
  • Simplifying code

Prerequisites

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

Overview

  • Works with newest API version 5.199
  • Utilizes routes same as in popular web frameworks
  • has serde_json, tokio and reqwest under the hood

Installation

$ cargo add safe-vk
$ cargo update

Greeting

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

async fn reply(update: Ctx<Message>) -> Result<()> {
    // Sending request to VK API
    update.message_text("hello from rust! 🦀").send().await?;
    Ok(())
}

#[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");

    // You can select Filter to change how strictly it listens to your command
    // You can find more info in the docs
    let bot = SafeVk::new().command("/hello", reply, Filter::Strict);

    // Start listening VK API
    safe_vk::start_polling(&token, group_id, bot).await.unwrap();
}

More Examples

For more, try to play with examples. To run an example, use the following command 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 comfyui
$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example oobabooga
$ GROUP_ID=YOUR_GROUP_ID TOKEN=YOUR_TOKEN cargo run --example macros
$ 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

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 and understand Rust's strong, safe type system. This project is heavily inspired by the axum crate. Feel free to contribute and propose new ideas! 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

~8–24MB
~341K SLoC