mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-22 11:04:21 +00:00
Setup command framework and add /start command
This commit is contained in:
parent
1d69eb7f6e
commit
26d385503e
6 changed files with 921 additions and 36 deletions
806
Cargo.lock
generated
806
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -6,6 +6,8 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
diesel = { version = "2.2.1", features = ["postgres"] }
|
diesel = { version = "2.2.1", features = ["postgres"] }
|
||||||
|
log = "0.4.22"
|
||||||
micronfig = "0.3.0"
|
micronfig = "0.3.0"
|
||||||
teloxide = "0.12.2"
|
pretty_env_logger = "0.5.0"
|
||||||
tokio = { version = "1.38.0", features = ["full", "macros"] }
|
teloxide = { version = "0.12.2", features = ["full"] }
|
||||||
|
tokio = { version = "1.38.0", features = ["full"] }
|
||||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -1,35 +1,22 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use teloxide::dispatching::HandlerExt;
|
||||||
|
|
||||||
mod database;
|
pub(crate) mod database;
|
||||||
mod telegram;
|
mod telegram;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let mut tg = telegram::connect();
|
// Logging setup
|
||||||
|
{
|
||||||
teloxide::repl(tg, |tg: teloxide::Bot, msg: teloxide::types::Message| async move {
|
pretty_env_logger::init();
|
||||||
use teloxide::prelude::*;
|
log::info!("Logging initialized successfully!");
|
||||||
|
}
|
||||||
let mut db = database::connect()
|
// Telegram setup
|
||||||
.expect("Failed to connect to the database");
|
{
|
||||||
|
log::trace!("Setting up Telegram bot...");
|
||||||
let whoami = {
|
let mut dispatcher = telegram::dispatcher();
|
||||||
use diesel::prelude::*;
|
dispatcher.dispatch().await
|
||||||
use database::schema::telegram::dsl::*;
|
}
|
||||||
use database::models::*;
|
|
||||||
telegram
|
|
||||||
.filter(telegram_id.eq(msg.chat.id.0))
|
|
||||||
.limit(1)
|
|
||||||
.select(TelegramUser::as_select())
|
|
||||||
.load(&mut db)
|
|
||||||
.expect("Failed to query")
|
|
||||||
.pop()
|
|
||||||
.expect("Failed to get object")
|
|
||||||
};
|
|
||||||
|
|
||||||
tg.send_message(msg.chat.id, format!("{whoami:?}")).await?;
|
|
||||||
Ok(())
|
|
||||||
}).await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
53
src/telegram/commands/mod.rs
Normal file
53
src/telegram/commands/mod.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
use anyhow::Error;
|
||||||
|
use diesel::PgConnection;
|
||||||
|
use teloxide::{Bot, dptree};
|
||||||
|
use teloxide::dispatching::{DefaultKey, Dispatcher, HandlerExt, UpdateFilterExt};
|
||||||
|
use teloxide::dispatching::dialogue::{InMemStorage, TraceStorage};
|
||||||
|
use teloxide::types::{Message, Update};
|
||||||
|
use teloxide::utils::command::BotCommands;
|
||||||
|
|
||||||
|
mod start;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
enum State {
|
||||||
|
#[default]
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Dialogue = teloxide::dispatching::dialogue::Dialogue<State, TraceStorage<InMemStorage<State>>>;
|
||||||
|
type Result = anyhow::Result<()>;
|
||||||
|
|
||||||
|
async fn detect_command(bot: Bot, dialogue: Dialogue, message: Message) -> Result {
|
||||||
|
let text = message.text();
|
||||||
|
if text.is_none() {
|
||||||
|
// Ignore non-textual messages
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
let text = text.unwrap();
|
||||||
|
|
||||||
|
match text {
|
||||||
|
"/start" => start::handler(bot, dialogue, message).await,
|
||||||
|
_ => anyhow::bail!("Unknown command"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn dispatcher(bot: Bot) -> Dispatcher<Bot, Error, DefaultKey> {
|
||||||
|
Dispatcher::builder(
|
||||||
|
bot,
|
||||||
|
Update::filter_message()
|
||||||
|
.enter_dialogue::<Message, TraceStorage<InMemStorage<State>>, State>()
|
||||||
|
.branch(
|
||||||
|
dptree::case![State::Default]
|
||||||
|
.endpoint(detect_command)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.dependencies(
|
||||||
|
dptree::deps![
|
||||||
|
TraceStorage::new(
|
||||||
|
InMemStorage::<State>::new()
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.enable_ctrlc_handler()
|
||||||
|
.build()
|
||||||
|
}
|
42
src/telegram/commands/start.rs
Normal file
42
src/telegram/commands/start.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
use anyhow::Context;
|
||||||
|
use teloxide::Bot;
|
||||||
|
use teloxide::payloads::SendMessageSetters;
|
||||||
|
use teloxide::requests::Requester;
|
||||||
|
use teloxide::types::{Message};
|
||||||
|
use super::{Dialogue, Result};
|
||||||
|
|
||||||
|
pub(super) async fn handler(bot: Bot, _dialogue: Dialogue, message: Message) -> Result {
|
||||||
|
let author = message.from()
|
||||||
|
.context("Failed to get the user who sent the original message")?;
|
||||||
|
|
||||||
|
let author_username = match author.username.as_ref() {
|
||||||
|
None => {
|
||||||
|
format!("{}", &author.first_name)
|
||||||
|
},
|
||||||
|
Some(username) => {
|
||||||
|
format!("@{}", &username)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let me = bot
|
||||||
|
.get_me()
|
||||||
|
.await
|
||||||
|
.context("Failed to get information about self")?;
|
||||||
|
|
||||||
|
let me_username = me.username.as_ref()
|
||||||
|
.context("Failed to get bot's username")?;
|
||||||
|
|
||||||
|
let text = format!(
|
||||||
|
"👋 Ciao {author_username}! Sono @{me_username}, il robot tuttofare della RYG!\n\n\
|
||||||
|
Puoi vedere l'elenco delle mie funzionalità dal menu in basso.\n\n\
|
||||||
|
Cosa posso fare per te oggi?"
|
||||||
|
);
|
||||||
|
|
||||||
|
let _reply = bot
|
||||||
|
.send_message(message.chat.id, text)
|
||||||
|
.reply_to_message_id(message.id)
|
||||||
|
.await
|
||||||
|
.context("Failed to send message")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1,7 +1,14 @@
|
||||||
|
use anyhow::Error;
|
||||||
use teloxide::Bot;
|
use teloxide::Bot;
|
||||||
|
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod commands;
|
||||||
|
|
||||||
pub fn connect() -> Bot {
|
pub fn dispatcher() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||||
Bot::new(config::TELEGRAM_BOT_TOKEN())
|
commands::dispatcher(
|
||||||
|
Bot::new(
|
||||||
|
config::TELEGRAM_BOT_TOKEN()
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue