File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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\n Generating…" ,
41+ });
42+
43+ await ctx .replyRichTextDraft (draftId , {
44+ type: " markdown" ,
45+ markdown: " # Answer\n\n The answer is **42**." ,
46+ });
47+
48+ await ctx .replyRichText ({
49+ type: " markdown" ,
50+ markdown: " # Answer\n\n The answer is **42**." ,
51+ });
52+ ```
You can’t perform that action at this time.
0 commit comments