vkteams-bot-0.1.6 has been yanked.
VK Teams Bot API client
VK Teams Bot API client written in Rust.
Table of Contents
Environment
- Begin with bot API following instructions
- Set environment variables or save in
.envfile
# Unix-like
$ export VKTEAMS_VKTEAMS_BOT_API_TOKEN=<Your token here> #require
$ export VKTEAMS_BOT_API_URL=<Your base api url> #require
$ export VKTEAMS_PROXY=<Proxy> #optional
# Windows
$ set VKTEAMS_VKTEAMS_BOT_API_TOKEN=<Your token here> #require
$ set VKTEAMS_BOT_API_URL=<Your base api url> #require
$ set VKTEAMS_PROXY=<Proxy> #optional
- Put lines in you
Cargo.tomlfile
[dependencies]
vkteams_bot = "0.1"
log = "0.4"
Usage
#[macro_use]
extern crate log;
use vkteams_bot::{self, bot::types::*};
#[tokio::main]
async fn main() {
// CHeck .env file and init logger
dotenvy::dotenv().expect("Unable to load .env file");
pretty_env_logger::init();
info!("Starting...");
// Start bot with API version 1
let bot = Bot::new(APIVersionUrl::V1);
// Send answer to chat
send_text_msg(&bot).await;
}
async fn send_text_msg(bot: &Bot) {
// Send text message to chat
// Get initial events from the API
let chat_events = bot.get_events().await.unwrap();
// Loop at all events
for event in chat_events.events {
// New kwyboard
let mut kb: Keyboard = Default::default();
// New HTML parser
let mut html_parser: MessageTextParser = Default::default();
let chat = event.payload.chat.to_owned().unwrap();
// Check event type
match event.event_type {
EventType::NewMessage => {
kb.add_button(&ButtonKeyboard::cb(
String::from("test_callback"),
String::from("test_callback_data"),
ButtonStyle::Primary,
))
.add_button(&ButtonKeyboard::url(
String::from("Example"),
String::from("https://example.com"),
ButtonStyle::Attention,
));
// Write HTML message
html_parser
.add(MessageTextFormat::Bold(String::from("Test BOLD message")))
.next_line()
.add(MessageTextFormat::Link(
String::from("https://example.com"),
String::from("Example"),
))
.next_line()
.add(MessageTextFormat::OrderedList(vec![
String::from("First"),
String::from("Second"),
String::from("Third"),
]))
.next_line()
.add(MessageTextFormat::Mention(chat.chat_id.to_owned()));
// Build reply message with keyboard
let msg = RequestMessagesSendTExt::new(chat.chat_id.to_owned())
.set_keyboard(kb)
.set_reply_msg_id(event.payload.msg_id.unwrap())
.set_text(html_parser.parse());
// Send message to chat
bot.messages_send_text(msg).await.unwrap();
}
_ => {
warn!("Not implmented EventType: {:?}. Skip", &event.event_type);
}
}
}
}