telebot
1.0.0
該庫允許您用Rust語言編寫電報機器人。這是電報機器人API的幾乎完整的包裝器,並使用Hyper將請求發送到Telegram服務器。每個電報函數呼叫都返回一個未來,該未來帶有實際的機器人和答案。
將其添加到您的Cargo.toml
[ dependencies ]
telebot = " 0.3.1 " 此示例顯示了Telebot庫的基本用法。它為簡單的“/回复”命令創建了一個新的處理程序,並回復了收到的文本。 Tokio Eventloop每200ms進行每200ms進行一次調查,並將其與註冊事件進行匹配。如果命令與“/回复”匹配,它將調用該功能並執行返回的未來。
use telebot :: Bot ;
use futures :: stream :: Stream ;
use std :: env ;
// import all available functions
use telebot :: functions :: * ;
fn main ( ) {
// Create the bot
let mut bot = Bot :: new ( & env :: var ( "TELEGRAM_BOT_KEY" ) . unwrap ( ) ) . update_interval ( 200 ) ;
// Register a reply command which answers a message
let handle = bot . new_cmd ( "/reply" )
. and_then ( | ( bot , msg ) | {
let mut text = msg . text . unwrap ( ) . clone ( ) ;
if text . is_empty ( ) {
text = "<empty>" . into ( ) ;
}
bot . message ( msg . chat . id , text ) . send ( )
} )
. for_each ( |_| Ok ( ( ) ) ) ;
bot . run_with ( handle ) ;
} 前一個示例非常簡單,只有一個處理程序,沒有錯誤處理。如果您想查看進一步的解釋和說明,請參閱此處。
該板條箱使用自定義派生來生成電報API的功能。因此,每個完整的函數都用函數中的結構來描述。 RS和補充板條箱Telebot-derive生成完整的簽名。為了找到一個函數,可以使用結構簽名。例如,考慮發送定位:
/// Use this method to send point on the map. On success, the sent Message is returned.
# [ derive ( TelegramFunction , Serialize ) ]
# [ call = "sendLocation" ]
# [ answer = "Message" ]
# [ function = "location" ]
pub struct SendLocation {
chat_id : u32 ,
latitude : f32 ,
longitude : f32 ,
# [ serde ( skip_serializing_if= "Option::is_none" ) ]
disable_notification : Option < bool > ,
# [ serde ( skip_serializing_if= "Option::is_none" ) ]
reply_to_message_id : Option < u32 > ,
# [ serde ( skip_serializing_if= "Option::is_none" ) ]
reply_markup : Option < NotImplemented >
}字段“函數”定義了本地API中函數的名稱。可以通過調用帶有字段名稱的附加功能來更改結構中的每個可選字段。 bot.location(432432, 48.8566, 2.3522).disable_notification(true).send()
根據任何一個
可以選擇。
除非您另有明確說明,否則任何有意提交的捐款(如Apache-2.0許可證中定義)應為雙重許可,如上所述,沒有任何其他條款或條件。