이 라이브러리를 사용하면 Rust Language로 Telegram Bot을 쓸 수 있습니다. Telegram Bot API의 거의 완전한 래퍼이며 Hyper를 사용하여 Telegram 서버에 요청을 보냅니다. 각 전보 기능 호출은 실제 봇과 답을 가지고있는 미래를 반환합니다.
이것을 Cargo.toml 에 추가하십시오
[ dependencies ]
telebot = " 0.3.1 " 이 예제는 Telebot 라이브러리의 기본 사용을 보여줍니다. 간단한 "/reply"명령에 대한 새 핸들러를 생성하고 수신 된 텍스트를 응답합니다. Tokio Eventloop은 새로운 업데이트를 위해 200ms마다 투표하고 등록 된 이벤트와 일치시킵니다. 명령이 "/reply"와 일치하면 함수를 호출하고 반환 된 미래를 실행합니다.
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 ) ;
} 전자 예제는 하나의 핸들러와 오류 처리가 없으면 매우 간단했습니다. 자세한 설명과 그림을보고 싶다면 여기를 참조하십시오.
이 상자는 사용자 정의 파생물을 사용하여 Telegram API의 기능을 생성합니다. 따라서 각각의 완전한 함수는 functions.rs의 구조물로 설명되며 보충 상자 텔레 보트-공개는 완전한 시그니처를 생성합니다. 함수를 찾기 위해 구조물 서명을 사용할 수 있습니다. 예를 들어 SendLocation을 고려하십시오.
/// 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의 함수 이름을 정의합니다. 구조물의 각 선택 필드는 필드 이름으로 추가 기능을 호출하여 변경할 수 있습니다. 예를 들어, 파리의 위치를 알림없이 채팅 432432로 보내는 것 : bot.location(432432, 48.8566, 2.3522).disable_notification(true).send()
어느 쪽에도 라이센스가 부여되었습니다
귀하의 선택에.
귀하가 명시 적으로 명시 적으로 명시하지 않는 한, Apache-2.0 라이센스에 정의 된대로 귀하의 작업에 포함되도록 의도적으로 제출 된 기부금은 추가 이용 약관이나 조건없이 위와 같이 이중 라이센스를받습니다.