telegram-lib 0.1.0

telegram crate for simple use
Documentation
use std::error::Error;
use std::time;

use dptree::case;
use teloxide::dispatching::dialogue::GetChatId;
use teloxide::macros::BotCommands;
use teloxide::prelude::Requester;
use teloxide::types::{ChatId, Message};
use teloxide::Bot;

use crate::{message_handler, models};

fn validate_chat_id(_: ChatId) -> bool {
    return true;
}

type HandlerResult = Result<(), Box<dyn Error + Send + Sync>>;

async fn message_handler_ping(bot: Bot, message: Message) -> HandlerResult {
    if let Some(chat_id) = message.chat_id() {
        if validate_chat_id(chat_id) {
            bot.send_message(chat_id, "pong").await.map(|_| ())?;
        }
    }
    Ok(())
}

#[derive(Debug, BotCommands, Clone)]
#[command(
    rename_rule = "lowercase",
    parse_with = "split",
    description = "supported commands:"
)]
enum Command {
    #[command(description = "start to use me")]
    Start,
    #[command(description = "send a greet")]
    Greet,
    #[command(description = "show time")]
    Time,
}

async fn command_handler_start(bot: Bot, message: Message) -> HandlerResult {
    if let Some(chat_id) = message.chat_id() {
        if validate_chat_id(chat_id) {
            bot.send_message(chat_id, "nice to meet you")
                .await
                .map(|_| ())?;
        }
    }

    Ok(())
}

async fn command_handler_greet(bot: Bot, message: Message) -> HandlerResult {
    if let Some(chat_id) = message.chat_id() {
        if validate_chat_id(chat_id) {
            bot.send_message(chat_id, "hello").await.map(|_| ())?;
        }
    }

    Ok(())
}

async fn command_handler_time(bot: Bot, message: Message) -> HandlerResult {
    if let Some(chat_id) = message.chat_id() {
        if validate_chat_id(chat_id) {
            let now = time::SystemTime::now();
            bot.send_message(chat_id, format!("{:?}", now))
                .await
                .map(|_| ())?;
        }
    }

    Ok(())
}

#[test]
fn test_new_bot() {
    let token = std::env::var("TELEGRAM_BOT_KEY").unwrap();

    // command handler 注入业务逻辑
    let mut command_handler = message_handler::Handler::new();
    command_handler =
        command_handler.add_handler(case![Command::Start].endpoint(command_handler_start));
    command_handler =
        command_handler.add_handler(case![Command::Greet].endpoint(command_handler_greet));
    command_handler =
        command_handler.add_handler(case![Command::Time].endpoint(command_handler_time));
    let command = command_handler.build_command::<Command>();

    // text handler 注入业务逻辑
    let mut text_handler = message_handler::Handler::new();
    text_handler = text_handler.add_handler(
        dptree::filter(|message: Message| message.text() == Some("ping"))
            .endpoint(message_handler_ping),
    );
    let text = text_handler.build_message();

    // build 最终的 message handler
    let mut message_handler = message_handler::Handler::new();
    message_handler = message_handler.add_handler(command);
    message_handler = message_handler.add_handler(text);
    let message = message_handler.build_message();

    // 初始化 telegram bot
    let mut tg = models::Telegram::new(&token);
    tg = tg.on_message(message);

    // run bot
    let r = tokio::runtime::Runtime::new().unwrap();
    r.block_on(tg.run());
}