内容介绍
本专题将带你使用axum实现一个 Webhook 形式的 Telegram 机器人。webhook
Telegram 机器人支持两种方式:轮询和 Webhook。为了节约资源我们将使用 Webhook 的方式开发 Telegram 机器人。处理文本消息
本章将开始与 Telegram 机器人进行交互。首先从最简单的文本消息开始。处理指令
本章我们将学习如何处理 Telegram 的“指令”(command)。开始之前,我们对之前的代码进行必要的封装。发送图片
本章继续完善我们的机器人。收到用户的`/logo`指令,我们需要把我们的 LOGO 图片发送给用户。让我们来看看如何让 Telegram 机器人发送图片信息。发送Markdown
Telegram 还支持 Markdown 和 HTML 类型的文本消息。本章我们将实现`/help`指令,它会将帮助信息以 Markdown 格式发送给用户。总结
本专题带你实现了一个简单的 Telegram 机器人。我们实现的功能是很简单的,其实 Telegram 支持多种消息
webhook
Telegram 机器人支持两种方式:轮询和 Webhook。为了节约资源我们将使用 Webhook 的方式开发 Telegram 机器人。
本章代码在01/webhook分支。
本章代码在01/webhook分支。
创建一个机器人
你可以通过官方文档获取帮助。
你可以通过官方文档获取帮助。
流程重现
下面重现一下创建机器人的流程。
以下内容中,>
开头的是你要输入的内容,<
开头的是 Telegram 返回的内容,【】
里的内容是本站提供的注释。
> /start
< I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual (https://core.telegram.org/bots).
You can control me by sending these commands:
/newbot - create a new bot
/mybots - edit your bots [beta]
...
> /newbot
< Alright, a new bot. How are we going to call it? Please choose a name for your bot.
> AXUM.RS 【你的机器人叫什么名称】
< Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
> axum_rs_bot 【你的机器人的用户名】
< Done! Congratulations on your new bot. You will find it at t.me/axum_rs_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
【此处就是你的TOKEN,请妥善保存】
Keep your token secure and store it safely, it can be used by anyone to control your bot.
开发 Webhook
一旦机器人接收到新消息,Telegram 会通过 Webhook 进行通知,我们要做的就是处理它的通知信息。它的通知方式为,以 POST 方式将 JSON 格式的消息发送到 Webhook。
假设,我们的 Webhook 是 https://tg.axum.rs/
,那么它是这样通知的:
为此,我们先从路由开始:
路由定义
路由非常简单,只需要定义一个接收POST
请求的处理器就好了。本例中的get(handler::index)
仅作演示,你可以只定义POST
而不处理GET
。
hander
pub async fn hook(Json(update): Json<Update>) -> String {
let msg = format!("recieved: {:?}", update);
tracing::debug!("{}", msg);
msg
}
pub async fn index() -> &'static str {
"A telegram bot from axum.rs"
}
我们只关注 hook()
。它将接收到的 JSON 数据反序列化为Update
结构体。
数据结构
#[derive(Deserialize, Debug)]
pub struct Update {
pub update_id: u64,
pub message: Message,
}
#[derive(Deserialize, Debug)]
pub struct Message {
pub message_id: u64,
}
可以看到,目前为止数据结构非常简单。
Update
:每次有新消息发送到机器人的时候,Telegram 就会向 Webhook 发送一个序列化为 JSON 的Update
对象
Message
:它包含了新消息的详细数据
注册 Webhook
使用 ngrok 内网穿透进行本地开发
Webhook 必须满足以下条件:
-
必须是公网可访问
-
必须是域名
-
必须是
HTTPS
协议
必须是公网可访问
必须是域名
必须是HTTPS
协议
很明显,如果我们在开发期间就去折腾这些非常低效,好在有ngrok,它可以实现内网穿透,将本地开发环境暴露给公网。
向 Telegram 注册 Webhook
现在可以向 Telegram 注册我们的 Webhook:
https://api.telegram.org/bot<你的TOKEN>/setWebhook?url=<你的Webhook>
假设 Token 是12345:ABCDE
,Webhook 是https://tg.axum.rs/
:
$ curl 'https://api.telegram.org/bot12345:ABCDE/setWebhook?url=https://tg.axum.rs/'
建议对你的 webhook 地址进行 Url 编码之后再提交。
建议对你的 webhook 地址进行 Url 编码之后再提交。
本章介绍了创建机器人、搭建最基本的 Webhook 以及注册 Webhook 的方法。下一章我们将开始与机器人进行交互。