vkteams-bot 0.1.5

VK Teams Bot API client
Documentation
vkteams-bot-0.1.5 has been yanked.

VK Teams Bot API client

VK Teams Bot API client written in Rust.

Table of Contents

Environment

  1. Begin with bot API following instructions
  2. Set environment variables or save in .env file
# 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
  1. Put lines in you Cargo.toml file
[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() -> Result<(), Box<dyn std::error::Error>> {
    // 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.unwrap();
    Ok(())
}

async fn send_text_msg(bot: &Bot) -> Result<(), Box<dyn std::error::Error>> {
    // 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::new();
        // New HTML parser
        let mut html_parser = MessageTextParser::new();
        // Check event type
        match event.event_type {
            EventType::NewMessage => {
                // Add button with callback
                kb.add_button(&ButtonKeyboard::cb(
                    String::from("test"),
                    String::from("test_callback_data"),
                    ButtonStyle::Primary,
                ))
                // Add button with url
                .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"),
                    ));
                // Build reply message with keyboard
                let msg = RequestMessagesSendTExt::new(event.payload.chat.unwrap().chat_id)
                    .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);
            }
        }
    }
    Ok(())
}