Skip to content

Commit c57294c

Browse files
committed
More Walkthrough sections
1 parent 43b394a commit c57294c

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

src/chat-actions.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Chat Actions
3+
parent: /#walkthrough
4+
walkthrough:
5+
track: main
6+
order: 17
7+
---
8+
9+
Clients can show actions such as typing or uploading while preparing a response.
10+
11+
## Showing an Action
12+
13+
Use {{ "sendChatAction" |> m }} before starting the work.
14+
15+
```ts
16+
client.command("status", async (ctx) => {
17+
await ctx.sendChatAction({ type: "typing" });
18+
19+
const status = await getStatus();
20+
await ctx.reply(status);
21+
});
22+
```
23+
24+
Chat actions remain visible for a short time. Send the action again if the work takes longer.
25+
26+
## Reporting Upload Progress
27+
28+
Upload actions can include a progress percentage.
29+
30+
```ts
31+
await client.sendChatAction(chatId, {
32+
type: "uploadingDocument",
33+
progress: 50,
34+
});
35+
```
36+
37+
See {{ "ChatActionType" |> t }} for all available actions.
38+
39+
## Cancelling an Action
40+
41+
Send the `cancel` action when no response will follow.
42+
43+
```ts
44+
await client.sendChatAction(chatId, { type: "cancel" });
45+
```

src/streaming-responses.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: Streaming Responses
3+
parent: /#walkthrough
4+
walkthrough:
5+
track: bot
6+
order: 5
7+
---
8+
9+
Bots can stream text while generating a response.
10+
11+
## Streaming a Draft
12+
13+
Call {{ "sendMessageDraft" |> m }} repeatedly with the same draft ID. Each call should contain the complete text generated so far.
14+
15+
```ts
16+
client.command("answer", async (ctx) => {
17+
const draftId = ctx.msg.id;
18+
let response = "";
19+
20+
for await (const chunk of generateAnswer()) {
21+
response += chunk;
22+
await ctx.replyDraft(draftId, response);
23+
}
24+
25+
await ctx.reply(response);
26+
});
27+
```
28+
29+
Send the completed response normally after the draft finishes.
30+
31+
## Streaming a Rich Text Draft
32+
33+
Use {{ "sendRichTextDraft" |> m }} to stream an {{ "InputRichText" |> t }}. Reuse the same draft ID and finish with a regular rich text message.
34+
35+
```ts
36+
const draftId = ctx.msg.id;
37+
38+
await ctx.replyRichTextDraft(draftId, {
39+
type: "markdown",
40+
markdown: "# Answer\n\nGenerating…",
41+
});
42+
43+
await ctx.replyRichTextDraft(draftId, {
44+
type: "markdown",
45+
markdown: "# Answer\n\nThe answer is **42**.",
46+
});
47+
48+
await ctx.replyRichText({
49+
type: "markdown",
50+
markdown: "# Answer\n\nThe answer is **42**.",
51+
});
52+
```

0 commit comments

Comments
 (0)