本章我们将学习如何处理 Telegram 的“指令”(command)。开始之前,我们对之前的代码进行必要的封装。

本章代码在03/处理指令分支。

封装错误类型

先从封装自定义错误开始:

// src/error.rs

#[derive(Debug)]
pub enum AppErrorType {
    HttpError,
    SerdeError,
}
#[derive(Debug)]
pub struct AppError {
    pub message: Option<String>,
    pub cause: Option<String>,
    pub error_type: AppErrorType,
}

// impl AppError ...

接着定义自己的Result

没什么需要多说的,我们每个专题都要这些操作。

封装 Telegram API 请求

现在我们要将发送请求到 Telegram API 的部分封装成一个函数:

// src/bot.rs

async fn invoke_api<T: Serialize>(data: &T, method: &str, token: &str) -> Result<Response> {
    let api_addr = format!("https://api.telegram.org/bot{}/{}", token, method);
    let res = reqwest::Client::new()
        .post(&api_addr)
        .form(data)
        .send()
        .await
        .map_err(AppError::from)?
        .text()
        .await
        .map_err(AppError::from)?;
    let res = serde_json::from_str(&res).map_err(AppError::from)?;
    Ok(res)
}

pub async fn send_text_message(token: &str, chat_id: u64, text: String) -> Result<Response> {
    let data = request::TextMessage { chat_id, text };
    invoke_api(&data, "sendMessage", token).await
}

封装调用 API 后,Telegram 返回的响应

#[derive(Deserialize, Debug)]
pub struct Response {
    pub ok: bool,
    pub result: Option<Message>,
}

为可选字段加上Option<>

#[derive(Deserialize, Debug)]
pub struct Message {
    pub message_id: u64,
    pub from: Option<User>,
    pub chat: Chat,
    pub date: u64,
    pub text: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct User {
    pub id: u64,
    pub is_bot: bool,
    pub first_name: Option<String>,
    pub username: Option<String>,
    pub language_code: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct Chat {
    pub id: u64,
    pub first_name: Option<String>,
    pub username: Option<String>,
    #[serde(rename(deserialize = "type"))]
    pub types: String,
}

Telegram API 中,每个数据结构中的必填项并不多,所以我们需要为那些可选字段加上Option<>,以避免发生无法解析的错误。

关于 Telegram API 中各数据结构是否必填,请参见官方文档。文档中,所有标有Optional的,均为可选的。

处理指令

指令的格式

在 Telegram 中,指令都是以/开头,后面是有效的标识符,比如:/start/help 等。如内容介绍部分所说,我们最终要实现三个指令:

  • /website:访问 axum 中文网官方

  • /help:显示帮助信息

本章将实现第一个指令:/website

/website 指令

指令其实也是用户发送过来的文本消息,所以我们只要判断用户发送的文本消息是不是/website即可。

识别出指令

handler::hook()中,我们需要对用户输入的内容进行处理:

pub async fn hook(
    Json(payload): Json<Update>,
    Extension(state): Extension<AppState>,
) -> Result<String> {
    // ..

    let msg_text = payload.message.text.unwrap_or("".to_string());

    let reply_msg = match msg_text.as_str() {
        "/website" => command::website(),
        _ => echo(msg_text),
    };

    let res = bot::send_text_message(&state.bot.token, payload.message.chat.id, reply_msg)
        .await
        .map_err(log_error(String::from("bot_send_text_message")))?;

    let result = format!("{:?}", res);
    tracing::debug!("sendMessage: {}", &result);
    Ok(result)
}

然后将内容通过 Telegram API 发送给用户。

command::website()很简单,只是返回我们的网站的地址:

pub fn website() -> String {
    "https://axum.rs".to_string()
}