4个版本
0.3.2 | 2022年3月15日 |
---|---|
0.3.1 | 2022年3月15日 |
0.2.0 | 2022年3月15日 |
0.1.2 |
|
0.1.0 |
|
1999 在 Web编程 中
每月109次下载
25KB
340 行
rustelebot
Rust的Telegram机器人API。目前它只支持使用sendMessage
发送消息,没有完整的特性或输入字段。
该库的主要用途是与你的应用程序集成,以便快速将消息发送到Telegram机器人,前提是我们知道chat_id
。
APIs
create_instance
- 创建一个包含Telegram机器人令牌和目标chat_id
的机器人实例send_message
- 同步调用Telegram机器人的APIsendMessage
send_message_async
- 异步调用Telegram机器人的APIsendMessage
示例
同步发送
简单方式发送
fn main() {
let instance = rustelebot::create_instance("123456:123456", "-1000000");
if let Err(_) = rustelebot::send_message(&instance, "Hello world", None) {
// error handling here...
}
}
以MarkdownV2
或HTML
样式发送消息
以MarkdownV2
发送消息
use rustelebot::types::{SendMessageOption, SendMessageParseMode};
fn main() {
let instance = rustelebot::create_instance("123456:123456", "-1000000");
let option = SendMessageOption { parse_mode: Some(SendMessageParseMode::MarkdownV2) };
// note on two spaces at the end of the line for a new line in markdown
if let Err(_) = rustelebot::send_message(&instance,
r#"__Hello world__
`Tap to copy this text`
Visit my [website](https://wasin.io)"#, Some(option)) {
// error handling here...
}
}
以HTML
发送消息
use rustelebot::types::{SendMessageOption, SendMessageParseMode};
fn main() {
let instance = rustelebot::create_instance("123456:123456", "-1000000");
let option = SendMessageOption { parse_mode: Some(SendMessageParseMode::HTML) };
if let Err(_) = rustelebot::send_message(&instance,
r#"<u>Hello world</u>
<code>Tap to copy this text</code>
Visit my <a href="https://wasin.io">website</a>"#, Some(option)) {
// error handling here...
}
}
异步发送
fn main() {
let instance = rustelebot::create_instance("123456:123456", "-1000000");
async fn async_fn(instance: &BotInstance) {
let f1 = rustelebot::send_message_async(&instance, "Msg1", None);
let f2 = rustelebot::send_message_async(&instance, "Msg2", None);
let f3 = rustelebot::send_message_async(&instance, "Msg3", None);
let f4 = rustelebot::send_message_async(&instance, "Msg4", None);
// wait for all futures
// this doesn't not guarantee order
futures::join!(f1, f2, f3, f4);
}
// block on the current thread for the whole async (futures) to complete
futures::executor::block_on(async_fn(&instance));
}
我们可以像同步方式一样提供SendMessageOption
。功能相同。
测试
你可以定义以下两个环境变量进行测试
RUSTELEBOT_BOT_TOKEN
- Telegram机器人的令牌RUSTELEBOT_CHAT_ID
- Telegram机器人的chat id
然后执行
cargo测试
一些测试将代表该Telegram机器人向指定的chat id发送单个或多个消息。请参阅src/tests.rs
。
注意
你可以使用此Telegram机器人@username_to_id_bot
来获取你的Telegram频道的chat_id
。
许可证
MIT,Wasin Thonkaew
依赖关系
~12–21MB
~346K SLoC