Code Monkey home page Code Monkey logo

tgbotapi-rs's Introduction

tgbotapi

An async Rust library for working with the Telegram Bot API.

This project is not yet complete but provides most of the core experience.

It makes no assumptions about how you are using the API and only provides types and wrappers around the Bot API.

It defines a common trait, TelegramRequest that all requests implement. This trait supports dynamic endpoints, arbitrary values (as long as they can be serialized into JSON with serde), and file uploads (including multiple files).

Examples

use tgbotapi::{Telegram, requests::{ChatID, GetUpdates, SendMessage}};

// Create a new Telegram instance
let telegram = Telegram::new("api_token".into());

// Create a request to get updates with long-polling enabled
let mut get_updates = GetUpdates {
    timeout: Some(30),
    ..Default::default()
};

// Loop forever getting new updates
loop {
    // Ask Telegram for new updates
    for update in telegram.make_request(&get_updates).await? {
        // Increment the offset to tell Telegram we processed the update
        get_updates.offset = Some(update.update_id + 1);

        // Ignore all non-message updates
        let message = match update.message {
            Some(message) => message,
            _ => continue,
        };

        // Create a request to send a message
        let send_message = SendMessage {
            chat_id: message.chat_id(),
            text: "Hello, world!".into(),
            ..Default::default()
        };

        // Send the message
        telegram.make_request(&send_message).await?;
    }
}

For higher-performance bots you should use webhooks.

use tgbotapi::Update;

// Get the body of the request from your web handler
let body = get_request_body!();
let update: Update = serde_json::from_slice(&body)?;

// Now we can do something with the update

tgbotapi-rs's People

Contributors

syfaro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.