dev #25
@ -178,12 +178,9 @@ pub async fn admin_command_handler(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let bi =
|
|
||||||
BotInstance::new(name.clone(), token.to_string(), DEFAULT_SCRIPT.to_string())
|
BotInstance::new(name.clone(), token.to_string(), DEFAULT_SCRIPT.to_string())
|
||||||
.store(&mut db)
|
.store(&mut db)
|
||||||
.await?;
|
.await?
|
||||||
|
|
||||||
bi
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bot.send_message(
|
bot.send_message(
|
||||||
|
|||||||
@ -1,21 +1,21 @@
|
|||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use quickjs_rusty::serde::{from_js, to_js};
|
use quickjs_rusty::serde::to_js;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::{
|
use std::{
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::{Arc, Mutex, RwLock},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
use teloxide::{
|
use teloxide::{
|
||||||
dispatching::{dialogue::GetChatId, UpdateFilterExt},
|
dispatching::{dialogue::GetChatId, UpdateFilterExt},
|
||||||
dptree::{self, Handler},
|
dptree::{self, Handler},
|
||||||
prelude::{DependencyMap, Requester},
|
prelude::{DependencyMap, Requester},
|
||||||
types::{CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, Message, Update},
|
types::{CallbackQuery, InlineKeyboardMarkup, Message, Update},
|
||||||
Bot,
|
Bot,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
botscript::{self, message_info::MessageInfoBuilder, BotMessage, RunnerConfig},
|
botscript::{self, message_info::MessageInfoBuilder, BotMessage},
|
||||||
commands::BotCommand,
|
commands::BotCommand,
|
||||||
db::{callback_info::CallbackInfo, CallDB, DB},
|
db::{callback_info::CallbackInfo, CallDB, DB},
|
||||||
message_answerer::MessageAnswerer,
|
message_answerer::MessageAnswerer,
|
||||||
@ -108,7 +108,7 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
|
|||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if bm.meta() == true {
|
if bm.meta() {
|
||||||
if let Some(ref meta) = variant {
|
if let Some(ref meta) = variant {
|
||||||
user.insert_meta(&mut db, meta).await?;
|
user.insert_meta(&mut db, meta).await?;
|
||||||
};
|
};
|
||||||
@ -193,8 +193,7 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
|
|||||||
let literal = bm.literal().map_or("", |s| s.as_str());
|
let literal = bm.literal().map_or("", |s| s.as_str());
|
||||||
|
|
||||||
let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0);
|
let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0);
|
||||||
ma.answer(literal, variant.as_ref().map(|v| v.as_str()), buttons)
|
ma.answer(literal, variant.as_deref(), buttons).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,28 +1,20 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
future::Future,
|
future::Future,
|
||||||
sync::{Arc, Mutex, RwLock},
|
sync::{Arc, Mutex},
|
||||||
thread::JoinHandle,
|
thread::JoinHandle,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use teloxide::{
|
use teloxide::{dispatching::dialogue::serializer::Json, dptree, prelude::Dispatcher, Bot};
|
||||||
dispatching::dialogue::serializer::Json,
|
|
||||||
dptree,
|
|
||||||
prelude::{Dispatcher, Requester},
|
|
||||||
types::{ChatId, UserId},
|
|
||||||
Bot,
|
|
||||||
};
|
|
||||||
use tokio::runtime::Handle;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bot_handler::{script_handler, BotHandler},
|
bot_handler::{script_handler, BotHandler},
|
||||||
db::{bots::BotInstance, DbError, DB},
|
db::{bots::BotInstance, DbError, DB},
|
||||||
message_answerer::MessageAnswerer,
|
message_answerer::MessageAnswerer,
|
||||||
mongodb_storage::MongodbStorage,
|
mongodb_storage::MongodbStorage,
|
||||||
BotController, BotError, BotResult, BotRuntime,
|
BotController, BotResult, BotRuntime,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct BotRunner {
|
pub struct BotRunner {
|
||||||
@ -235,7 +227,7 @@ pub async fn spawn_notificator_thread(
|
|||||||
Some(n) => {
|
Some(n) => {
|
||||||
// waiting time to send notification
|
// waiting time to send notification
|
||||||
tokio::time::sleep(n.wait_for()).await;
|
tokio::time::sleep(n.wait_for()).await;
|
||||||
'n: for n in n.notifications().into_iter() {
|
'n: for n in n.notifications().iter() {
|
||||||
for user in n.get_users(&c.db).await?.into_iter() {
|
for user in n.get_users(&c.db).await?.into_iter() {
|
||||||
let text = match n.resolve_message(&c.db, &user).await? {
|
let text = match n.resolve_message(&c.db, &user).await? {
|
||||||
Some(text) => text,
|
Some(text) => text,
|
||||||
|
|||||||
@ -2,7 +2,7 @@ pub mod application;
|
|||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod message_info;
|
pub mod message_info;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, Mutex, PoisonError};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::db::raw_calls::RawCallError;
|
use crate::db::raw_calls::RawCallError;
|
||||||
@ -12,7 +12,6 @@ use crate::utils::parcelable::{ParcelType, Parcelable, ParcelableError, Parcelab
|
|||||||
use chrono::{DateTime, Days, NaiveTime, ParseError, TimeDelta, Timelike, Utc};
|
use chrono::{DateTime, Days, NaiveTime, ParseError, TimeDelta, Timelike, Utc};
|
||||||
use db::attach_db_obj;
|
use db::attach_db_obj;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use futures::lock::MutexGuard;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use quickjs_rusty::serde::{from_js, to_js};
|
use quickjs_rusty::serde::{from_js, to_js};
|
||||||
use quickjs_rusty::utils::create_empty_object;
|
use quickjs_rusty::utils::create_empty_object;
|
||||||
@ -543,7 +542,8 @@ impl BotMessage {
|
|||||||
pub fn update_defaults(self) -> Self {
|
pub fn update_defaults(self) -> Self {
|
||||||
let bm = self;
|
let bm = self;
|
||||||
// if message is `start`, defaulting meta to true, if not set
|
// if message is `start`, defaulting meta to true, if not set
|
||||||
let bm = match bm.meta {
|
|
||||||
|
match bm.meta {
|
||||||
Some(_) => bm,
|
Some(_) => bm,
|
||||||
None => match &bm.literal {
|
None => match &bm.literal {
|
||||||
Some(l) if l == "start" => Self {
|
Some(l) if l == "start" => Self {
|
||||||
@ -552,9 +552,7 @@ impl BotMessage {
|
|||||||
},
|
},
|
||||||
_ => bm,
|
_ => bm,
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
|
|
||||||
bm
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_replace(&self) -> bool {
|
pub fn is_replace(&self) -> bool {
|
||||||
@ -1062,7 +1060,7 @@ impl Runner {
|
|||||||
#[allow(clippy::unwrap_used)]
|
#[allow(clippy::unwrap_used)]
|
||||||
#[allow(clippy::print_stdout)]
|
#[allow(clippy::print_stdout)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use quickjs_rusty::{serde::from_js, OwnedJsObject};
|
use quickjs_rusty::OwnedJsObject;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@ -6,9 +6,9 @@ use teloxide::Bot;
|
|||||||
use tokio::runtime::Handle;
|
use tokio::runtime::Handle;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{application::Application, message_forward::MessageForward, CallDB, DB},
|
db::{application::Application, message_forward::MessageForward, DB},
|
||||||
message_answerer::MessageAnswerer,
|
message_answerer::MessageAnswerer,
|
||||||
send_application_to_chat, BotError,
|
send_application_to_chat,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::ScriptError;
|
use super::ScriptError;
|
||||||
|
|||||||
@ -9,6 +9,12 @@ pub struct MessageInfoBuilder {
|
|||||||
inner: MessageInfo,
|
inner: MessageInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for MessageInfoBuilder {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MessageInfoBuilder {
|
impl MessageInfoBuilder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
use bson::doc;
|
use bson::doc;
|
||||||
use bson::oid::ObjectId;
|
|
||||||
use chrono::{DateTime, FixedOffset, Local};
|
use chrono::{DateTime, FixedOffset, Local};
|
||||||
use futures::StreamExt;
|
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ pub mod raw_calls;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::{DateTime, FixedOffset, Local, Utc};
|
use chrono::{DateTime, Local, Utc};
|
||||||
use enum_stringify::EnumStringify;
|
use enum_stringify::EnumStringify;
|
||||||
use futures::stream::TryStreamExt;
|
use futures::stream::TryStreamExt;
|
||||||
|
|
||||||
|
|||||||
@ -177,7 +177,7 @@ async fn test_drop_media_except() {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_random_users() {
|
async fn test_get_random_users() {
|
||||||
let mut db = setup_db().await;
|
let db = setup_db().await;
|
||||||
|
|
||||||
let users = db.get_random_users(1).await.unwrap();
|
let users = db.get_random_users(1).await.unwrap();
|
||||||
assert_eq!(users.len(), 1);
|
assert_eq!(users.len(), 1);
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@ -108,15 +106,15 @@ async fn newscript_handler(bot: Bot, mut db: DB, msg: Message, name: String) ->
|
|||||||
let mut bytes = bytes.unwrap().to_vec();
|
let mut bytes = bytes.unwrap().to_vec();
|
||||||
buf.append(&mut bytes);
|
buf.append(&mut bytes);
|
||||||
}
|
}
|
||||||
let script = match String::from_utf8(buf) {
|
|
||||||
|
match String::from_utf8(buf) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("Failed to parse buf to string, err: {err}");
|
warn!("Failed to parse buf to string, err: {err}");
|
||||||
bot.send_message(msg.chat.id, format!("Failed to Convert file to script: file is not UTF-8, err: {err}")).await?;
|
bot.send_message(msg.chat.id, format!("Failed to Convert file to script: file is not UTF-8, err: {err}")).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
script
|
|
||||||
}
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
@ -129,7 +127,7 @@ async fn newscript_handler(bot: Bot, mut db: DB, msg: Message, name: String) ->
|
|||||||
None => {
|
None => {
|
||||||
bot.send_message(
|
bot.send_message(
|
||||||
msg.chat.id,
|
msg.chat.id,
|
||||||
format!("Failed to set script, possibly bots name is incorrent"),
|
"Failed to set script, possibly bots name is incorrent".to_string(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
@ -85,7 +85,7 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
use teloxide::types::InlineKeyboardButton;
|
use teloxide::types::InlineKeyboardButton;
|
||||||
use teloxide::types::InlineKeyboardMarkup;
|
use teloxide::types::InlineKeyboardMarkup;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user