Compare commits
31 Commits
d442cccebe
...
ff816c4fdc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff816c4fdc | ||
|
|
0ba10a1600 | ||
|
|
0c94673766 | ||
|
|
4aa3cb5080 | ||
|
|
af4cad6765 | ||
|
|
bdc0d6432c | ||
|
|
e74f875e3b | ||
|
|
1669327df2 | ||
|
|
1e046e2e19 | ||
|
|
79acdc283e | ||
|
|
97e7886149 | ||
|
|
585c3599ab | ||
|
|
f08af9609e | ||
| 12288ced5c | |||
| 5955fc297d | |||
| bf816c4c5c | |||
| 92c97abd4a | |||
| ab3cf214d1 | |||
| a5f02631e8 | |||
| e71cde3735 | |||
| c80fcbe025 | |||
| 7b39cd18cf | |||
| a0b22e2ef3 | |||
| 0c1f08a9db | |||
| 40dec27adc | |||
| 1d80fd056b | |||
| 62b68a6451 | |||
| 9be1f3ec1d | |||
| fe08b42581 | |||
| d49971995c | |||
| f7318f3661 |
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -897,6 +897,12 @@ version = "0.31.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
||||
|
||||
[[package]]
|
||||
name = "git-const"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa4f63cc0cd93d91bcc11b46f61db771bc36c405d96043506ae40facd87b31f7"
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.2"
|
||||
@ -915,6 +921,7 @@ dependencies = [
|
||||
"enum_stringify",
|
||||
"envconfig",
|
||||
"futures",
|
||||
"git-const",
|
||||
"itertools 0.14.0",
|
||||
"lazy_static",
|
||||
"log",
|
||||
@ -1463,8 +1470,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "libquickjs-ng-sys"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c98c1ad542ec61348faba7ce5386fef9060e35fbeea19dda64ce41862084e0a"
|
||||
source = "git+https://github.com/akulij/quickjs-rusty.git?rev=549f830#549f8308b737006098293b2f0a5ff8ed420442b5"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"cc",
|
||||
@ -2009,8 +2015,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "quickjs-rusty"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3b4d659d1bc37e9112a14ad9a7727d182b0fb12216eb6684bdbada3e9991a22"
|
||||
source = "git+https://github.com/akulij/quickjs-rusty.git?rev=549f830#549f8308b737006098293b2f0a5ff8ed420442b5"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
|
||||
@ -14,12 +14,13 @@ dotenvy = "0.15.7"
|
||||
enum_stringify = "0.6.3"
|
||||
envconfig = "0.11.0"
|
||||
futures = "0.3.31"
|
||||
git-const = "1.1.0"
|
||||
itertools = "0.14.0"
|
||||
lazy_static = "1.5.0"
|
||||
log = "0.4.27"
|
||||
mongodb = "3.2.3"
|
||||
pretty_env_logger = "0.5.0"
|
||||
quickjs-rusty = "0.9.0"
|
||||
quickjs-rusty = { git = "https://github.com/akulij/quickjs-rusty.git", rev = "549f830" }
|
||||
serde = { version = "1.0.219", features = ["derive", "serde_derive"] }
|
||||
serde_json = "1.0.140"
|
||||
teloxide = { version = "0.14.0", features = ["macros", "postgres-storage-nativetls"] }
|
||||
|
||||
@ -43,7 +43,6 @@ const dialog = {
|
||||
|
||||
function leave_application(user) {
|
||||
print("point of reach")
|
||||
print(JSON.stringify(user))
|
||||
user_application(user)
|
||||
|
||||
return false
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use git_const::git_hash;
|
||||
use itertools::Itertools;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
@ -44,6 +45,8 @@ pub enum AdminCommands {
|
||||
Cancel,
|
||||
/// Create new instance of telegram bot
|
||||
Deploy { token: String },
|
||||
/// Get commit hash of this bot
|
||||
Commit,
|
||||
}
|
||||
|
||||
pub async fn admin_command_handler(
|
||||
@ -189,6 +192,12 @@ pub async fn admin_command_handler(
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
AdminCommands::Commit => {
|
||||
let hash = git_hash!();
|
||||
bot.send_message(msg.chat.id, format!("Commit: {hash}"))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ use log::{error, info};
|
||||
use quickjs_rusty::serde::to_js;
|
||||
use std::{
|
||||
str::FromStr,
|
||||
sync::{Arc, RwLock},
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
use teloxide::{
|
||||
dispatching::{dialogue::GetChatId, UpdateFilterExt},
|
||||
@ -23,7 +23,7 @@ use crate::{
|
||||
pub type BotHandler =
|
||||
Handler<'static, DependencyMap, BotResult<()>, teloxide::dispatching::DpHandlerDescription>;
|
||||
|
||||
pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
|
||||
pub fn script_handler(r: Arc<Mutex<BotRuntime>>) -> BotHandler {
|
||||
let cr = r.clone();
|
||||
dptree::entry()
|
||||
.branch(
|
||||
@ -35,7 +35,8 @@ pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
|
||||
let r = std::sync::Arc::clone(&r);
|
||||
let command = bc.command();
|
||||
|
||||
let rc = r.rc.lock().expect("RwLock lock on commands map failed");
|
||||
let r = r.lock().expect("RwLock lock on commands map failed");
|
||||
let rc = &r.rc;
|
||||
|
||||
rc.get_command_message(command)
|
||||
})
|
||||
@ -46,7 +47,8 @@ pub fn script_handler(r: Arc<BotRuntime>) -> BotHandler {
|
||||
.filter_map(move |q: CallbackQuery| {
|
||||
q.data.and_then(|data| {
|
||||
let r = std::sync::Arc::clone(&cr);
|
||||
let rc = r.rc.lock().expect("RwLock lock on commands map failed");
|
||||
let r = r.lock().expect("RwLock lock on commands map failed");
|
||||
let rc = &r.rc;
|
||||
|
||||
rc.get_callback_message(&data)
|
||||
})
|
||||
@ -155,9 +157,9 @@ async fn handle_callback(bot: Bot, mut db: DB, bm: BotMessage, q: CallbackQuery)
|
||||
handler,
|
||||
bm.literal()
|
||||
);
|
||||
match handler.call_args(vec![]) {
|
||||
match handler.call_args(vec![jsuser]) {
|
||||
Ok(v) => {
|
||||
println!("Ok branch, value: {:?}", v.js_to_string());
|
||||
println!("Ok branch, got value: {v:?}");
|
||||
if v.is_bool() {
|
||||
v.to_bool().unwrap_or(true)
|
||||
} else if v.is_int() {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
sync::{Arc, RwLock},
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
thread::JoinHandle,
|
||||
time::Duration,
|
||||
};
|
||||
@ -14,6 +14,7 @@ use teloxide::{
|
||||
prelude::{Dispatcher, Requester},
|
||||
Bot,
|
||||
};
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
use crate::{
|
||||
bot_handler::{script_handler, BotHandler},
|
||||
@ -28,18 +29,11 @@ pub struct BotRunner {
|
||||
thread: Option<JoinHandle<BotResult<()>>>,
|
||||
}
|
||||
|
||||
unsafe impl Sync for BotRunner {}
|
||||
unsafe impl Send for BotRunner {}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BotInfo {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref BOT_POOL: RwLock<HashMap<String, BotRunner>> = RwLock::new(HashMap::new());
|
||||
}
|
||||
|
||||
pub static DEFAULT_SCRIPT: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/default_script.js"));
|
||||
|
||||
@ -171,7 +165,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
async fn script_handler_gen(r: Arc<BotRuntime>, plug_handlers: Vec<BotHandler>) -> BotHandler {
|
||||
async fn script_handler_gen(
|
||||
r: Arc<Mutex<BotRuntime>>,
|
||||
plug_handlers: Vec<BotHandler>,
|
||||
) -> BotHandler {
|
||||
let handler = script_handler(r.clone());
|
||||
// each handler will be added to dptree::entry()
|
||||
let handler = plug_handlers
|
||||
@ -182,48 +179,6 @@ async fn script_handler_gen(r: Arc<BotRuntime>, plug_handlers: Vec<BotHandler>)
|
||||
handler
|
||||
}
|
||||
|
||||
pub async fn start_bot(
|
||||
bi: BotInstance,
|
||||
db: &mut DB,
|
||||
plug_handlers: Vec<BotHandler>,
|
||||
) -> BotResult<BotInfo> {
|
||||
let db = db.clone().with_name(bi.name.clone());
|
||||
let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?;
|
||||
|
||||
let handler = script_handler(controller.runtime.clone());
|
||||
// each handler will be added to dptree::entry()
|
||||
let handler = plug_handlers
|
||||
.into_iter()
|
||||
// as well as the script handler at the end
|
||||
.chain(std::iter::once(handler))
|
||||
.fold(dptree::entry(), |h, plug| h.branch(plug));
|
||||
|
||||
let thread = spawn_bot_thread(controller.bot.clone(), controller.db.clone(), handler).await?;
|
||||
|
||||
let info = BotInfo {
|
||||
name: bi.name.clone(),
|
||||
};
|
||||
let runner = BotRunner {
|
||||
controller,
|
||||
info: info.clone(),
|
||||
thread: Some(thread),
|
||||
};
|
||||
|
||||
BOT_POOL
|
||||
.write()
|
||||
.map_or_else(
|
||||
|err| {
|
||||
Err(BotError::RwLockError(format!(
|
||||
"Failed to lock BOT_POOL because previous thread paniced, err: {err}"
|
||||
)))
|
||||
},
|
||||
Ok,
|
||||
)?
|
||||
.insert(bi.name.clone(), runner);
|
||||
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
pub async fn spawn_bot_thread(
|
||||
bot: Bot,
|
||||
mut db: DB,
|
||||
@ -235,9 +190,10 @@ pub async fn spawn_bot_thread(
|
||||
let thread = std::thread::spawn(move || -> BotResult<()> {
|
||||
let state_mgr = state_mgr;
|
||||
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
// let rt = tokio::runtime::Builder::new_current_thread()
|
||||
// .enable_all()
|
||||
// .build()?;
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
|
||||
rt.block_on(
|
||||
Dispatcher::builder(bot, handler)
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@ -104,16 +104,14 @@ type CallbackStore = CallbackInfo<Callback>;
|
||||
pub struct BotController {
|
||||
pub bot: Bot,
|
||||
pub db: DB,
|
||||
pub runtime: Arc<BotRuntime>,
|
||||
pub runtime: Arc<Mutex<BotRuntime>>,
|
||||
}
|
||||
|
||||
pub struct BotRuntime {
|
||||
pub rc: Mutex<RunnerConfig>,
|
||||
pub rc: RunnerConfig,
|
||||
pub runner: Runner,
|
||||
}
|
||||
|
||||
unsafe impl Send for BotRuntime {}
|
||||
unsafe impl Sync for BotRuntime {}
|
||||
|
||||
impl Drop for BotController {
|
||||
fn drop(&mut self) {
|
||||
@ -121,8 +119,6 @@ impl Drop for BotController {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for BotController {}
|
||||
|
||||
const MAIN_BOT_SCRIPT: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/mainbot.js"));
|
||||
|
||||
impl BotController {
|
||||
@ -148,10 +144,7 @@ impl BotController {
|
||||
let mut runner = Runner::init_with_db(&mut db)?;
|
||||
runner.call_attacher(|c, o| attach_user_application(c, o, &db, &bot))??;
|
||||
let rc = runner.init_config(script)?;
|
||||
let runtime = Arc::new(BotRuntime {
|
||||
rc: Mutex::new(rc),
|
||||
runner,
|
||||
});
|
||||
let runtime = Arc::new(Mutex::new(BotRuntime { rc, runner }));
|
||||
|
||||
Ok(Self { bot, db, runtime })
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user