Git Product home page Git Product logo

telebot's Introduction

Telebot - Telegram Bot Library for Rust

Travis Build Status License MIT Crates.io doc.rs

This library allows you to write a Telegram Bot in the Rust language. It's an almost complete wrapper for the Telegram Bot API and uses hyper to send requests to the Telegram server. Each Telegram function call returns a future which carries the actual bot and the answer.

Usage

Add this to your Cargo.toml

[dependencies]
telebot = "0.3.0"

You need a nightly version of the Rust compiler, because procedural macros are not yet supported in the stable channel.

How it works

This example shows the basic usage of the telebot library. It creates a new handler for a simple "/reply" command and replies the received text. The tokio eventloop polls every 200ms for new updates and matches them with the registered events. If the command matches with "/reply" it will call the function and execute the returned future.

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

Additional example

The former example was very simple with just one handler and no error handling. If you want to see a further explained and illustrated one, please see here.

Find a Telegram function in the source code

This crate uses custom derive to generate functions of the Telegram API. Therefore each complete function is described with a struct in functions.rs and the supplemental crate telebot-derive generates the complete signature. In order to find a function, the struct signature can be used. For example consider 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>
}

The field "function" defines the name of the function in the local API. Each optional field in the struct can be changed by calling an additional function with the name of the field. So for example to send the location of Paris to chat 432432 without notification: bot.location(432432, 48.8566, 2.3522).disable_notification(true).send()

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

telebot's People

Contributors

angelsl avatar asonix avatar bemyak avatar berkus avatar bytesnake avatar cirfi avatar dylan-dpc avatar eroc33 avatar gowee avatar gugahoa avatar iovxw avatar jeizsm avatar juanpotato avatar l4l avatar lexfro avatar paullgdc avatar ratijas avatar redl0tus avatar tecywiz121 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.