@operad-io/telegram 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -8
- package/package.json +5 -4
- package/src/api.ts +94 -12
- package/src/commands.ts +489 -0
- package/src/events.ts +131 -0
- package/src/format.ts +26 -0
- package/src/index.ts +62 -7
- package/src/pending.ts +266 -34
- package/src/progress.ts +494 -0
- package/src/replies.ts +76 -34
- package/src/settings.ts +3 -0
- package/src/source.ts +161 -29
- package/src/state.ts +224 -24
- package/src/typing.ts +328 -0
- package/telegram.schema.json +3 -0
package/README.md
CHANGED
|
@@ -43,19 +43,33 @@ on it: each update is recorded once as a `telegram` event keyed by its
|
|
|
43
43
|
`update_id`, and a message is written only when the bound chat holds no message
|
|
44
44
|
whose source ref is that update id. A killed `operad serve` that starts again
|
|
45
45
|
reads the same update without writing a second message or starting a second
|
|
46
|
-
reply.
|
|
46
|
+
reply. A poll that returns is followed at once; only a failed poll waits a
|
|
47
|
+
second before the next try.
|
|
47
48
|
|
|
48
49
|
Each new assistant reply of a chat bound to Telegram is sent to that Telegram
|
|
49
|
-
chat once
|
|
50
|
-
links; headings and lists become
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
chat once, and so is the line that says a reply failed. Markdown is reduced to
|
|
51
|
+
what Telegram accepts (bold, italic, code, links; headings and lists become
|
|
52
|
+
plain lines) and a long reply goes out as several messages. A failed send is
|
|
53
|
+
retried with growing waits; when it gives up, the plugin records a
|
|
54
|
+
`telegram/send_failed` event for the chat (in the run log's inputs) and writes a
|
|
55
|
+
line in its own log; the chat's messages are not touched.
|
|
54
56
|
The event does **not** show in `operad history` of the chat or in the chat's History
|
|
55
57
|
section: those list only the chat's runs and the actions people ran on it, and read
|
|
56
58
|
no source event. Showing it there needs a kernel change. What was sent is kept in `<home>/telegram/sent.json`,
|
|
57
59
|
so a restart neither resends a reply nor skips one written while it was down.
|
|
58
|
-
Replies from before the plugin first ran are not sent.
|
|
60
|
+
Replies from before the plugin first ran are not sent. A full sweep moves the
|
|
61
|
+
point before which messages are not new, and the done records more than a day
|
|
62
|
+
behind it are dropped; an unfinished send stays.
|
|
63
|
+
|
|
64
|
+
While a reply is written, one progress message per reply is edited to the
|
|
65
|
+
transcript's unfinished part: the text so far and the tool calls in progress.
|
|
66
|
+
It changes at most once every two seconds and only when the text changed; a
|
|
67
|
+
long text shows its end within Telegram's 4096 characters, reduced like a reply
|
|
68
|
+
so an open code fence is closed. When the reply is recorded, the final reply is
|
|
69
|
+
sent as new message(s) and the progress message is deleted. A failed progress
|
|
70
|
+
edit never stops the final reply. The progress message id is kept in
|
|
71
|
+
`<home>/telegram/progress.json`, so a restart deletes a message a killed `serve`
|
|
72
|
+
left behind.
|
|
59
73
|
|
|
60
74
|
## What waits for a person
|
|
61
75
|
|
|
@@ -73,4 +87,6 @@ as the confirmation. After an answer from anywhere the buttons go and the
|
|
|
73
87
|
message ends "answered: X", X being the action or choice; a tap on an item
|
|
74
88
|
already answered says so and changes nothing. Where an item was answered
|
|
75
89
|
somewhere else and no action shows what it was, the message ends "answered".
|
|
76
|
-
What was sent is kept in `<home>/telegram/pending.json`.
|
|
90
|
+
What was sent is kept in `<home>/telegram/pending.json`. The file keeps the open
|
|
91
|
+
items and the newest 100 answered ones; older answered items are dropped when
|
|
92
|
+
the file is read and when an answer is settled.
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/telegram",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/chat": "0.
|
|
10
|
-
"@operad-io/engine": "0.
|
|
9
|
+
"@operad-io/chat": "0.3.0",
|
|
10
|
+
"@operad-io/engine": "0.3.0"
|
|
11
11
|
},
|
|
12
12
|
"description": "operad: the Telegram plugin, a channel and an outside source",
|
|
13
13
|
"repository": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"LICENSE"
|
|
24
24
|
],
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@operad-io/
|
|
26
|
+
"@operad-io/bot": "0.3.0",
|
|
27
|
+
"@operad-io/dev": "0.3.0"
|
|
27
28
|
}
|
|
28
29
|
}
|
package/src/api.ts
CHANGED
|
@@ -66,6 +66,15 @@ export interface SendMessageRequest {
|
|
|
66
66
|
readonly signal?: AbortSignal | undefined;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/** One message to react to. */
|
|
70
|
+
export interface SetMessageReactionRequest {
|
|
71
|
+
readonly chatId: number;
|
|
72
|
+
readonly messageId: number;
|
|
73
|
+
/** The emoji to set; `👀` is the only reaction this plugin sets. */
|
|
74
|
+
readonly reaction: string;
|
|
75
|
+
readonly signal?: AbortSignal | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
69
78
|
/** One inline-keyboard button. */
|
|
70
79
|
export interface InlineButton {
|
|
71
80
|
readonly text: string;
|
|
@@ -73,11 +82,22 @@ export interface InlineButton {
|
|
|
73
82
|
readonly data: string;
|
|
74
83
|
}
|
|
75
84
|
|
|
76
|
-
/** A message's new text;
|
|
85
|
+
/** A message's new text and what it offers; a new keyboard replaces the old one. */
|
|
77
86
|
export interface EditMessageRequest {
|
|
78
87
|
readonly chatId: number;
|
|
79
88
|
readonly messageId: number;
|
|
80
89
|
readonly text: string;
|
|
90
|
+
/** `HTML` when the text is already Telegram HTML; absent for plain text. */
|
|
91
|
+
readonly parseMode?: "HTML" | undefined;
|
|
92
|
+
/** The new inline keyboard; absent removes the one the message has. */
|
|
93
|
+
readonly keyboard?: readonly (readonly InlineButton[])[] | undefined;
|
|
94
|
+
readonly signal?: AbortSignal | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** One message to remove. */
|
|
98
|
+
export interface DeleteMessageRequest {
|
|
99
|
+
readonly chatId: number;
|
|
100
|
+
readonly messageId: number;
|
|
81
101
|
readonly signal?: AbortSignal | undefined;
|
|
82
102
|
}
|
|
83
103
|
|
|
@@ -88,6 +108,28 @@ export interface AnswerCallbackRequest {
|
|
|
88
108
|
readonly signal?: AbortSignal | undefined;
|
|
89
109
|
}
|
|
90
110
|
|
|
111
|
+
/** One command of a bot's menu. */
|
|
112
|
+
export interface BotCommand {
|
|
113
|
+
/** The name typed after `/`: lowercase letters, digits and underscores, at most 32 characters. */
|
|
114
|
+
readonly command: string;
|
|
115
|
+
/** The one line shown beside the name. */
|
|
116
|
+
readonly description: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The menu of a bot. */
|
|
120
|
+
export interface SetMyCommandsRequest {
|
|
121
|
+
readonly commands: readonly BotCommand[];
|
|
122
|
+
readonly signal?: AbortSignal | undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What a chat shows of the bot while a reply is written. */
|
|
126
|
+
export interface SendChatActionRequest {
|
|
127
|
+
readonly chatId: number;
|
|
128
|
+
/** `typing` is the only action this plugin sends. */
|
|
129
|
+
readonly action: "typing";
|
|
130
|
+
readonly signal?: AbortSignal | undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
91
133
|
/** Why a Bot API call failed, and whether trying again can help. */
|
|
92
134
|
export class BotApiError extends Error {
|
|
93
135
|
/** False for a refusal that will not change (a bad request, a blocked bot). */
|
|
@@ -114,10 +156,18 @@ export interface BotApi {
|
|
|
114
156
|
getUpdates(request: GetUpdatesRequest): Promise<readonly TelegramUpdate[]>;
|
|
115
157
|
/** Sends one text message; returns its Telegram `message_id`. */
|
|
116
158
|
sendMessage(request: SendMessageRequest): Promise<number>;
|
|
117
|
-
/**
|
|
159
|
+
/** Sets one emoji reaction on a message. */
|
|
160
|
+
setMessageReaction(request: SetMessageReactionRequest): Promise<void>;
|
|
161
|
+
/** Replaces a message's text; its inline keyboard goes, or becomes the one given. */
|
|
118
162
|
editMessage(request: EditMessageRequest): Promise<void>;
|
|
163
|
+
/** Sets the bot's command menu. */
|
|
164
|
+
setMyCommands(request: SetMyCommandsRequest): Promise<void>;
|
|
165
|
+
/** Removes one message. */
|
|
166
|
+
deleteMessage(request: DeleteMessageRequest): Promise<void>;
|
|
119
167
|
/** Tells Telegram the tap was seen, with a short note for the person. */
|
|
120
168
|
answerCallback(request: AnswerCallbackRequest): Promise<void>;
|
|
169
|
+
/** Shows what the bot is doing in a chat, such as that it is typing. */
|
|
170
|
+
sendChatAction(request: SendChatActionRequest): Promise<void>;
|
|
121
171
|
}
|
|
122
172
|
|
|
123
173
|
export interface BotApiOptions {
|
|
@@ -208,6 +258,13 @@ function updateOf(value: unknown): TelegramUpdate | undefined {
|
|
|
208
258
|
};
|
|
209
259
|
}
|
|
210
260
|
|
|
261
|
+
/** The Bot API's inline keyboard for rows of buttons. */
|
|
262
|
+
function keyboardMarkup(
|
|
263
|
+
keyboard: readonly (readonly InlineButton[])[] | undefined,
|
|
264
|
+
): readonly (readonly { readonly text: string; readonly callback_data: string }[])[] {
|
|
265
|
+
return (keyboard ?? []).map((row) => row.map((button) => ({ text: button.text, callback_data: button.data })));
|
|
266
|
+
}
|
|
267
|
+
|
|
211
268
|
/** The Bot API over `fetch`; no runtime package is needed. */
|
|
212
269
|
export function createBotApi(options: BotApiOptions): BotApi {
|
|
213
270
|
const token = options.token.reveal();
|
|
@@ -253,12 +310,27 @@ export function createBotApi(options: BotApiOptions): BotApi {
|
|
|
253
310
|
chat_id: input.chatId,
|
|
254
311
|
message_id: input.messageId,
|
|
255
312
|
text: input.text,
|
|
256
|
-
|
|
313
|
+
...(input.parseMode === undefined ? {} : { parse_mode: input.parseMode }),
|
|
314
|
+
reply_markup: { inline_keyboard: keyboardMarkup(input.keyboard) },
|
|
257
315
|
link_preview_options: { is_disabled: true },
|
|
258
316
|
},
|
|
259
317
|
input.signal,
|
|
260
318
|
);
|
|
261
319
|
},
|
|
320
|
+
async setMyCommands(input) {
|
|
321
|
+
await post(
|
|
322
|
+
"setMyCommands",
|
|
323
|
+
{ commands: input.commands.map((command) => ({ command: command.command, description: command.description })) },
|
|
324
|
+
input.signal,
|
|
325
|
+
);
|
|
326
|
+
},
|
|
327
|
+
async deleteMessage(input) {
|
|
328
|
+
await post(
|
|
329
|
+
"deleteMessage",
|
|
330
|
+
{ chat_id: input.chatId, message_id: input.messageId },
|
|
331
|
+
input.signal,
|
|
332
|
+
);
|
|
333
|
+
},
|
|
262
334
|
async answerCallback(input) {
|
|
263
335
|
await post(
|
|
264
336
|
"answerCallbackQuery",
|
|
@@ -269,6 +341,24 @@ export function createBotApi(options: BotApiOptions): BotApi {
|
|
|
269
341
|
input.signal,
|
|
270
342
|
);
|
|
271
343
|
},
|
|
344
|
+
async sendChatAction(input) {
|
|
345
|
+
await post(
|
|
346
|
+
"sendChatAction",
|
|
347
|
+
{ chat_id: input.chatId, action: input.action },
|
|
348
|
+
input.signal,
|
|
349
|
+
);
|
|
350
|
+
},
|
|
351
|
+
async setMessageReaction(input) {
|
|
352
|
+
await post(
|
|
353
|
+
"setMessageReaction",
|
|
354
|
+
{
|
|
355
|
+
chat_id: input.chatId,
|
|
356
|
+
message_id: input.messageId,
|
|
357
|
+
reaction: [{ type: "emoji", emoji: input.reaction }],
|
|
358
|
+
},
|
|
359
|
+
input.signal,
|
|
360
|
+
);
|
|
361
|
+
},
|
|
272
362
|
async getUpdates(input) {
|
|
273
363
|
const query = new URLSearchParams({
|
|
274
364
|
offset: String(input.offset),
|
|
@@ -311,15 +401,7 @@ export function createBotApi(options: BotApiOptions): BotApi {
|
|
|
311
401
|
chat_id: input.chatId,
|
|
312
402
|
text: input.text,
|
|
313
403
|
...(input.parseMode === undefined ? {} : { parse_mode: input.parseMode }),
|
|
314
|
-
...(input.keyboard === undefined
|
|
315
|
-
? {}
|
|
316
|
-
: {
|
|
317
|
-
reply_markup: {
|
|
318
|
-
inline_keyboard: input.keyboard.map((row) =>
|
|
319
|
-
row.map((button) => ({ text: button.text, callback_data: button.data })),
|
|
320
|
-
),
|
|
321
|
-
},
|
|
322
|
-
}),
|
|
404
|
+
...(input.keyboard === undefined ? {} : { reply_markup: { inline_keyboard: keyboardMarkup(input.keyboard) } }),
|
|
323
405
|
link_preview_options: { is_disabled: true },
|
|
324
406
|
};
|
|
325
407
|
let response: Response;
|