Code-generated Telegram Bot API types
import type { ApiMethods } from "@gramio/types"
type SendMessageReturn = ReturnType<ApiMethods["sendMessage"]>
Versioning
7.0.x types - for 7.0 Telegram Bot API
Write you own type-safe TBA API wrapper
import { stringify } from "node:querystring"
import type { ApiMethods } from "@gramio/types"
const TOKEN = ""
const api = new Proxy<ApiMethods>({} as ApiMethods, {
get: (_target, method: string) => async (args: Record<string, any>) => {
const url =
`http://api.telegram.org/bot` +
TOKEN +
"/" +
method +
`?` +
stringify(args)
const response = await fetch(url, {
method: "GET",
})
const data = await response.json()
if (!response.ok) throw new Error("some error")
return data.result
},
})
api.sendMessage({
chat_id: 1,
text: "msg",
})
Generate types
Prerequire - rust
- Clone this repo and open it
git clone https://github.com/kravetsone/gramio-types.git
- Clone repo with TBA parser to the
src
folder
git clone https://github.com/ark0f/tg-bot-api.git
- Run the JSON schema generator in the
cloned
folder
cargo run --package gh-pages-generator --bin gh-pages-generator -- dev
- 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
- Profit! Check out the types of Telegram Bot API in
types
folder!