Add scheme.

This commit is contained in:
AB
2022-01-23 15:02:21 +03:00
parent e5079fa584
commit 20f366572b
5 changed files with 108 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "desubot" name = "desubot"
version = "0.5.8" version = "0.5.9"
authors = ["AB <ab@hexor.ru>"] authors = ["AB <ab@hexor.ru>"]
edition = "2018" edition = "2018"

View File

@@ -1,5 +1,5 @@
#[allow(dead_code)] #[allow(dead_code)]
static CODE_HELP: &str = "<b>Code highlighter</b> pub(crate) static CODE_HELP: &str = "<b>Code highlighter</b>
<i>Usage</i> <i>Usage</i>
<pre>/code <pre>/code
@@ -7,7 +7,7 @@ static CODE_HELP: &str = "<b>Code highlighter</b>
#&lt;lang - JS by default&gt; #&lt;theme - Dracula by default&gt;</pre> #&lt;lang - JS by default&gt; #&lt;theme - Dracula by default&gt;</pre>
Language may be defined by both name and extension - Rust, rs... Language may be defined by both name and extension - Rust, rs...
Max lines - 80 Max length - 4000
List of themes: List of themes:
1337 1337

View File

@@ -21,6 +21,7 @@ use syntect::highlighting::Theme;
use syntect::parsing::SyntaxReference; use syntect::parsing::SyntaxReference;
use syntect::util::LinesWithEndings; use syntect::util::LinesWithEndings;
use telegram_bot::prelude::*; use telegram_bot::prelude::*;
use telegram_bot::*;
use telegram_bot::{Api, Message, ParseMode, UserId}; use telegram_bot::{Api, Message, ParseMode, UserId};
include!("../assets/help_text.rs"); include!("../assets/help_text.rs");
@@ -46,6 +47,9 @@ pub struct Sql {
pub struct Code { pub struct Code {
pub data: String, pub data: String,
} }
pub struct Scheme {
pub data: String,
}
#[async_trait] #[async_trait]
pub trait Execute { pub trait Execute {
@@ -59,6 +63,68 @@ pub trait Execute {
) -> Result<(), Error>; ) -> Result<(), Error>;
} }
#[async_trait]
impl Execute for Scheme {
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
match api
.send(
message
.text_reply(format!(
"{}{}{}",
"<pre>",
include_str!("../assets/scheme.sql").to_string(),
"</pre>"
))
.parse_mode(ParseMode::Html),
)
.await
{
Ok(_) => debug!("/scheme command sent to {}", message.chat.id()),
Err(_) => warn!("/scheme command sent failed to {}", message.chat.id()),
};
match {
Code {
data: format!(
"{}{}",
include_str!("../assets/scheme.sql").to_string(),
"\n#sql"
),
}
.exec_with_result(&api, &message)
.await
} {
Ok(path) => {
let file = InputFileUpload::with_path(path.clone());
// api.send(message.chat.document(&file)).await?;
//
// // Send an image from disk
api.send(message.chat.document(&file)).await?;
//debug!("{:#?}", formatter);
let _ = std::fs::remove_file(&path);
}
Err(_) => {
let _ = api
.send(message.text_reply(CODE_HELP).parse_mode(ParseMode::Html))
.await?;
}
}
Ok(())
}
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
unimplemented!()
}
async fn exec_mystem(
&self,
api: &Api,
message: &Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait] #[async_trait]
impl Execute for Sql { impl Execute for Sql {
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> { async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
@@ -571,9 +637,6 @@ impl Execute for Code {
.split("\n") .split("\n")
.map(|s| s.to_string()) .map(|s| s.to_string())
.collect(); .collect();
if lines.len() >= 81 {
return Err(CodeHighlightningError);
}
let last_line = &lines[lines.len() - 1]; let last_line = &lines[lines.len() - 1];
let tags = last_line let tags = last_line

View File

@@ -1,3 +1,5 @@
#[allow(unused_mut)]
#[allow(dead_code)]
use crate::errors; use crate::errors;
use crate::utils; use crate::utils;
use rusqlite::{named_params, params, Connection, Error, Result}; use rusqlite::{named_params, params, Connection, Error, Result};
@@ -37,17 +39,21 @@ pub(crate) fn update_scheme() -> Result<()> {
pub(crate) fn load_stopwords() -> Result<()> { pub(crate) fn load_stopwords() -> Result<()> {
info!("Populating stop words wait please."); info!("Populating stop words wait please.");
let conn = open()?; let conn = open()?;
for table in include_str!("../assets/stop-words.txt").split('\n').into_iter() { for table in include_str!("../assets/stop-words.txt")
.split('\n')
.into_iter()
{
let word = table.trim(); let word = table.trim();
if word != "" { if word != "" {
let mut stmt = conn.prepare_cached( let mut _stmt = conn
.prepare_cached(
" "
INSERT OR IGNORE INTO INSERT OR IGNORE INTO
stop_words('word') stop_words('word')
VALUES (:word) VALUES (:word)
", ",
)?.insert(params![word]); )?
//let mut rows = stmt.word(named_params! {":conf_id": conf_id})?; .insert(params![word]);
} }
} }
info!("Stop words updated."); info!("Stop words updated.");

View File

@@ -1,5 +1,5 @@
//use crate::commands::Command; //use crate::commands::Command;
use crate::commands::{Code, Execute, Here, Markov, MarkovAll, Omedeto, Sql, Top}; use crate::commands::{Code, Execute, Here, Markov, MarkovAll, Omedeto, Scheme, Sql, Top};
use crate::db; use crate::db;
use crate::errors; use crate::errors;
use crate::utils; use crate::utils;
@@ -38,11 +38,30 @@ pub async fn handler(
.await .await
} { } {
Ok(path) => { Ok(path) => {
let mut cnt_lines = 0;
for _ in s.lines() {
cnt_lines = cnt_lines + 1;
}
let mut cnt_chars = 0;
for _ in s.chars() {
cnt_chars = cnt_chars + 1;
}
let file = InputFileUpload::with_path(path.clone()); let file = InputFileUpload::with_path(path.clone());
info!("lines: {}, chars: {}", cnt_lines, cnt_chars);
// api.send(message.chat.document(&file)).await?; // api.send(message.chat.document(&file)).await?;
// //
// // Send an image from disk // // Send an image from disk
if cnt_chars > 4000 {
let _ = api
.send(message.text_reply(CODE_HELP).parse_mode(ParseMode::Html))
.await?;
return Ok(());
}
if cnt_lines < 81 {
api.send(message.chat.photo(&file)).await?; api.send(message.chat.photo(&file)).await?;
} else {
api.send(message.chat.document(&file)).await?;
}
//debug!("{:#?}", formatter); //debug!("{:#?}", formatter);
let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&path);
} }
@@ -116,6 +135,13 @@ pub async fn handler(
.exec(&api, &message) .exec(&api, &message)
.await? .await?
} }
s if s =="/scheme" || s == "/schema" => {
Scheme {
data: "".to_string(),
}
.exec(&api, &message)
.await?
}
"/omedeto" => { "/omedeto" => {
Omedeto { Omedeto {
data: "".to_string(), data: "".to_string(),