• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TypeScript types for the entire Telegram Bot API

Types for the Telegram Bot API

Please consider contributing to @grammyjs/types instead. typegram is legacy and will not be updated directly anymore. Instead, @grammyjs/types is maintained and kept in sync with the Bot API specification. Changes are backported to typegram periodically to keep older projects running.

This project provides TypeScript types for the entire Telegram Bot API in version 6.8.

It contains zero bytes of executable code.

Installation

npm install --save-dev typegram

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:

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

(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 typegram 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 typegram 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.)

typegram cannot possibly 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 typegram to fit your needs by passing your custom InputFile to the ApiMethods type.

import * as Typegram from "typegram";

type API = Typegram.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> = Typegram.Opts<MyInputFile>[M];
type Ret<M extends keyof API> = Typegram.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 = Typegram.InputMedia<MyInputFile>;
type InputMediaPhoto = Typegram.InputMediaPhoto<MyInputFile>;
type InputMediaVideo = Typegram.InputMediaVideo<MyInputFile>;
type InputMediaAnimation = Typegram.InputMediaAnimation<MyInputFile>;
type InputMediaAudio = Typegram.InputMediaAudio<MyInputFile>;
type InputMediaDocument = Typegram.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 from typegram.

More Repositories

1

cloudconvert-bot

Telegram bot to do file conversions.
TypeScript
22
star
2

oson

oson structured object notation
TypeScript
17
star
3

link-preview-bot

Telegram bot to generate link previews for all links in messages.
TypeScript
15
star
4

subscription-manager-bot

Telegram Bot to restrict group members to paying customers
TypeScript
8
star
5

cattpbot

TypeScript
7
star
6

winding

Cross-platform window manager for Deno
TypeScript
6
star
7

strom

The ultimate streaming library for Deno
TypeScript
6
star
8

sitemap-cli

Deno CLI to create a sitemap from build output
TypeScript
4
star
9

telegraf-session-firestore

Google Firestore based session middleware for telegraf.js
TypeScript
4
star
10

super-octo-chainsaw

money
TypeScript
3
star
11

fort-prog-project

Haskell
2
star
12

skaffe

Building block for hybrid Deno/Node scaffolding tools
TypeScript
2
star
13

knorpelsenf.me

TypeScript
2
star
14

alphamoji-bot

JavaScript
2
star
15

ohnaobot

oh no
TypeScript
2
star
16

ltexbot

Render LaTeX to images right inside Telegram
TypeScript
2
star
17

tsglobal24

Snapshots of the live coding session at TS Global Summit 2024
TypeScript
1
star
18

tdt-17-mini-project

Python
1
star
19

curryware

Experimental functional middleware system
TypeScript
1
star
20

bladestitching

Source code and more for my bachelor thesis
Python
1
star
21

withings-deno

Withings API Client for Deno
TypeScript
1
star
22

audazzle-bot

Telegram bot that turns Telegram into a full-blown online music player for your entire library.
1
star
23

ipa-bot

Repo for https://t.me/ipabot
JavaScript
1
star
24

image-labelling-bot

A Telegram Bot helping you sort your images
TypeScript
1
star
25

jaha

Ja-Ja-Jaha MX
HTML
1
star