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

rustelebot

A telegram bot API which help you send message, or utilize telegram bot API integrateable with your application

4 releases

0.3.2 Mar 15, 2022
0.3.1 Mar 15, 2022
0.2.0 Mar 15, 2022
0.1.2 Feb 1, 2022
0.1.0 Jan 31, 2022

#38 in #bot-api

Download history 36/week @ 2024-07-21 56/week @ 2024-07-28 17/week @ 2024-08-04 60/week @ 2024-08-11 30/week @ 2024-08-18 76/week @ 2024-08-25 121/week @ 2024-09-01 78/week @ 2024-09-08 22/week @ 2024-09-15 85/week @ 2024-09-22 57/week @ 2024-09-29 46/week @ 2024-10-06 57/week @ 2024-10-13 61/week @ 2024-10-20 59/week @ 2024-10-27 67/week @ 2024-11-03

247 downloads per month

MIT license

25KB
340 lines

rustelebot

Telegram Bot API in Rust. Right now it supports only send message with sendMessage with no exhuastive features or input fields of such API.

Main use case of this library is to integrate it with your application to quickly send message to telegram bot, in turn send message to telegram chat group or telegram channel group provided that we know chat_id.

APIs

  • create_instance - create a bot instance consistsing of Telegram's bot token, and target chat_id
  • send_message - call Telegram bot's API sendMessage to send message synchronously
  • send_message_async - call Telegram bot's API sendMessage to send message asynchronously

Example

Send Synchronously

Send in simple way

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

Send in MarkdownV2 or HTML style message

Send message in 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...
	}
}

Send messsage in 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...
	}
}

Send Asynchronously

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));
}

Similarly we can provide SendMessageOption as seen in synchronized way. Functionalities are equal.

Tests

You can test by define the following two environment variables

  • RUSTELEBOT_BOT_TOKEN - telegram bot's token
  • RUSTELEBOT_CHAT_ID - telegram bot's chat id

then execute

cargo test

some tests will send a single, or multiple messages to specified chat id on behalf of such telegram bot. Please take a look at src/tests.rs.

Note

You can utilize this telegram bot @username_to_id_bot in order to get your telegram channel's chat_id.

License

MIT, Wasin Thonkaew

Dependencies

~14–23MB
~355K SLoC