

[](https://crates.io/crates/safe-vk)
# 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](./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](https://dev.vk.com/en/api/bots-long-poll/getting-started)
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
- Works with API version [**5.199**](https://dev.vk.com/en/reference/version/5.199)
- Utilizes threads for faster responses
- has
[serde_json](https://docs.rs/serde_json/1.0.111/serde_json/index.html),
[tokio](https://docs.rs/tokio/1.35.1/tokio/index.html) and
[reqwest](https://docs.rs/reqwest/0.11.23/reqwest/index.html) under the hood
## Installation
```bash
$ cargo add safe-vk
$ cargo update
```
## Greeting
```rust
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](./examples/). To run an examples, use the following commands in your terminal:
```shell
$ 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](https://crates.io/crates/axum),
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](LICENSE) file for more details