Skip to content

Типы Telegram Bot API

Автоматически сгенерированные и опубликованные типы Telegram Bot API

Справочник по типам API

Версионирование

Типы 7.7.x соответствуют Telegram Bot API 7.7

Использование в качестве пакета NPM

ts
import type { APIMethods, 
APIMethodReturn
} from "@gramio/types";
type
SendMessageReturn
=
Awaited
<
ReturnType
<APIMethods["sendMessage"]>>;
type
GetMeReturn
=
APIMethodReturn
<"getMe">;

Автоматическое обновление пакета

Эта библиотека автоматически обновляется до последней версии Telegram Bot API в случае изменений благодаря CI/CD! Если GitHub Action завершается с ошибкой, это означает, что в Bot API нет изменений.

Импорты

  • index - экспортирует всё в разделе
  • methods - экспортирует APIMethods, который описывает функции API
  • objects - экспортирует объекты с префиксом Telegram (например, Update)
  • params - экспортирует параметры, которые используются в methods

Создайте свою собственную обертку Telegram Bot API с проверкой типов

ts
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",
});

Использование с @gramio/keyboards

typescript
import { 
Keyboard
} from "@gramio/keyboards";
// код из примера выше
api
.
sendMessage
({
chat_id
: 1,
text
: "message with keyboard",
reply_markup
: new
Keyboard
().
text
("button text"),
});

С поддержкой загрузки файлов

См. files/usage-without-gramio

Генерация типов вручную

Требуется - rust

  1. Клонируйте этот репозиторий и откройте его
bash
git clone https://github.com/gramiojs/types.git
  1. Клонируйте репозиторий с генератором схемы Telegram Bot API
bash
git clone https://github.com/ark0f/tg-bot-api.git
  1. Запустите генератор схемы JSON в папке cloned
bash
cd tg-bot-api && cargo run --package gh-pages-generator --bin gh-pages-generator -- dev && cd ..
  1. Запустите генерацию кода типов из корня проекта
bash
bun generate

или, если вы не используете bun, используйте tsx

bash
npx tsx src/index.ts
  1. Готово! Проверьте типы Telegram Bot API в папке out!
type SendMessageReturn = TelegramMessage
type GetMeReturn = TelegramUser