Coder Social home page Coder Social logo

telezig's Introduction

telezig

A telegram bot library written in Zig ⚡

License

Installation

Install telezig using gyro

gyro add --src github axgdev/telezig

Usage

Echo bot example:

  • Load the bot token to pass it as a string to the library
fn getToken(token_path: []const u8, buffer: []u8) !void {
    const file = try std.fs.cwd().openFile(token_path, .{ .mode = .read_only });
    defer file.close();
    _ = try file.reader().read(buffer);
}
  • Example of a echo bot loop
// Windows-only startup, this might not be needed in the future if fixed, see: https://github.com/ziglang/zig/issues/8943
if (builtin.os.tag == std.Target.Os.Tag.windows) _ = try std.os.windows.WSAStartup(2, 0);

var allocator = std.testing.allocator;
var token: [46]u8 = undefined;
// Here we call the function above to load the token from a file,
// but you can decide to load it from an environment variable or any other way
try getToken("token.txt", token[0..]);
var bot = try Telezig.init(allocator, token[0..]);

var update_id: i64 = std.math.minInt(i64);
var sleep_seconds: u64 = 10;
// Here we run an infinite loop to get messages written to the bot and respond with the same text.
// The only way to stop it is to kill the app
while (true) {
    // Sleep some seconds on each loop to not make too many requests to the Telegram API
    std.time.sleep(sleep_seconds * std.time.ns_per_s);
    
    // Get the updates from the Telegram API
    var update = try bot.getUpdates();
    defer bot.allocator.free(update.text);

    // We only send an echo message if someone has sent any new message to the bot
    var new_update_id = update.update_id;
    if (update_id == new_update_id) continue;
    update_id = new_update_id;

    // Send the same message we received
    try bot.sendMessage(update);
}

// Windows-only cleanup, this might not be needed in the future if fixed, see: https://github.com/ziglang/zig/issues/8943
if (builtin.os.tag == std.Target.Os.Tag.windows) try std.os.windows.WSACleanup();

Dependencies

License

telezig is released under the MIT License. 🎉🍻

telezig's People

Contributors

axgdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

0xonebeing

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.