Skip to content

Commit 7baeefd

Browse files
committed
docs: add Cheat Sheet page to navigation and create quick reference guide with common GramIO patterns
1 parent dd8ba56 commit 7baeefd

File tree

2 files changed

+310
-0
lines changed

2 files changed

+310
-0
lines changed

docs/.vitepress/config/locales/en.locale.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const localeEn = {
1111
nav: [
1212
{ text: "Home", link: "/" },
1313
{ text: "Get started", link: "/get-started" },
14+
{ text: "Cheat Sheet", link: "/cheat-sheet" },
1415
{ text: "Plugins", link: "/plugins/overview" },
1516
{ text: "Changelogs", link: "/changelogs/" },
1617
// { text: "Examples", link: "/markdown-examples" },
@@ -118,6 +119,7 @@ export const localeEn = {
118119
text: "Documentation",
119120
items: [
120121
{ text: "Get started", link: "/get-started" },
122+
{ text: "Cheat Sheet", link: "/cheat-sheet" },
121123
{ text: "Bot API", link: "/bot-api" },
122124
{
123125
text: "Updates",

docs/cheat-sheet.md

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
---
2+
title: GramIO Cheat Sheet - Quick Reference for Telegram Bot Development
3+
head:
4+
- - meta
5+
- name: "description"
6+
content: "Quick reference for GramIO — the TypeScript Telegram Bot API framework. Commands, triggers, keyboards, formatting, plugins, hooks and more in one place."
7+
- - meta
8+
- name: "keywords"
9+
content: "GramIO, cheat sheet, quick reference, telegram bot, TypeScript, commands, triggers, keyboards, hooks, plugins, formatting, sessions, files"
10+
---
11+
12+
# Cheat Sheet
13+
14+
Quick reference for the most common GramIO patterns. Click any heading to go to the full docs.
15+
16+
## [Setup](/get-started)
17+
18+
```bash
19+
npm create gramio my-bot
20+
```
21+
22+
```ts
23+
import { Bot } from "gramio";
24+
25+
const bot = new Bot(process.env.BOT_TOKEN as string);
26+
27+
bot.start();
28+
```
29+
30+
## [Commands](/triggers/command)
31+
32+
```ts
33+
bot.command("start", (ctx) => ctx.send("Hello!"));
34+
35+
// With arguments: /say hello world → ctx.args = "hello world"
36+
bot.command("say", (ctx) => ctx.send(ctx.args ?? "nothing"));
37+
```
38+
39+
## [Hears](/triggers/hears)
40+
41+
```ts
42+
// Exact string
43+
bot.hears("hi", (ctx) => ctx.send("Hey!"));
44+
45+
// Regex — ctx.args holds the match array
46+
bot.hears(/^hello (.+)/i, (ctx) => ctx.send(`Hi, ${ctx.args?.[1]}!`));
47+
48+
// Function
49+
bot.hears(
50+
(ctx) => ctx.text?.startsWith("?"),
51+
(ctx) => ctx.send("A question!"),
52+
);
53+
```
54+
55+
## [Listen to any update](/bot-api)
56+
57+
```ts
58+
bot.on("message", (ctx) => ctx.send("Got your message!"));
59+
60+
bot.on(["message", "edited_message"], (ctx) => ctx.send("New or edited!"));
61+
```
62+
63+
## [Inline Keyboard](/keyboards/inline-keyboard)
64+
65+
```ts
66+
import { InlineKeyboard, CallbackData } from "gramio";
67+
68+
// Simple text buttons
69+
const keyboard = new InlineKeyboard()
70+
.text("Yes ✅", "yes")
71+
.text("No ❌", "no")
72+
.row()
73+
.url("GitHub", "https://github.com/gramiojs/gramio");
74+
75+
ctx.send("Choose:", { reply_markup: keyboard });
76+
```
77+
78+
```ts
79+
// Type-safe callback data
80+
const actionData = new CallbackData("action").number("id");
81+
82+
ctx.send("Pick one:", {
83+
reply_markup: new InlineKeyboard()
84+
.text("Item 1", actionData.pack({ id: 1 }))
85+
.text("Item 2", actionData.pack({ id: 2 })),
86+
});
87+
```
88+
89+
## [Callback Query](/triggers/callback-query)
90+
91+
```ts
92+
// String
93+
bot.callbackQuery("yes", (ctx) => ctx.editText("You said yes!"));
94+
95+
// Type-safe CallbackData
96+
const actionData = new CallbackData("action").number("id");
97+
98+
bot.callbackQuery(actionData, (ctx) => {
99+
ctx.send(`You picked ID: ${ctx.queryData.id}`);
100+
// ^? number
101+
});
102+
```
103+
104+
## [Reply Keyboard](/keyboards/keyboard)
105+
106+
```ts
107+
import { Keyboard } from "gramio";
108+
109+
const keyboard = new Keyboard()
110+
.text("Option A")
111+
.text("Option B")
112+
.row()
113+
.requestLocation("Share location 📍")
114+
.resized();
115+
116+
ctx.send("Choose:", { reply_markup: keyboard });
117+
```
118+
119+
## [Remove Keyboard](/keyboards/remove-keyboard)
120+
121+
```ts
122+
import { RemoveKeyboard } from "gramio";
123+
124+
ctx.send("Keyboard removed.", { reply_markup: new RemoveKeyboard() });
125+
```
126+
127+
## [Format Messages](/formatting)
128+
129+
```ts
130+
import { format, bold, italic, link, code, pre, spoiler } from "gramio";
131+
132+
ctx.send(format`
133+
${bold`Hello!`} Welcome to ${link("GramIO", "https://gramio.dev")}.
134+
135+
Here is some ${italic`styled`} text and a ${spoiler`surprise`}.
136+
137+
${code("inline code")} or a block:
138+
${pre("const x = 1;", "typescript")}
139+
`);
140+
```
141+
142+
## [Send Files](/files/media-upload)
143+
144+
```ts
145+
import { MediaUpload } from "@gramio/files";
146+
147+
// From disk
148+
ctx.sendDocument(await MediaUpload.path("./report.pdf"));
149+
150+
// From URL
151+
ctx.sendPhoto(await MediaUpload.url("https://example.com/cat.png"));
152+
153+
// From Buffer
154+
ctx.sendDocument(await MediaUpload.buffer(buffer, "file.pdf"));
155+
156+
// By file_id (already uploaded to Telegram)
157+
ctx.sendPhoto("AgACAgIAAxk...");
158+
```
159+
160+
## [Session](/plugins/official/session)
161+
162+
```ts
163+
import { session } from "@gramio/session";
164+
165+
const bot = new Bot(process.env.BOT_TOKEN as string).extend(
166+
session({
167+
key: "session",
168+
initial: () => ({ count: 0 }),
169+
}),
170+
);
171+
172+
bot.command("count", (ctx) => {
173+
ctx.session.count++;
174+
// ^? { count: number }
175+
ctx.send(`Count: ${ctx.session.count}`);
176+
});
177+
```
178+
179+
## [Error Handling](/hooks/on-error)
180+
181+
```ts
182+
// Catch all errors
183+
bot.onError(({ context, kind, error }) => {
184+
console.error(kind, error.message);
185+
if (context.is("message")) context.send("Something went wrong.");
186+
});
187+
188+
// Only for specific update types
189+
bot.onError("message", ({ context, kind, error }) => {
190+
context.send(`${kind}: ${error.message}`);
191+
});
192+
193+
// Custom typed errors
194+
class NoRights extends Error {
195+
constructor(public role: "admin" | "moderator") {
196+
super();
197+
}
198+
}
199+
200+
const bot = new Bot(process.env.BOT_TOKEN as string)
201+
.error("NO_RIGHTS", NoRights)
202+
.onError(({ kind, error, context }) => {
203+
if (kind === "NO_RIGHTS" && context.is("message"))
204+
context.send(`You need the «${error.role}» role.`);
205+
});
206+
```
207+
208+
## [Hooks](/hooks/overview)
209+
210+
```ts
211+
bot.onStart((info) => console.log(`@${info.username} is running!`));
212+
213+
bot.onStop(() => console.log("Shutting down..."));
214+
215+
// Intercept every API request
216+
bot.preRequest((ctx) => {
217+
console.log("Calling", ctx.method);
218+
return ctx;
219+
});
220+
```
221+
222+
## [Use a Plugin](/plugins/overview)
223+
224+
```ts
225+
import { autoAnswerCallbackQuery } from "@gramio/auto-answer-callback-query";
226+
227+
bot.extend(autoAnswerCallbackQuery());
228+
```
229+
230+
## [Write a Plugin](/plugins/how-to-write)
231+
232+
```ts
233+
import { Plugin } from "gramio";
234+
235+
const myPlugin = new Plugin("my-plugin").derive("message", (ctx) => ({
236+
isAdmin: ctx.from?.id === 123456789,
237+
}));
238+
239+
bot.extend(myPlugin);
240+
241+
bot.on("message", (ctx) => {
242+
if (ctx.isAdmin) ctx.send("Hi boss!");
243+
// ^? boolean
244+
});
245+
```
246+
247+
## [Inline Query](/triggers/inline-query)
248+
249+
```ts
250+
bot.inlineQuery("cats", async (ctx) => {
251+
await ctx.answer(
252+
[
253+
ctx.buildInlineQueryResult.article({
254+
id: "1",
255+
title: "Cat fact",
256+
input_message_content: { message_text: "Cats purr at 25Hz." },
257+
}),
258+
],
259+
{ cache_time: 30 },
260+
);
261+
});
262+
```
263+
264+
## [Autoload handlers](/plugins/official/autoload)
265+
266+
```ts
267+
import { autoload } from "@gramio/autoload";
268+
269+
// Loads all files from ./src/commands/**/*.ts automatically
270+
const bot = new Bot(process.env.BOT_TOKEN as string).extend(autoload());
271+
```
272+
273+
```ts
274+
// src/commands/start.ts
275+
import type { Bot } from "gramio";
276+
277+
export default (bot: Bot) => bot.command("start", (ctx) => ctx.send("Hi!"));
278+
```
279+
280+
## [Scenes (conversations)](/plugins/official/scenes)
281+
282+
```ts
283+
import { Scene, Scenes } from "@gramio/scenes";
284+
285+
const loginScene = new Scene("login")
286+
.step("message", (ctx) => ctx.send("Enter your email:"))
287+
.step("message", (ctx) => {
288+
const email = ctx.text;
289+
return ctx.send(`Got it: ${email}`);
290+
});
291+
292+
const bot = new Bot(process.env.BOT_TOKEN as string).extend(
293+
new Scenes([loginScene]),
294+
);
295+
296+
bot.command("login", (ctx) => ctx.scene.enter("login"));
297+
```
298+
299+
## [Webhook](/updates/webhook)
300+
301+
```ts
302+
bot.start({
303+
webhook: {
304+
url: "https://example.com/bot",
305+
port: 8080,
306+
},
307+
});
308+
```

0 commit comments

Comments
 (0)