Crates.io | voiceflousion |
lib.rs | voiceflousion |
version | 0.3.1 |
source | src |
created_at | 2024-07-23 20:03:02.854262 |
updated_at | 2024-08-21 16:51:15.080782 |
description | A crate that provides toolkit for Voiceflow AI bots integrations and pre-built functionality for quick integration with popular messengers |
homepage | https://github.com/Vondert/voiceflousion |
repository | https://github.com/Vondert/voiceflousion |
max_upload_size | |
id | 1313219 |
size | 352,642 |
Voiceflousion is a framework designed to integrate chatbots from the Voiceflow chatbot constructor into any chat platform. Currently, it supports Voiceflow bots integration with Telegram and WhatsApp, with future plans for Instagram, Discord, and more. The framework also provides its own web server for launching chatbots and tools for creating custom integrations for any chat platform, supporting message formats such as text, buttons, images, cards, and carousels.
It is recommended to store sensitive information like API keys in a .env
file. Here is an example of the .env
file content:
VF_API_KEY=VF.DM.xxxxxxxxxxxx.xxxxxxxxxx
BOT_ID=32487267c832hx4h248
VERSION_ID=27hd634532742g424234
TELEGRAM_BOT_TOKEN=4324234324:xxxxxxxxxxxxxxxx
WHATSAPP_BOT_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WHATSAPP_BOT_ID=xxxxxxxxxxxxxxxxxxxx
git clone https://github.com/Vondert/voiceflousion.git
cd voiceflousion
cd example
.env.example
to .env
and fill in the required information:mv .env.example .env
cargo run --release
Create a .env
file in the root directory of your project with the necessary variables:
VF_API_KEY=VF.DM.xxxxxxxxxxxx.xxxxxxxxxx
BOT_ID=32487267c832hx4h248
VERSION_ID=27hd634532742g424234
TELEGRAM_BOT_TOKEN=4324234324:xxxxxxxxxxxxxxxx
WHATSAPP_BOT_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WHATSAPP_BOT_ID=xxxxxxxxxxxxxxxxxxxx
Add the following dependencies to your Cargo.toml
file:
[package]
name = "example"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.193", features = ["derive"] }
dotenv = "0.15.0"
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread", "macros"] }
voiceflousion = { version = "0.3.1", features = ["all-integrations", "server"] }
Load Environment Variables: Use dotenv to load environment variables from the .env
file.
use dotenv::dotenv;
dotenv().ok();
Retrieve Environment Variables: Get the necessary API keys and IDs from the environment.
use std::env;
let bot_id: String = env::var("BOT_ID").unwrap_or_else(|_| "".to_string());
let version_id: String = env::var("VERSION_ID").unwrap_or_else(|_| "".to_string());
let vf_api_key: String = env::var("VF_API_KEY").unwrap_or_else(|_| "".to_string());
let telegram_bot_token = env::var("TELEGRAM_BOT_TOKEN").unwrap_or_else(|_| "".to_string());
let telegram_bot_id = telegram_bot_token.split(':').next().unwrap().to_string();
let whatsapp_bot_token = env::var("WHATSAPP_BOT_TOKEN").unwrap_or_else(|_| "".to_string());
let whatsapp_bot_id = env::var("WHATSAPP_BOT_ID").unwrap_or_else(|_| "".to_string());
Initialize Voiceflow Client: Create a new VoiceflowClient
instance.
use std::sync::Arc;
use voiceflousion::core::voiceflow::VoiceflowClient;
let voiceflow_client = Arc::new(VoiceflowClient::new(vf_api_key, bot_id.clone(), version_id, 10, None));
Build Telegram Client and WhatsApp Client: Set up the TelegramClient
and WhatsAppClient
with session management.
use voiceflousion::core::ClientBuilder;
use voiceflousion::integrations::telegram::TelegramClient;
let client_builder = ClientBuilder::new(telegram_bot_id.clone(), telegram_bot_token.clone(), voiceflow_client.clone(), 10)
.add_session_duration(120)
.allow_sessions_cleaning(60);
let telegram_client = Arc::new(TelegramClient::new(client_builder));
let client_builder = ClientBuilder::new(whatsapp_bot_id.clone(), whatsapp_bot_token.clone(), voiceflow_client.clone(), 10)
.set_session_duration(120)
.allow_sessions_cleaning(60);
let whatsapp_client = WhatsAppClient::new(client_builder);
Build Telegram Client and WhatsApp Client managers: Set up the ClientManager
with created clients
use voiceflousion::core::base_structs::ClientsManager;
let telegram_client_manager = Arc::new(ClientsManager::from_clients(vec![telegram_client]));
let whatsapp_client_manager = Arc::new(ClientsManager::from_clients(vec![whatsapp_client]));
Set Up Voiceflousion server: Create VoiceflousionServer
with previously built ClientManager
instances and set up the base dialog handler for handling updates from clients.
use voiceflousion::server::handlers::base_dialog_handler;
use voiceflousion::server::VoiceflousionServer;
let telegram_voiceflousion_server = VoiceflousionServer::<TelegramClient>::new({
|update, client| Box::pin(base_dialog_handler(update, client))
})
.set_clients_manager(telegram_client_manager);
let whatsapp_voiceflousion_server = VoiceflousionServer::<WhatsAppClient>::new({
|update, client| Box::pin(base_dialog_handler(update, client))
})
.set_clients_manager(whatsapp_client_manager);
Run the Server: Start the server to listen for incoming webhook requests.
let telegram_voiceflousion_server_closure = async {
telegram_voiceflousion_server.run(([127, 0, 0, 1], 8080)).await;
};
let whatsapp_voiceflousion_server_closure = async {
whatsapp_voiceflousion_server.run(([127, 0, 0, 1], 8081)).await;
};
tokio::join!(telegram_voiceflousion_server_closure, whatsapp_voiceflousion_server_closure);
Receive webhook address: Copy given urls from console and set webhook with Telegram API and in WhatsApp application on Meta developers platform.
Server is set on 127.0.0.1:8080
Bots without authentication token are available on 127.0.0.1:8080/telegram/:id
Bots with authentication token are available on 127.0.0.1:8080/telegram/:id/?token=<token>
Server is set on 127.0.0.1:8081
Bots without authentication token are available on 127.0.0.1:8081/whatsapp/:id
Bots with authentication token are available on 127.0.0.1:8081/whatsapp/:id/?token=<token>
The dependencies required to run the project are listed in the example/Cargo.toml file. For custom integrations, you may need the async-trait
crate.
By using this framework, you can easily and flexibly integrate your bots with pre-built integrations for Telegram and WhatsApp, create powerful and configurable bot management system. Voiceflousion also provides toolkit for creating custom integrations by implementing the Client
, Update
, Sender
, and Responder
traits. Once implemented, you can use your custom client in the same way as demonstrated in the example. For using VoiceflousionServer
with your custom Client
type you also need to implement ServerClient
trait.
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
Vondert (Ivan Milennyi)
Github: https://github.com/Vondert
Linkedin: https://www.linkedin.com/in/ivan-milennyi-1084842a5/
Email: 25042018avic@gmail.com