npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

telegram-bot-api-types

v7.11.0

Published

Telegram Bot API types

Downloads

675

Readme

telegram-bot-api-types

Telegram Bot API SDK compiled size is 0KB that only includes type definition files and JSDoc files. It can be used to conveniently develop Telegram Bots in TypeScript and JavaScript. You can wrap your API client with as many web request libraries as you want.

This is a d.ts and jsdoc file for Telegram Bot API. It is based on the official Telegram Bot API documentation.

Installation

npm i telegram-bot-api-types --save-dev # d.ts for TypeScript
npm i telegram-bot-api-jsdoc --save-dev # jsdoc for JavaScript

Example

You can use any HTTP request library you want to encapsulate your API client. Here is a simple echo bot.

import type * as Telegram from "telegram-bot-api-types"; // Import the types with namespace
import * as fs from 'node:fs';

class APIClientBase {
    // Implement your request method
    // you can use any http request library you want
    // You can go to `test` to see an example of the implementation
}

type APIClient = APIClientBase & Telegram.AllBotMethods;

// Use Proxy to automatically call the methods
export function createAPIClient(token: string): APIClient {
    const client = new APIClientBase(token);
    return new Proxy(client, {
        get(target, prop, receiver) {
            if (prop in target) {
                return Reflect.get(target, prop, receiver);
            }
            return (...args: any[]) => {
                if (typeof prop === 'string' && prop.endsWith('WithReturns')) {
                    const method = prop.slice(0, -11) as Telegram.BotMethod;
                    return Reflect.apply(target.requestJSON, target, [method, ...args]);
                }
                return Reflect.apply(target.request, target, [prop as Telegram.BotMethod, ...args]);
            };
        }
    }) as APIClient;
}

async function main() {
    const { token } = JSON.parse(fs.readFileSync('config.json', 'utf8'));
    const client = createAPIClient(token);
    const { result: user } = await client.getMeWithReturns()
    console.log(`Hello! My name is ${user.username}`);
    await client.deleteWebhook({});
    while (true) {
        const { result: updates } = await client.getUpdatesWithReturns({ offset: 0 });
        for (const update of updates) {
            if (update.message) {
                await client.sendMessageWithReturns({ chat_id: update.message.chat.id, text: update.message.text });
            }
        }
    }
}

main().catch(console.error);

You don't need to implement the methods one by one, you can use the Proxy object to create a client that automatically calls the methods. You can find the implementation of the APIClientBase class in the test folder.

If you want to use javascript, you can follow the example jsdoc.test.js

License

telegram-bot-api-types is released under the MIT license. See LICENSE for details.