GithubHelp home page GithubHelp logo

pirogoeth / telegram-bot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from telegram-rs/telegram-bot

0.0 2.0 0.0 56.66 MB

Rust Library for creating a Telegram Bot

License: MIT License

Shell 0.30% Rust 99.70%

telegram-bot's Introduction

Rust Telegram Bot Library

Build Status License Crates.io

Documentation: Latest crates.io version master

A library for writing your own Telegram bots. More information here. Official API here.

Example

Here is a simple example (see example/simple.rs):

extern crate futures;
extern crate telegram_bot;
extern crate tokio;

use std::env;

use futures::{Stream, future::lazy};

use telegram_bot::*;

fn main() {
    let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();
    runtime.block_on(lazy(|| {
        let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
        let api = Api::configure(token).build().unwrap();

        // Convert stream to the stream with errors in result
        let stream = api.stream().then(|mb_update| {
            let res: Result<Result<Update, Error>, ()> = Ok(mb_update);
            res
        });

        // Print update or error for each update.
        tokio::executor::current_thread::spawn(
            stream.for_each(move |update| {
                match update {
                    Ok(update) => {
                        // If the received update contains a new message...
                        if let UpdateKind::Message(message) = update.kind {
                            if let MessageKind::Text { ref data, .. } = message.kind {
                                // Print received text message to stdout.
                                println!("<{}>: {}", &message.from.first_name, data);

                                // Answer message with "Hi".
                                api.spawn(message.text_reply(
                                    format!("Hi, {}! You just wrote '{}'", &message.from.first_name, data)
                                ));
                            }
                        }
                    }
                    Err(_) => {}
                }

                Ok(())
            })
        );

        Ok::<_, ()>(())
    })).unwrap();

    runtime.run().unwrap();
}

You can find a bigger examples in the examples.

Usage

This library is available via crates.io. In order to use it, just add this to your Cargo.toml:

telegram-bot = "0.7"

Collaboration

Yes please! Every type of contribution is welcome: Create issues, hack some code or make suggestions. Don't know where to start? Good first issues are tagged with up for grab.

telegram-bot's People

Contributors

knsd avatar lukaskalbertodt avatar dns2utf8 avatar shouya avatar neuschaefer avatar rockneurotiko avatar white-oak avatar yuyoyuppe avatar

Watchers

Sean Johnson avatar James Cloos 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.