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许可证中定义)应为双重许可,如上所述,没有任何其他条款或条件。