Socket
Socket
Sign inDemoInstall

@gramio/types

Package Overview
Dependencies
0
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @gramio/types

Code-generated and Auto-published Telegram Bot API types


Version published
Weekly downloads
12
decreased by-76.92%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Code-generated and Auto-published Telegram Bot API types

Versioning

7.1.x types are for 7.1 Telegram Bot API

Usage as an NPM package

import type { APIMethods, APIMethodReturn } from "@gramio/types";

type SendMessageReturn = Awaited<ReturnType<APIMethods["sendMessage"]>>;
//   ^? type SendMessageReturn = TelegramMessage

type GetMeReturn = APIMethodReturn<"getMe">;
//   ^? type GetMeReturn = TelegramUser

Please see API Types References

Auto-update package

This library is updated automatically to the latest version of the Telegram Bot API in case of changes thanks to CI CD! If the github action failed, there are no changes in the bot api

Imports (after @gramio/)

  • index - exports everything in the section
  • methods - exports APIMethods which describes the api functions
  • objects - exports objects with the Telegram prefix (for example Update)
  • params - exports params that are used in methods

Write you own type-safe Telegram Bot API wrapper

import type {
    APIMethods,
    APIMethodParams,
    TelegramAPIResponse,
} from "@gramio/types";

const TBA_BASE_URL = "https://api.telegram.org/bot";
const TOKEN = "";

const api = new Proxy({} as APIMethods, {
    get:
        <T extends keyof APIMethods>(_target: APIMethods, method: T) =>
        async (params: APIMethodParams<T>) => {
            const response = await fetch(`${TBA_BASE_URL}${TOKEN}/${method}`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(params),
            });

            const data = (await response.json()) as TelegramAPIResponse;
            if (!data.ok) throw new Error(`Some error occurred in ${method}`);

            return data.result;
        },
});

api.sendMessage({
    chat_id: 1,
    text: "message",
});
Usage with @gramio/keyboards
import { Keyboard } from "@gramio/keyboards";

// the code from the example above

api.sendMessage({
    chat_id: 1,
    text: "message with keyboard",
    reply_markup: new Keyboard().text("button text"),
});
With File uploading support

Documentation

Generate types manually

Prerequire - rust

  1. Clone this repo and open it
git clone https://github.com/gramiojs/types.git
  1. Clone repo with Telegram Bot API schema generator
git clone https://github.com/ark0f/tg-bot-api.git
  1. Run the JSON schema generator in the cloned folder
cd tg-bot-api && cargo run --package gh-pages-generator --bin gh-pages-generator -- dev && cd ..
  1. Run types code-generation from the root of the project
bun generate

or, if you don't use bun, use tsx

npx tsx src/index.ts
  1. Profit! Check out the types of Telegram Bot API in out folder!

Keywords

FAQs

Last updated on 04 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc