ห้องสมุดนี้ช่วยให้คุณสามารถเขียนบอทโทรเลขในภาษาสนิม มันเป็น wrapper เกือบสมบูรณ์สำหรับ Telegram Bot API และใช้ Hyper เพื่อส่งคำขอไปยังเซิร์ฟเวอร์โทรเลข การโทรโทรเลขแต่ละรายการจะส่งคืนอนาคตซึ่งมีบอทจริงและคำตอบ
เพิ่มสิ่งนี้ลงใน Cargo.toml ของคุณ
[ dependencies ]
telebot = " 0.3.1 " ตัวอย่างนี้แสดงการใช้งานขั้นพื้นฐานของไลบรารี Telebot มันสร้างตัวจัดการใหม่สำหรับคำสั่ง "ตอบกลับ" อย่างง่ายและตอบกลับข้อความที่ได้รับ Tokio eventloop สำรวจทุก ๆ 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 ) ;
} ตัวอย่างเดิมนั้นง่ายมากโดยมีเพียงตัวจัดการเพียงตัวเดียวและไม่มีการจัดการข้อผิดพลาด หากคุณต้องการเห็นคำอธิบายเพิ่มเติมและแสดงให้เห็นโปรดดูที่นี่
ลังนี้ใช้การกำหนดเองเพื่อสร้างฟังก์ชั่นของ Telegram API ดังนั้นแต่ละฟังก์ชั่นที่สมบูรณ์จะถูกอธิบายด้วย struct ในฟังก์ชัน RS และ Crate Telebot-derive เสริมสร้างลายเซ็นที่สมบูรณ์ เพื่อค้นหาฟังก์ชั่นสามารถใช้ลายเซ็นโครงสร้างได้ ตัวอย่างเช่นพิจารณา 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 จะได้รับใบอนุญาตคู่ดังกล่าวข้างต้นโดยไม่มีข้อกำหนดหรือเงื่อนไขเพิ่มเติมใด ๆ