mirror of
https://github.com/house-of-vanity/desubot.git
synced 2025-07-07 20:54:06 +00:00
Init
This commit is contained in:
22
src/commands.rs
Normal file
22
src/commands.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use crate::db;
|
||||
use telegram_bot::prelude::*;
|
||||
use telegram_bot::{Api, Error, Message, MessageKind, ParseMode, UpdateKind};
|
||||
use tokio::time::delay_for;
|
||||
|
||||
pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
|
||||
let members: Vec<telegram_bot::User> = db::get_members(&message.chat.id()).unwrap();
|
||||
let mut msg = "I summon you, ".to_string();
|
||||
for user in members {
|
||||
let mention = match user.username {
|
||||
Some(username) => format!("@{}", username),
|
||||
_ => format!("[{}](tg://user?id={})", user.first_name, user.id),
|
||||
};
|
||||
msg = format!("{} {}", msg, mention);
|
||||
}
|
||||
|
||||
api.send(message.text_reply(msg).parse_mode(ParseMode::MarkdownV2))
|
||||
.await?;
|
||||
//api.send(message.chat.text("Text to message chat")).await?;
|
||||
//api.send(message.from.text("Private text")).await?;
|
||||
Ok(())
|
||||
}
|
95
src/db.rs
Normal file
95
src/db.rs
Normal file
@ -0,0 +1,95 @@
|
||||
use rusqlite::{named_params, params, Connection, Result};
|
||||
use telegram_bot::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct User {
|
||||
id: i64,
|
||||
username: Option<String>,
|
||||
first_name: String,
|
||||
last_name: Option<String>,
|
||||
date: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Conf {
|
||||
id: i64,
|
||||
title: Option<String>,
|
||||
date: i32,
|
||||
}
|
||||
|
||||
pub(crate) fn open() -> Result<Connection> {
|
||||
let path = "./memory.sqlite3";
|
||||
let db = Connection::open(&path)?;
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
pub(crate) fn get_user(id: i64) -> Result<telegram_bot::User> {
|
||||
let conn = open()?;
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT id, username, first_name, last_name, date FROM user WHERE id = :id")?;
|
||||
|
||||
let mut rows = stmt.query_named(&[(":id", &id)])?;
|
||||
let mut users = Vec::new();
|
||||
|
||||
while let Some(row) = rows.next()? {
|
||||
users.push(telegram_bot::User {
|
||||
id: UserId::new(row.get(0)?),
|
||||
username: row.get(1)?,
|
||||
is_bot: false,
|
||||
first_name: row.get(2)?,
|
||||
last_name: row.get(3)?,
|
||||
language_code: None,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(users[0].clone())
|
||||
}
|
||||
|
||||
pub(crate) fn get_confs() -> Result<Vec<Conf>> {
|
||||
let conn = open()?;
|
||||
let mut stmt = conn.prepare("SELECT id, title, date FROM conf")?;
|
||||
|
||||
let mut rows = stmt.query(params![])?;
|
||||
let mut confs = Vec::new();
|
||||
|
||||
while let Some(row) = rows.next()? {
|
||||
confs.push(Conf {
|
||||
id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
date: row.get(2)?,
|
||||
})
|
||||
}
|
||||
println!("Confs: {:?}", confs);
|
||||
|
||||
Ok(confs)
|
||||
}
|
||||
|
||||
pub(crate) fn get_members(id: &telegram_bot::ChatId) -> Result<Vec<telegram_bot::User>> {
|
||||
let conn = open()?;
|
||||
let str_id = id.to_string();
|
||||
let mut stmt = conn.prepare(
|
||||
"
|
||||
SELECT DISTINCT(u.username), u.id, u.first_name, u.last_name, u.date
|
||||
FROM relations r
|
||||
JOIN user u
|
||||
ON u.id = r.user_id
|
||||
LEFT JOIN conf c
|
||||
ON r.conf_id = c.id
|
||||
WHERE c.id = :id",
|
||||
)?;
|
||||
let mut rows = stmt.query_named(&[(":id", &str_id)])?;
|
||||
let mut users = Vec::new();
|
||||
|
||||
while let Some(row) = rows.next()? {
|
||||
users.push(telegram_bot::User {
|
||||
id: UserId::new(row.get(1)?),
|
||||
username: row.get(0)?,
|
||||
is_bot: false,
|
||||
first_name: row.get(2)?,
|
||||
last_name: row.get(3)?,
|
||||
language_code: None,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(users)
|
||||
}
|
67
src/main.rs
Normal file
67
src/main.rs
Normal file
@ -0,0 +1,67 @@
|
||||
use std::env;
|
||||
|
||||
use futures::StreamExt;
|
||||
use telegram_bot::types::chat::MessageChat;
|
||||
use telegram_bot::*;
|
||||
|
||||
mod commands;
|
||||
mod db;
|
||||
|
||||
async fn handler(api: Api, message: Message) -> Result<(), Error> {
|
||||
match message.kind {
|
||||
MessageKind::Text { ref data, .. } => {
|
||||
let title = match &message.chat {
|
||||
MessageChat::Supergroup(chat) => &chat.title,
|
||||
_ => "test",
|
||||
};
|
||||
|
||||
println!(
|
||||
"<{}>[{}::{}({})]: {}",
|
||||
&message.chat.id(),
|
||||
title,
|
||||
&message.from.id,
|
||||
&message.from.first_name,
|
||||
data
|
||||
);
|
||||
match data.as_str() {
|
||||
"/here" => commands::here(api, message).await?,
|
||||
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
/*
|
||||
println!("get_user: {:?}", db::get_user(1228494339));
|
||||
println!("get_confs: {:?}", db::get_confs());
|
||||
println!("get_members: {:?}", db::get_members(-1001233797421));
|
||||
|
||||
*/
|
||||
let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set");
|
||||
let api = Api::new(token);
|
||||
|
||||
// Fetch new updates via long poll method
|
||||
let mut stream = api.stream();
|
||||
while let Some(update) = stream.next().await {
|
||||
// If the received update contains a new message...
|
||||
let update = update?;
|
||||
if let UpdateKind::Message(message) = update.kind {
|
||||
handler(api.clone(), message).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/*
|
||||
{ id: MessageId(94793), from:
|
||||
User { id: UserId(124317807), first_name: "Холм", last_name: Some("Вечный"), username: Some("ultradesu"), is_bot: false, language_code: Some("en") },
|
||||
date: 1606564847,
|
||||
chat: Supergroup(Supergroup { id: SupergroupId(-1001233797421), title: "Квантовый Аллах", username: None, invite_link: None }),
|
||||
forward: None, reply_to_message: None, edit_date: None, kind: Text { data: "пук", entities: [] }
|
||||
|
||||
*/
|
Reference in New Issue
Block a user