Skip to content

Commit

Permalink
feat: add initial commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Phosphorus-M committed Mar 4, 2024
1 parent 1100b78 commit 9b44a12
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 45 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ edition = "2021"
publish = false

[dependencies]
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
shuttle-runtime = "0.36.0"
shuttle-secrets = "0.36.0"
teloxide = { version = "0.12.2", features = ["teloxide-macros", "macros"] }
Expand Down
8 changes: 6 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ en Windows
```
Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr "https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1").Content
```
Y luego

Agregar tu Token de [@botfather](https://telegram.me/BotFather) en el `Secrets.dev.toml`

Y luego

```
cargo binstall cargo-shuttle
cargo shuttle run

```



<table><tr><td>

Un BOT de telegram de uso general para los canales de la Universidad Nacional del Oeste.
Expand Down
4 changes: 4 additions & 0 deletions Shuttle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Declare ignored files that should be included in deployment:
assets = [
"assets/*", # include all files and subdirs in static/
]
143 changes: 143 additions & 0 deletions assets/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
[
{
"url": "https://www.instagram.com/autoconvocadosuno/",
"title": "Instagram GEA"
},
{
"url": "https://www.facebook.com/autoconvocadosuno",
"title": "Facebook GEA"
},
{
"url": "https://www.instagram.com/genero_y_diversidad_gea/",
"title": "Genero y Diversidad - Instagram"
},
{
"url": "https://www.facebook.com/generoydiversidadgea",
"title": "Genero y Diversidad - Facebook"
},
{
"url": "https://www.facebook.com/cegresadosuno",
"title": "Centro de Egresados"
},
{
"url": "https://wa.me/5491133242915",
"title": "Tutorias Administración"
},
{
"url": "https://www.youtube.com/channel/UCxd-UFY6wChSTF7WJCCFz7g",
"title": "Youtube GEA"
},
{
"url": "https://docs.google.com/document/d/1E1Y8mML7hAQAJ_0C6RrXLYKjmFqNym4OATaAXUYJvYA/edit?usp=sharing",
"title": "Tutorías Informática"
},
{
"url": "https://docs.google.com/spreadsheets/d/1yIwkX-2vIMUAFnzGUT4LP2he41JGnovj0UIKqgqHweE/edit?usp=drivesdk",
"title": "Tarjeta de Beneficios"
},
{
"url": "https://docs.google.com/document/d/1H3S79xQP7XN_yMV-OJLq13wOExNfbtu2JU0rplCHRJQ/edit?usp=sharing",
"title": "Tutorías Ingeniería"
},
{
"url": "https://docs.google.com/document/d/18x3DSwftcavC-s6ukv2w_3z9bDOhpCHyyw9YVtWoOvY/edit?usp=drivesdk&fbclid=IwAR2uBq8Arzl_w-LN9j6hfactZg7FsjBCJwgnsZ8WcFgX9DmORW0UZfMIFvY",
"title": "Grupos de la carrera - INFORMATICA"
},
{
"url": "https://autogestion.uno.edu.ar/uno/",
"title": "SIU Guarani"
},
{
"url": "http://campusvirtual.uno.edu.ar/moodle/",
"title": "Campus Virtual"
},
{
"url": "https://docs.google.com/document/d/1OxtsP0X-j7xtzUXzaRUOV2JSIj0LwAZOGpcF-_7vX5I/edit?usp=sharing",
"title": "Grupos de la carrera - ENFERMERIA"
},
{

"url": "https://docs.google.com/document/d/10xNX9DHaG1tiYs42p2ltnTZ-UdvWHAxlx1uQqgvYg30/edit?usp=sharing",
"title": "Grupos de la carrera - ADMINISTRACION"
},
{
"url": "https://chat.whatsapp.com/GDllGTv7zBiGZQHlBapjR3",
"title": "Grupo de la carrera - INFORMATICA"
},
{
"url": "https://www.facebook.com/groups/informatica.uno.merlo",
"title": "Grupo de la carrera - FACEBOOK"
},
{
"url": "https://t.me/Informatica_UNO",
"title": "Grupo de la carrera - INFORMATICA (telegram)"
},
{
"url": "https://discord.gg/DgZnkwf",
"title": "Discord de la carrera"
},
{
"url": "https://t.me/gnuno_merlo",
"title": "GNUno"
},
{
"url": "https://chat.whatsapp.com/BV5aRoVPieZ9OKDzSiOd0M",
"title": "Grupo de la carrera - QUIMICA"
},
{
"url": "https://chat.whatsapp.com/BiQjWxHVnzqLRkD0olthhu?fbclid=IwAR3eGe2Gj0rmksYlJaW2ntTQyCDzfFJQGbgaojFp191LryuFu6dIWVCqx9o",
"title": "Grupo de la carrera - ADM"
},
{
"url": "https://wa.me/5491139154715",
"title": "Enfermería 1ro"
},
{
"url": "https://wa.me/5493885212445",
"title": "Enfermería 2do y 3ro"
},
{
"url": "https://wa.me/5491169364262",
"title": "Sist. Contables y Conta 1"
},
{
"url": "https://wa.me/5491133242915",
"title": "Tutorías ADM"
},
{
"url": "https://wa.me/5491156910849",
"title": "Tutorias Mate 1 y 2"
},
{
"url": "https://chat.whatsapp.com/FU5uDcZQBY4JrUYClg2Tgj",
"title": "Ingles"
},
{
"url": "https://drive.google.com/file/d/1BZP-SuGjH0kfXjkXCRwBaqim86I-8zb2/view?usp=drivesdk",
"title": "Resolucion de Cursada Virtual"
},
{
"url": "https://drive.google.com/file/d/0B41zO4qldC4eaHJ5OHFYWFp0UFk/view?usp=drivesdk",
"title": "Estatuto del Centro de Estudiantes"
},
{
"url": "https://drive.google.com/file/d/1OPWqtycr2VWqSKoAK1w7IeXs5wxk8A6M/view?usp=drivesdk",
"title": "Protocolo de Genero"
},
{
"url": "https://drive.google.com/file/d/0B2S9Bo8h-kwFSV92ZU5melNIbGM/view?usp=drivesdk",
"title": "Reglamento de Ayudante de Catedra"
},
{
"url": "https://drive.google.com/file/d/0B41zO4qldC4eSzZxWTdhMU45dkk/view?usp=drivesdk",
"title": "Reglamento Electoral"
},
{
"url": "https://drive.google.com/file/d/1GRgITeadkmpYh1lCa6DGYJXd0jeiKs_L/view?usp=drivesdk",
"title": "Reglamento de Examenes Virtuales"
},
{
"url": "https://drive.google.com/file/d/0B2S9Bo8h-kwFeEFIR19jWUpOdjQ/view?usp=drivesdk",
"title": "Reglamento de Alumnos"
}
]
37 changes: 2 additions & 35 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;

use crate::command::Command;
use crate::{command::Command, hooks::{left_chat_member::left_chat_member, new_chat_member::new_chat_member}};
use shuttle_runtime::Service;
use teloxide::{dispatching::UpdateFilterExt, prelude::*, utils::html, RequestError};
use teloxide::{dispatching::UpdateFilterExt, prelude::*, RequestError};
use tracing::log::debug;

pub struct BotService {
Expand Down Expand Up @@ -77,37 +77,4 @@ impl BotService {
}
}

/// Welcome Endpoint
async fn new_chat_member(bot: Bot, chat_member: ChatMemberUpdated) -> ResponseResult<()> {
let user = chat_member.old_chat_member.user.clone();

let telegram_group_name = chat_member.chat.title().unwrap_or("");

// We get a "@username" mention via `mention()` method if the user has a
// username, otherwise we create a textual mention with "Full Name" as the
// text linking to the user
let username = user
.mention()
.unwrap_or_else(|| html::user_mention(user.id.0 as i64, user.full_name().as_str()));

bot.send_message(chat_member.chat.id, format!("Bienvenidx a {telegram_group_name}, un lugar horrible pero bueno, pasala bien {username}!"))
.await?;

Ok(())
}

async fn left_chat_member(bot: Bot, chat_member: ChatMemberUpdated) -> ResponseResult<()> {
let user = chat_member.old_chat_member.user;

let username = user
.mention()
.unwrap_or_else(|| html::user_mention(user.id.0 as i64, user.full_name().as_str()));

bot.send_message(
chat_member.chat.id,
format!("Hasta la proximaaaa {username}!"),
)
.await?;

Ok(())
}
10 changes: 8 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use teloxide::{prelude::*, types::Me, utils::command::BotCommands};

use crate::bot::InstanceState;
use crate::{bot::InstanceState, commands::{hacer_algo::hacer_algo, links_utiles::links_utiles}};

/// Enumeration of commands accepted by the bot.
#[derive(BotCommands, Clone)]
Expand All @@ -16,6 +16,10 @@ Estos son los siguientes comandos:"
pub enum Command {
#[command(description = "Muestra este texto. Uso: /help")]
Help,
#[command(description = "Hace algo")]
HacerAlgo,
#[command(description = "Devuelve una lista de links")]
Links,
}

impl Command {
Expand All @@ -31,7 +35,9 @@ impl Command {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.await?;
}
},
Command::HacerAlgo => hacer_algo(&msg, &bot).await?,
Command::Links => links_utiles(&msg, &bot).await?
}

Ok(())
Expand Down
9 changes: 9 additions & 0 deletions src/commands/hacer_algo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use teloxide::{requests::{Requester, ResponseResult}, types::Message, Bot};


pub async fn hacer_algo(msg: &Message, bot: &Bot) -> ResponseResult<()> {
bot.send_message(msg.chat.id, "Hola mundo").await?;
println!("Hola mundo");

Ok(())
}
24 changes: 24 additions & 0 deletions src/commands/links_utiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use teloxide::{payloads::SendMessageSetters, requests::{Requester, ResponseResult}, types::Message, utils::markdown::escape, Bot, RequestError};
use teloxide::types::ParseMode::MarkdownV2;

use crate::models::link::Link;
use std::{fmt::Write, fs::read_to_string};


pub async fn links_utiles(msg: &Message, bot: &Bot) -> ResponseResult<()> {

let content = read_to_string("assets/links.json")?;
let links: Vec<Link> = serde_json::from_str(&content).map_err(|e|RequestError::InvalidJson { source: e, raw: content.into_boxed_str() })?;

let mut message = "Estos son los links que tenemos:\n".to_string();

for link in &links[0..18] {
let title = escape(&link.title);
let url = escape(&link.url);

let _ = writeln!(&mut message, "[{title}]({url})");
}
bot.send_message(msg.chat.id, message).parse_mode(MarkdownV2).await?;

Ok(())
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod hacer_algo;
pub mod links_utiles;
18 changes: 18 additions & 0 deletions src/hooks/left_chat_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use teloxide::{Bot, types::ChatMemberUpdated, requests::{ResponseResult, Requester}, utils::html};


pub async fn left_chat_member(bot: Bot, chat_member: ChatMemberUpdated) -> ResponseResult<()> {
let user = chat_member.old_chat_member.user;

let username = user
.mention()
.unwrap_or_else(|| html::user_mention(user.id.0 as i64, user.full_name().as_str()));

bot.send_message(
chat_member.chat.id,
format!("Hasta la proximaaaa {username}!"),
)
.await?;

Ok(())
}
2 changes: 2 additions & 0 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod left_chat_member;
pub mod new_chat_member;
23 changes: 23 additions & 0 deletions src/hooks/new_chat_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use teloxide::{Bot, types::ChatMemberUpdated, requests::{ResponseResult, Requester}, utils::html};



/// Welcome Endpoint
pub async fn new_chat_member(bot: Bot, chat_member: ChatMemberUpdated) -> ResponseResult<()> {
let user = chat_member.old_chat_member.user.clone();

let telegram_group_name = chat_member.chat.title().unwrap_or("");

// We get a "@username" mention via `mention()` method if the user has a
// username, otherwise we create a textual mention with "Full Name" as the
// text linking to the user
let username = user
.mention()
.unwrap_or_else(|| html::user_mention(user.id.0 as i64, user.full_name().as_str()));

bot.send_message(chat_member.chat.id, format!("Bienvenidx a {telegram_group_name}, un lugar horrible pero bueno, pasala bien {username}!"))
.await?;

Ok(())
}

Loading

0 comments on commit 9b44a12

Please sign in to comment.