mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-21 18:44:19 +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]
|
||||
anyhow = "1.0.86"
|
||||
diesel = { version = "2.2.1", features = ["postgres"] }
|
||||
log = "0.4.22"
|
||||
micronfig = "0.3.0"
|
||||
teloxide = "0.12.2"
|
||||
tokio = { version = "1.38.0", features = ["full", "macros"] }
|
||||
pretty_env_logger = "0.5.0"
|
||||
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 teloxide::dispatching::HandlerExt;
|
||||
|
||||
mod database;
|
||||
pub(crate) mod database;
|
||||
mod telegram;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let mut tg = telegram::connect();
|
||||
|
||||
teloxide::repl(tg, |tg: teloxide::Bot, msg: teloxide::types::Message| async move {
|
||||
use teloxide::prelude::*;
|
||||
|
||||
let mut db = database::connect()
|
||||
.expect("Failed to connect to the database");
|
||||
|
||||
let whoami = {
|
||||
use diesel::prelude::*;
|
||||
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;
|
||||
// Logging setup
|
||||
{
|
||||
pretty_env_logger::init();
|
||||
log::info!("Logging initialized successfully!");
|
||||
}
|
||||
// Telegram setup
|
||||
{
|
||||
log::trace!("Setting up Telegram bot...");
|
||||
let mut dispatcher = telegram::dispatcher();
|
||||
dispatcher.dispatch().await
|
||||
}
|
||||
|
||||
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::dispatching::{DefaultKey, Dispatcher};
|
||||
|
||||
mod config;
|
||||
mod commands;
|
||||
|
||||
pub fn connect() -> Bot {
|
||||
Bot::new(config::TELEGRAM_BOT_TOKEN())
|
||||
pub fn dispatcher() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||
commands::dispatcher(
|
||||
Bot::new(
|
||||
config::TELEGRAM_BOT_TOKEN()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue