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

@telegraf/types

v7.1.0

Published

Type declarations for the Telegram API

Downloads

255,652

Readme

Types for the Telegram Bot API Deno shield

Bot API Version NPM version

This project is a fork of @KnorpelSenf/typegram, specialised for Telegraf. Typegram is legacy, and now backported from @grammyjs/types.

This fork keeps Telegram Bot API types updated for Telegraf. This project provides TypeScript types for the entire Telegram Bot API.

It contains zero bytes of executable code.

Installation

npm install --save-dev @telegraf/types

Available Types

Generally, this package just exposes a huge load of interfaces that correspond to the types used throughout the Telegram Bot API.

Note that the API specification sometimes only has one name for multiple variants of a type, e.g. there are a number of different Updates you can receive, but they're all just called Update. This package represents such types as large unions of all possible options of what an Update could be, such that type narrowing can work as expected on your side. If you need to access the individual variants of an Update, refer to Update.MessageUpdate and its siblings.

In fact, this pattern is used for various types, namely:

  • Update
  • Message
  • CallbackQuery
  • Chat
  • ChatFromGetChat
  • InlineKeyboardButton
  • KeyboardButton
  • MessageEntity
  • Location

(Naturally, when the API specification is actually modelling types to be unions (e.g. InlineQueryResult), this is reflected here as a union type, too.)

Using API Response objects

The Telegram Bot API does not return just the requested data in the body of the response objects.

Instead, they are wrapped inside an object that has an ok: boolean status flag, indicating success or failure of the preceding API request. This outer object is modelled in @telegraf/types by the ApiResponse type.

Customizing InputFile and accessing API methods

The Telegram Bot API lets bots send files in three different ways. Two of those ways are by specifying a string—either a file_id or a URL. The third option, however, is by uploading files to the server using multipart/form-data.

The first two means to send a file are already covered by the type annotations across the library. In all places where a file_id or a URL is permitted, the corresponding property allows a string.

We will now look at the type declarations that are relevant for uploading files directly. Depending on the code you're using the types for, you may want to support different ways to specify the file to be uploaded. As an example, you may want to be able to make calls to sendDocument with an object that conforms to { path: string } in order to specify the location of a local file. (Your code is then assumed to able to translate calls to sendDocument and the like to multipart/form-data uploads when supplied with an object alike { path: '/tmp/file.txt' } in the document property of the argument object.)

This library cannot automatically know what objects you want to support as InputFiles.

However, you can specify your own version of what an InputFile is throughout all affected methods and interfaces.

For instance, let's stick with our example and say that you want to support InputFiles of the following type.

interface MyInputFile {
  path: string;
}

You can then customize the types to fit your needs by passing your custom InputFile to the ApiMethods type.

import * as Telegram from "@telegraf/types";

type API = Telegram.ApiMethods<MyInputFile>;

You can now access all types that must respect MyInputFile through the API type:

// The utility types `Opts` and `Ret`:
type Opts<M extends keyof API> = Telegram.Opts<MyInputFile>[M];
type Ret<M extends keyof API> = Telegram.Ret<MyInputFile>[M];

Each method takes just a single argument with a structure that corresponds to the object expected by Telegram. If you need to directly access that type, consider using Opts<M> where M is the method name (e.g. Opts<'getMe'>).

Each method returns the object that is specified by Telegram. If you directly need to access the return type of a method, consider using Ret<M> where M is the method name (e.g. Opts<'getMe'>).

// The adjusted `InputMedia*` types:
type InputMedia = Telegram.InputMedia<MyInputFile>;
type InputMediaPhoto = Telegram.InputMediaPhoto<MyInputFile>;
type InputMediaVideo = Telegram.InputMediaVideo<MyInputFile>;
type InputMediaAnimation = Telegram.InputMediaAnimation<MyInputFile>;
type InputMediaAudio = Telegram.InputMediaAudio<MyInputFile>;
type InputMediaDocument = Telegram.InputMediaDocument<MyInputFile>;

Note that interfaces other than the ones mentioned above are unaffected by the customization through MyInputFile. They can simply continue to be imported directly.

Development

This project is written for Deno and built for Node. Running npm prepare runs the deno2node script to build for Node.

Where do the types come from

They're handwritten. Typegram was started by @KnorpelSenf, who eventually used it as a starting point for the grammY types package. @telegraf/types is based on both packages, and regularly syncs with them and the Bot API.