#telegram-bot #bot-api #telegram #bot #api

rustelebot

一个Telegram机器人API,帮助你发送消息,或利用Telegram机器人API将你的应用程序集成

4个版本

0.3.2 2022年3月15日
0.3.1 2022年3月15日
0.2.0 2022年3月15日
0.1.2 2022年2月1日
0.1.0 2022年1月31日

1999Web编程

Download history 51/week @ 2024-04-07 57/week @ 2024-04-14 50/week @ 2024-04-21 43/week @ 2024-04-28 70/week @ 2024-05-05 28/week @ 2024-05-12 18/week @ 2024-05-19 24/week @ 2024-05-26 26/week @ 2024-06-02 6/week @ 2024-06-09 19/week @ 2024-06-16 23/week @ 2024-06-23 14/week @ 2024-06-30 34/week @ 2024-07-07 19/week @ 2024-07-14 40/week @ 2024-07-21

每月109次下载

MIT许可证

25KB
340

rustelebot

Rust的Telegram机器人API。目前它只支持使用sendMessage发送消息,没有完整的特性或输入字段。

该库的主要用途是与你的应用程序集成,以便快速将消息发送到Telegram机器人,前提是我们知道chat_id

APIs

  • create_instance - 创建一个包含Telegram机器人令牌和目标chat_id的机器人实例
  • send_message - 同步调用Telegram机器人的API sendMessage
  • send_message_async - 异步调用Telegram机器人的API sendMessage

示例

同步发送

简单方式发送

fn main() {
	let instance = rustelebot::create_instance("123456:123456", "-1000000");
	if let Err(_) = rustelebot::send_message(&instance, "Hello world", None) {
		// error handling here...
	}
}

MarkdownV2HTML样式发送消息

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