@segmentstream/ontology 0.183.0 → 0.185.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/CHANGELOG.md +62 -0
- package/README.md +5 -2
- package/dist/channel-files.d.ts +17 -0
- package/dist/email.d.ts +3 -7
- package/dist/service/chat/providers/email.js +13 -37
- package/dist/service/chat/providers/files-send.d.ts +4 -0
- package/dist/service/chat/providers/files-send.js +7 -3
- package/dist/service/chat/providers/provider.d.ts +21 -1
- package/dist/service/chat/providers/provider.js +82 -0
- package/dist/service/chat/providers/telegram.d.ts +21 -8
- package/dist/service/chat/providers/telegram.js +195 -48
- package/dist/service/chat/providers/whatsapp.d.ts +5 -1
- package/dist/service/chat/providers/whatsapp.js +152 -9
- package/dist/telegram.d.ts +84 -1
- package/dist/telegram.js +14 -0
- package/dist/whatsapp-files.d.ts +25 -1
- package/dist/whatsapp-files.js +68 -41
- package/dist/whatsapp.d.ts +83 -3
- package/dist/whatsapp.js +14 -2
- package/package.json +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { isTelegramBotUsername, miniAppLink, telegramMarkdownLinks, telegramMarkdownWithLinks, telegramTextLinks, telegramPreviewOptions, } from "../../../telegram.js";
|
|
1
|
+
import { isTelegramBotUsername, miniAppLink, telegramDeliverRoute, telegramMarkdownLinks, telegramMarkdownWithLinks, telegramTextLinks, telegramPreviewOptions, TELEGRAM_MESSAGE_LIMIT, } from "../../../telegram.js";
|
|
2
2
|
import { escapeMarkdownV2, TelegramFormatConverter } from "@chat-adapter/telegram";
|
|
3
|
-
import { Effect } from "effect";
|
|
3
|
+
import { Cause, Effect } from "effect";
|
|
4
4
|
import { refuseTelegramFiles, sendTelegramWithFiles, TELEGRAM_FILE_LIMITS } from "./files-send.js";
|
|
5
|
-
import { record, transportFailure, } from "./provider.js";
|
|
6
|
-
export
|
|
5
|
+
import { agentFileLinks, deliveredFiles, postToAgent, record, transportFailure, } from "./provider.js";
|
|
6
|
+
export { TELEGRAM_MESSAGE_LIMIT };
|
|
7
7
|
const converter = new TelegramFormatConverter();
|
|
8
8
|
/**
|
|
9
9
|
* Render source independently so user Markdown cannot open an entity around the footer. Without an
|
|
@@ -88,21 +88,42 @@ export function telegramKeyboard(rows, links = new Map()) {
|
|
|
88
88
|
: { text: button.text, callback_data: `ws:${button.token}` })),
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* One text's `sendMessage` fields as Telegram is sent them: the MarkdownV2 text and, for the one
|
|
93
|
+
* retry a refusal of its formatting allows, the plain text, each with the link preview its own
|
|
94
|
+
* links choose.
|
|
95
|
+
*/
|
|
96
|
+
export function telegramPart(input) {
|
|
97
|
+
const caller = {
|
|
98
|
+
link_preview_options: input.link_preview_options,
|
|
99
|
+
disable_web_page_preview: input.disable_web_page_preview,
|
|
100
|
+
};
|
|
101
|
+
const preview = (links) => telegramPreviewOptions(caller, links, input.previews);
|
|
102
|
+
return {
|
|
103
|
+
formatted: {
|
|
104
|
+
text: input.finalText,
|
|
105
|
+
parse_mode: "MarkdownV2",
|
|
106
|
+
...preview(input.formattedLinks ?? telegramMarkdownLinks(input.plainText)),
|
|
107
|
+
},
|
|
108
|
+
plain: { text: input.plainText, ...preview(telegramTextLinks(input.plainText)) },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
91
111
|
/** One proxy POST, except one plain retry after a definite 400 entity parse refusal.
|
|
92
112
|
* Never retry an unknown outcome; never chunk, truncate or invoke an agent.
|
|
113
|
+
* @legacy ontology.telegram-own-sender The ontology's own send of a text, which the agent's
|
|
114
|
+
* Telegram channel's deliver route does in its place.
|
|
93
115
|
*/
|
|
94
116
|
export function sendTelegram(input) {
|
|
117
|
+
return sendTelegramPart(input, telegramPart(input), input.replyMarkup);
|
|
118
|
+
}
|
|
119
|
+
function sendTelegramPart(target, part, replyMarkup) {
|
|
95
120
|
return Effect.gen(function* () {
|
|
96
|
-
const markup =
|
|
97
|
-
const formatted = yield* attemptTelegram(
|
|
98
|
-
text: input.finalText,
|
|
99
|
-
parse_mode: "MarkdownV2",
|
|
100
|
-
...markup,
|
|
101
|
-
});
|
|
121
|
+
const markup = replyMarkup === undefined ? {} : { reply_markup: replyMarkup };
|
|
122
|
+
const formatted = yield* attemptTelegram(target, part.formatted, markup);
|
|
102
123
|
if (!formatted.formattingRefused) {
|
|
103
124
|
return formatted;
|
|
104
125
|
}
|
|
105
|
-
const plain = yield* attemptTelegram(
|
|
126
|
+
const plain = yield* attemptTelegram(target, part.plain, markup);
|
|
106
127
|
return {
|
|
107
128
|
...plain,
|
|
108
129
|
cause: plain.cause === undefined
|
|
@@ -111,8 +132,14 @@ export function sendTelegram(input) {
|
|
|
111
132
|
};
|
|
112
133
|
});
|
|
113
134
|
}
|
|
114
|
-
function attemptTelegram(input,
|
|
135
|
+
function attemptTelegram(input, text, markup) {
|
|
115
136
|
return Effect.gen(function* () {
|
|
137
|
+
// What the delivery's history keeps: the text and its format, with its buttons.
|
|
138
|
+
const payload = {
|
|
139
|
+
text: text.text,
|
|
140
|
+
...(text.parse_mode === undefined ? {} : { parse_mode: text.parse_mode }),
|
|
141
|
+
...markup,
|
|
142
|
+
};
|
|
116
143
|
const result = yield* input.transport
|
|
117
144
|
.request({
|
|
118
145
|
connection: input.connection,
|
|
@@ -127,13 +154,8 @@ function attemptTelegram(input, payload) {
|
|
|
127
154
|
!/^[1-9][0-9]*$/.test(input.threadId)
|
|
128
155
|
? {}
|
|
129
156
|
: { message_thread_id: Number(input.threadId) }),
|
|
130
|
-
...
|
|
131
|
-
...
|
|
132
|
-
link_preview_options: input.link_preview_options,
|
|
133
|
-
disable_web_page_preview: input.disable_web_page_preview,
|
|
134
|
-
}, payload.parse_mode === "MarkdownV2"
|
|
135
|
-
? (input.formattedLinks ?? telegramMarkdownLinks(input.plainText))
|
|
136
|
-
: telegramTextLinks(payload.text), input.previews),
|
|
157
|
+
...text,
|
|
158
|
+
...markup,
|
|
137
159
|
...(input.replyTo === null
|
|
138
160
|
? {}
|
|
139
161
|
: {
|
|
@@ -202,7 +224,10 @@ function attemptTelegram(input, payload) {
|
|
|
202
224
|
return { payload, status: "sent", messageId, url };
|
|
203
225
|
});
|
|
204
226
|
}
|
|
205
|
-
/**
|
|
227
|
+
/**
|
|
228
|
+
* Telegram: the ontology renders the message, its MarkdownV2 with one plain retry after a definite
|
|
229
|
+
* entity parse refusal, and the agent's Telegram channel of the chat's name sends it.
|
|
230
|
+
*/
|
|
206
231
|
export const telegramProvider = {
|
|
207
232
|
provider: "telegram",
|
|
208
233
|
limit: TELEGRAM_MESSAGE_LIMIT,
|
|
@@ -221,30 +246,20 @@ export const telegramProvider = {
|
|
|
221
246
|
refuseButtons: () => undefined,
|
|
222
247
|
files: TELEGRAM_FILE_LIMITS,
|
|
223
248
|
refuseFiles: refuseTelegramFiles,
|
|
224
|
-
send:
|
|
249
|
+
send: sendTelegramMessage,
|
|
225
250
|
};
|
|
226
251
|
/**
|
|
227
|
-
*
|
|
228
|
-
*
|
|
252
|
+
* The member's Markdown as Telegram is sent it, with the chat's links to the app and previews: only
|
|
253
|
+
* where a link to the app leads changes, so the text reads as the member approved it. A text that
|
|
254
|
+
* goes as the caption of files is the same, so its links open in the bot's Mini App too.
|
|
229
255
|
*/
|
|
230
|
-
function
|
|
231
|
-
return Effect.gen(function* () {
|
|
232
|
-
const links = yield* miniAppLinksOf(input, telegramMarkdownLinks(input.text));
|
|
233
|
-
const finalText = links.size === 0
|
|
234
|
-
? input.finalText
|
|
235
|
-
: renderTelegram(input.text, input.attribution, input.separator, links);
|
|
236
|
-
return yield* sendTelegramWithFiles({ ...input, finalText }, sendTelegramText);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
/** A text reply or post: the member's Markdown, with the chat's links to the app and previews. */
|
|
240
|
-
function sendTelegramText(input) {
|
|
256
|
+
function telegramMessage(input) {
|
|
241
257
|
return Effect.gen(function* () {
|
|
242
258
|
const written = [
|
|
243
259
|
...telegramMarkdownLinks(input.text),
|
|
244
260
|
...telegramTextLinks(input.separator),
|
|
245
261
|
...telegramTextLinks(input.attribution),
|
|
246
262
|
];
|
|
247
|
-
// Only where a link to the app leads changes: the text reads as the member approved it.
|
|
248
263
|
const buttonUrls = (input.buttons ?? []).flatMap((row) => row.flatMap((button) => ("url" in button ? [button.url] : [])));
|
|
249
264
|
const links = yield* miniAppLinksOf(input, [
|
|
250
265
|
...telegramMarkdownLinks(input.text),
|
|
@@ -256,22 +271,154 @@ function sendTelegramText(input) {
|
|
|
256
271
|
const preview = input.previews?.settings?.linkPreviews === "all" && first !== undefined && links.has(first)
|
|
257
272
|
? { link_preview_options: { url: first } }
|
|
258
273
|
: {};
|
|
259
|
-
return
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
:
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
plainText: input.plainText,
|
|
270
|
-
replyTo: input.replyTo,
|
|
271
|
-
...(input.threadId === undefined ? {} : { threadId: input.threadId }),
|
|
274
|
+
return {
|
|
275
|
+
part: telegramPart({
|
|
276
|
+
finalText: links.size === 0
|
|
277
|
+
? input.finalText
|
|
278
|
+
: renderTelegram(input.text, input.attribution, input.separator, links),
|
|
279
|
+
plainText: input.plainText,
|
|
280
|
+
formattedLinks: written,
|
|
281
|
+
...(input.previews === undefined ? {} : { previews: input.previews }),
|
|
282
|
+
...preview,
|
|
283
|
+
}),
|
|
272
284
|
...(input.buttons === undefined
|
|
273
285
|
? {}
|
|
274
286
|
: { replyMarkup: telegramKeyboard(input.buttons, links) }),
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* One message, rendered here and sent by the agent's Telegram channel; by the ontology itself for
|
|
292
|
+
* an agent without its deliver route.
|
|
293
|
+
*/
|
|
294
|
+
function sendTelegramMessage(input) {
|
|
295
|
+
return Effect.gen(function* () {
|
|
296
|
+
const message = yield* telegramMessage(input);
|
|
297
|
+
if (input.agent?.origin !== undefined && input.agent.identityToken !== undefined) {
|
|
298
|
+
const delivered = yield* deliverThroughAgent(input, message);
|
|
299
|
+
if (delivered !== undefined) {
|
|
300
|
+
return delivered;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// @legacy ontology.telegram-own-sender The ontology's own Telegram sender, for an agent
|
|
304
|
+
// without the Telegram channel's deliver route or without a Telegram channel of the chat's
|
|
305
|
+
// name on its connection: the text, then the files' bytes, read and checked before the message
|
|
306
|
+
// was reserved, through the ontology's connection.
|
|
307
|
+
const text = () => sendTelegramPart(input, message.part, message.replyMarkup);
|
|
308
|
+
return yield* (input.files ?? []).length > 0
|
|
309
|
+
? sendTelegramWithFiles({ ...input, finalText: message.part.formatted.text }, text)
|
|
310
|
+
: text();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* How long the agent's Telegram channel has to send one message: its texts, then its files, each
|
|
315
|
+
* through the connection.
|
|
316
|
+
*/
|
|
317
|
+
const AGENT_DELIVER_TIMEOUT_MS = 90_000;
|
|
318
|
+
/** A message id Telegram names. */
|
|
319
|
+
const MESSAGE_ID = /^[1-9][0-9]*$/;
|
|
320
|
+
/**
|
|
321
|
+
* One message through the agent's Telegram channel (`POST` {@link telegramDeliverRoute}), never
|
|
322
|
+
* retried: the channel sends the parts rendered here in order, through its bot, then the files by
|
|
323
|
+
* their links. Undefined when the agent has no such route (`404`) or no Telegram channel of the
|
|
324
|
+
* chat's name on this connection (`channel_unknown`), so the ontology sends it itself. The agent's
|
|
325
|
+
* other `4xx` sent nothing; a `2xx` says what reached the person; a timeout, a `5xx` or an answer
|
|
326
|
+
* without its shape may have sent it, and is uncertain.
|
|
327
|
+
*/
|
|
328
|
+
function deliverThroughAgent(input, message) {
|
|
329
|
+
return Effect.gen(function* () {
|
|
330
|
+
const files = input.files ?? [];
|
|
331
|
+
const markup = message.replyMarkup === undefined ? {} : { reply_markup: message.replyMarkup };
|
|
332
|
+
const payload = {
|
|
333
|
+
text: message.part.formatted.text,
|
|
334
|
+
parse_mode: "MarkdownV2",
|
|
335
|
+
...markup,
|
|
336
|
+
};
|
|
337
|
+
// What became of each file, as the history keeps it, by its name.
|
|
338
|
+
const outcome = (status, results, rest) => ({
|
|
339
|
+
payload: results === undefined
|
|
340
|
+
? payload
|
|
341
|
+
: {
|
|
342
|
+
...payload,
|
|
343
|
+
files: results.map((result) => ({
|
|
344
|
+
...result,
|
|
345
|
+
filename: files[result.index]?.filename ?? "",
|
|
346
|
+
})),
|
|
347
|
+
},
|
|
348
|
+
status,
|
|
349
|
+
...rest,
|
|
350
|
+
...(results === undefined ? {} : { files: results }),
|
|
275
351
|
});
|
|
352
|
+
const unsent = files.length === 0
|
|
353
|
+
? undefined
|
|
354
|
+
: files.map((_file, index) => ({ index, status: "failed", messageId: null }));
|
|
355
|
+
const links = yield* agentFileLinks(files).pipe(Effect.exit);
|
|
356
|
+
if (links._tag === "Failure") {
|
|
357
|
+
return outcome("failed", unsent, {
|
|
358
|
+
messageId: null,
|
|
359
|
+
url: null,
|
|
360
|
+
cause: Cause.squash(links.cause),
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
const thread = typeof input.threadId === "string" && /^[1-9][0-9]*$/u.test(input.threadId)
|
|
364
|
+
? input.threadId
|
|
365
|
+
: null;
|
|
366
|
+
const body = {
|
|
367
|
+
operation_id: input.operationId ?? input.destination.id,
|
|
368
|
+
connection: input.connection,
|
|
369
|
+
chat_id: input.destination.external_id,
|
|
370
|
+
thread_id: thread,
|
|
371
|
+
reply_to: input.replyTo,
|
|
372
|
+
// A message of files alone has no text to send, nor to caption them with.
|
|
373
|
+
parts: input.plainText.trim() === "" ? [] : [message.part],
|
|
374
|
+
...markup,
|
|
375
|
+
...(links.value.length === 0 ? {} : { files: links.value }),
|
|
376
|
+
};
|
|
377
|
+
const answered = yield* postToAgent(input.agent, telegramDeliverRoute(input.destination.provider), body, AGENT_DELIVER_TIMEOUT_MS).pipe(Effect.exit);
|
|
378
|
+
if (answered._tag === "Failure") {
|
|
379
|
+
return outcome("uncertain", undefined, {
|
|
380
|
+
messageId: null,
|
|
381
|
+
url: null,
|
|
382
|
+
cause: Cause.squash(answered.cause),
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
const { status, answer } = answered.value;
|
|
386
|
+
if (status === 404 || (status === 422 && answer.error === "channel_unknown")) {
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
if (status >= 400 && status < 500) {
|
|
390
|
+
return outcome("failed", unsent, {
|
|
391
|
+
messageId: null,
|
|
392
|
+
url: null,
|
|
393
|
+
cause: new Error(`The agent's Telegram channel refused the message (${typeof answer.error === "string" ? answer.error : `status ${status}`}${typeof answer.reference === "string" ? `, reference ${answer.reference}` : ""}).`),
|
|
394
|
+
...(answer.refusal === "recipient_unreachable"
|
|
395
|
+
? { refusal: "recipient_unreachable" }
|
|
396
|
+
: {}),
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
const results = deliveredFiles(answer.files, files.length, MESSAGE_ID);
|
|
400
|
+
const id = answer.message_id;
|
|
401
|
+
const url = typeof answer.url === "string" && answer.url.startsWith("https://t.me/") ? answer.url : null;
|
|
402
|
+
if (status < 200 ||
|
|
403
|
+
status >= 300 ||
|
|
404
|
+
(answer.status !== "sent" && answer.status !== "uncertain") ||
|
|
405
|
+
(id !== null && (typeof id !== "string" || !MESSAGE_ID.test(id))) ||
|
|
406
|
+
(answer.status === "sent" &&
|
|
407
|
+
(id === null || results?.some((result) => result.status !== "sent") === true)) ||
|
|
408
|
+
(files.length > 0 && results === undefined)) {
|
|
409
|
+
return outcome("uncertain", undefined, {
|
|
410
|
+
messageId: null,
|
|
411
|
+
url: null,
|
|
412
|
+
cause: new Error(`The agent's Telegram channel answered with status ${status}.`),
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return answer.status === "sent"
|
|
416
|
+
? outcome("sent", results, { messageId: id, url })
|
|
417
|
+
: outcome("uncertain", results, {
|
|
418
|
+
messageId: id,
|
|
419
|
+
url,
|
|
420
|
+
cause: new Error("The agent's Telegram channel could not prove every message sent" +
|
|
421
|
+
`${typeof answer.reference === "string" ? ` (reference ${answer.reference})` : ""}.`),
|
|
422
|
+
});
|
|
276
423
|
});
|
|
277
424
|
}
|
|
@@ -18,10 +18,14 @@ export declare function whatsappInteractive(finalText: string, rows: ChatButtonR
|
|
|
18
18
|
* `graph` API, never retried. A Graph refusal with an error code, such as 131047 outside the
|
|
19
19
|
* 24-hour customer-service window, is definite; a timeout, a 5xx, an unparseable answer or a
|
|
20
20
|
* success without the message's id is uncertain. `sent` means Meta accepted the message.
|
|
21
|
+
* @legacy ontology.whatsapp-own-sender The ontology's own send of a message without files, which
|
|
22
|
+
* the agent's WhatsApp channel's deliver route does in its place.
|
|
21
23
|
*/
|
|
22
24
|
export declare function sendWhatsApp(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
23
25
|
/**
|
|
24
26
|
* WhatsApp: a paired member's chat with the workspace's number, by their WhatsApp id. WhatsApp
|
|
25
|
-
* has no group destinations, and a post cannot reply to a message.
|
|
27
|
+
* has no group destinations, and a post cannot reply to a message. The ontology renders the
|
|
28
|
+
* message, its text, buttons or template, and the agent's WhatsApp channel of the chat's name
|
|
29
|
+
* sends it from its number.
|
|
26
30
|
*/
|
|
27
31
|
export declare const whatsappProvider: ChatProvider;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
|
-
import { renderWhatsApp, WHATSAPP_MESSAGE_LIMIT } from "../../../whatsapp.js";
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
|
+
import { renderWhatsApp, whatsappDeliverRoute, WHATSAPP_MESSAGE_LIMIT, } from "../../../whatsapp.js";
|
|
3
3
|
import { whatsappTemplate } from "./whatsapp-template.js";
|
|
4
4
|
import { REFUSALS, WAMID } from "./whatsapp-refusals.js";
|
|
5
5
|
import { refuseWhatsAppFiles, sendWhatsAppWithFiles, WHATSAPP_FILE_LIMITS } from "./files-send.js";
|
|
6
|
-
import { record, transportFailure, } from "./provider.js";
|
|
6
|
+
import { agentFileLinks, deliveredFiles, postToAgent, record, transportFailure, } from "./provider.js";
|
|
7
7
|
/** A WhatsApp id: the member's number, digits only. */
|
|
8
8
|
const WA_ID = /^[1-9][0-9]{5,19}$/;
|
|
9
9
|
/** The most characters of an interactive message's body, under which WhatsApp shows buttons. */
|
|
@@ -66,18 +66,27 @@ export function whatsappInteractive(finalText, rows) {
|
|
|
66
66
|
},
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* A message without files as WhatsApp is sent it: the approved template the delivery read, else
|
|
71
|
+
* the text, as an interactive message with its buttons when it has them. Link previews stay off.
|
|
72
|
+
*/
|
|
73
|
+
function whatsappMessage(input) {
|
|
74
|
+
return (input.payload ??
|
|
75
|
+
(input.buttons === undefined
|
|
76
|
+
? { type: "text", text: { body: input.finalText, preview_url: false } }
|
|
77
|
+
: whatsappInteractive(input.finalText, input.buttons)));
|
|
78
|
+
}
|
|
69
79
|
/**
|
|
70
80
|
* One text message from the workspace's number, `POST /phone/messages` on the connection's
|
|
71
81
|
* `graph` API, never retried. A Graph refusal with an error code, such as 131047 outside the
|
|
72
82
|
* 24-hour customer-service window, is definite; a timeout, a 5xx, an unparseable answer or a
|
|
73
83
|
* success without the message's id is uncertain. `sent` means Meta accepted the message.
|
|
84
|
+
* @legacy ontology.whatsapp-own-sender The ontology's own send of a message without files, which
|
|
85
|
+
* the agent's WhatsApp channel's deliver route does in its place.
|
|
74
86
|
*/
|
|
75
87
|
export function sendWhatsApp(input) {
|
|
76
88
|
return Effect.gen(function* () {
|
|
77
|
-
const payload = input
|
|
78
|
-
(input.buttons === undefined
|
|
79
|
-
? { type: "text", text: { body: input.finalText, preview_url: false } }
|
|
80
|
-
: whatsappInteractive(input.finalText, input.buttons));
|
|
89
|
+
const payload = whatsappMessage(input);
|
|
81
90
|
const result = yield* input.transport
|
|
82
91
|
.request({
|
|
83
92
|
connection: input.connection,
|
|
@@ -141,7 +150,9 @@ export function sendWhatsApp(input) {
|
|
|
141
150
|
}
|
|
142
151
|
/**
|
|
143
152
|
* WhatsApp: a paired member's chat with the workspace's number, by their WhatsApp id. WhatsApp
|
|
144
|
-
* has no group destinations, and a post cannot reply to a message.
|
|
153
|
+
* has no group destinations, and a post cannot reply to a message. The ontology renders the
|
|
154
|
+
* message, its text, buttons or template, and the agent's WhatsApp channel of the chat's name
|
|
155
|
+
* sends it from its number.
|
|
145
156
|
*/
|
|
146
157
|
export const whatsappProvider = {
|
|
147
158
|
provider: "whatsapp",
|
|
@@ -158,6 +169,138 @@ export const whatsappProvider = {
|
|
|
158
169
|
refuseButtons: refuseWhatsAppButtons,
|
|
159
170
|
files: WHATSAPP_FILE_LIMITS,
|
|
160
171
|
refuseFiles: refuseWhatsAppFiles,
|
|
161
|
-
send:
|
|
172
|
+
send: deliverWhatsApp,
|
|
162
173
|
template: whatsappTemplate,
|
|
163
174
|
};
|
|
175
|
+
/**
|
|
176
|
+
* One message, rendered here and sent by the agent's WhatsApp channel; by the ontology itself for
|
|
177
|
+
* an agent without its deliver route.
|
|
178
|
+
*/
|
|
179
|
+
function deliverWhatsApp(input) {
|
|
180
|
+
return Effect.gen(function* () {
|
|
181
|
+
if (input.agent?.origin !== undefined && input.agent.identityToken !== undefined) {
|
|
182
|
+
const delivered = yield* deliverThroughAgent(input);
|
|
183
|
+
if (delivered !== undefined) {
|
|
184
|
+
return delivered;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// @legacy ontology.whatsapp-own-sender The ontology's own WhatsApp sender, for an agent
|
|
188
|
+
// without the WhatsApp channel's deliver route or without a WhatsApp channel of the chat's
|
|
189
|
+
// name on its connection: the message, or each file by a link granted just before it, through
|
|
190
|
+
// the ontology's connection.
|
|
191
|
+
return yield* (input.files ?? []).length > 0
|
|
192
|
+
? sendWhatsAppWithFiles(input)
|
|
193
|
+
: sendWhatsApp(input);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* How long the agent's WhatsApp channel has to send one message: its text or template, or its
|
|
198
|
+
* caption and each of its files, one message each, through the connection.
|
|
199
|
+
*/
|
|
200
|
+
const AGENT_DELIVER_TIMEOUT_MS = 90_000;
|
|
201
|
+
/** The refusals the agent's channel may name, as Meta's codes name them. */
|
|
202
|
+
const AGENT_REFUSALS = new Set(Object.values(REFUSALS));
|
|
203
|
+
/**
|
|
204
|
+
* One message through the agent's WhatsApp channel (`POST` {@link whatsappDeliverRoute}), never
|
|
205
|
+
* retried: the channel sends what is rendered here from its number, the message, or the caption
|
|
206
|
+
* and then each file by its link. Undefined when the agent has no such route (`404`) or no
|
|
207
|
+
* WhatsApp channel of the chat's name on this connection (`channel_unknown`), so the ontology
|
|
208
|
+
* sends it itself. The agent's other `4xx` sent nothing, with Meta's refusal of a message without
|
|
209
|
+
* files where it names one; a `2xx` says what reached the person; a timeout, a `5xx` or an answer
|
|
210
|
+
* without its shape may have sent it, and is uncertain.
|
|
211
|
+
*/
|
|
212
|
+
function deliverThroughAgent(input) {
|
|
213
|
+
return Effect.gen(function* () {
|
|
214
|
+
const files = input.files ?? [];
|
|
215
|
+
const message = whatsappMessage(input);
|
|
216
|
+
// What the delivery's history keeps, as the ontology's own sender kept it.
|
|
217
|
+
const payload = files.length === 0 ? message : { caption: input.finalText };
|
|
218
|
+
const outcome = (status, results, rest) => ({
|
|
219
|
+
payload: results === undefined
|
|
220
|
+
? payload
|
|
221
|
+
: {
|
|
222
|
+
...payload,
|
|
223
|
+
files: results.map((result) => ({
|
|
224
|
+
index: result.index,
|
|
225
|
+
filename: files[result.index]?.filename ?? "",
|
|
226
|
+
status: result.status,
|
|
227
|
+
messageId: result.messageId,
|
|
228
|
+
})),
|
|
229
|
+
},
|
|
230
|
+
status,
|
|
231
|
+
...rest,
|
|
232
|
+
...(results === undefined ? {} : { files: results }),
|
|
233
|
+
});
|
|
234
|
+
const unsent = files.length === 0
|
|
235
|
+
? undefined
|
|
236
|
+
: files.map((_file, index) => ({ index, status: "failed", messageId: null }));
|
|
237
|
+
const links = yield* agentFileLinks(files).pipe(Effect.exit);
|
|
238
|
+
if (links._tag === "Failure") {
|
|
239
|
+
return outcome("failed", unsent, {
|
|
240
|
+
messageId: null,
|
|
241
|
+
url: null,
|
|
242
|
+
cause: Cause.squash(links.cause),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const body = {
|
|
246
|
+
operation_id: input.operationId ?? input.destination.id,
|
|
247
|
+
connection: input.connection,
|
|
248
|
+
to: input.destination.external_id,
|
|
249
|
+
...(files.length === 0
|
|
250
|
+
? { message: message }
|
|
251
|
+
: {
|
|
252
|
+
// A message of files alone has no text to caption them with.
|
|
253
|
+
...(input.plainText.trim() === "" ? {} : { caption: input.finalText }),
|
|
254
|
+
files: links.value,
|
|
255
|
+
}),
|
|
256
|
+
};
|
|
257
|
+
const answered = yield* postToAgent(input.agent, whatsappDeliverRoute(input.destination.provider), body, AGENT_DELIVER_TIMEOUT_MS).pipe(Effect.exit);
|
|
258
|
+
if (answered._tag === "Failure") {
|
|
259
|
+
return outcome("uncertain", undefined, {
|
|
260
|
+
messageId: null,
|
|
261
|
+
url: null,
|
|
262
|
+
cause: Cause.squash(answered.cause),
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const { status, answer } = answered.value;
|
|
266
|
+
if (status === 404 || (status === 422 && answer.error === "channel_unknown")) {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
if (status >= 400 && status < 500) {
|
|
270
|
+
const refusal = files.length === 0 &&
|
|
271
|
+
typeof answer.refusal === "string" &&
|
|
272
|
+
AGENT_REFUSALS.has(answer.refusal)
|
|
273
|
+
? answer.refusal
|
|
274
|
+
: undefined;
|
|
275
|
+
return outcome("failed", unsent, {
|
|
276
|
+
messageId: null,
|
|
277
|
+
url: null,
|
|
278
|
+
cause: new Error(`The agent's WhatsApp channel refused the message (${typeof answer.error === "string" ? answer.error : `status ${status}`}${typeof answer.reference === "string" ? `, reference ${answer.reference}` : ""}).`),
|
|
279
|
+
...(refusal === undefined ? {} : { refusal }),
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
const results = deliveredFiles(answer.files, files.length, WAMID);
|
|
283
|
+
const id = answer.message_id;
|
|
284
|
+
if (status < 200 ||
|
|
285
|
+
status >= 300 ||
|
|
286
|
+
(answer.status !== "sent" && answer.status !== "uncertain") ||
|
|
287
|
+
(id !== null && (typeof id !== "string" || !WAMID.test(id))) ||
|
|
288
|
+
(answer.status === "sent" &&
|
|
289
|
+
(id === null || results?.some((result) => result.status !== "sent") === true)) ||
|
|
290
|
+
(files.length > 0 && results === undefined)) {
|
|
291
|
+
return outcome("uncertain", undefined, {
|
|
292
|
+
messageId: null,
|
|
293
|
+
url: null,
|
|
294
|
+
cause: new Error(`The agent's WhatsApp channel answered with status ${status}.`),
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return answer.status === "sent"
|
|
298
|
+
? outcome("sent", results, { messageId: id, url: null })
|
|
299
|
+
: outcome("uncertain", results, {
|
|
300
|
+
messageId: id,
|
|
301
|
+
url: null,
|
|
302
|
+
cause: new Error("The agent's WhatsApp channel could not prove every message sent" +
|
|
303
|
+
`${typeof answer.reference === "string" ? ` (reference ${answer.reference})` : ""}.`),
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
}
|
package/dist/telegram.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TelegramFormatConverter } from "@chat-adapter/telegram";
|
|
2
|
+
import type { ChannelDeliverFile, ChannelDeliveredFile } from "./channel-files.js";
|
|
2
3
|
/**
|
|
3
4
|
* Private chats and basic groups number messages per account. Supergroup and channel
|
|
4
5
|
* message numbers are shared. Their Bot API peer IDs start below -1000000000000.
|
|
@@ -66,6 +67,88 @@ export declare function telegramStartParam(page: string): string | undefined;
|
|
|
66
67
|
export declare function miniAppLink(url: string, origins: ReadonlySet<string>, botUsername: string): string | undefined;
|
|
67
68
|
/** A username Telegram accepts in a `t.me` link; any other leaves every link as it is. */
|
|
68
69
|
export declare function isTelegramBotUsername(username: string): boolean;
|
|
69
|
-
|
|
70
|
+
/** The most characters Telegram takes in one message's text. */
|
|
71
|
+
export declare const TELEGRAM_MESSAGE_LIMIT = 4096;
|
|
72
|
+
/**
|
|
73
|
+
* Where the agent's Telegram channel `name` delivers a message the ontology sends: a draft a member
|
|
74
|
+
* approved, a reply its review let go, a member's or a service's post. The ontology decides and
|
|
75
|
+
* renders it, its exact text with its attribution, links and previews; the channel sends it
|
|
76
|
+
* through its bot, its parts in order in one operation. Only the deployment's own functions may
|
|
77
|
+
* call it.
|
|
78
|
+
*/
|
|
79
|
+
export declare function telegramDeliverRoute(name: string): string;
|
|
80
|
+
/** One text as `sendMessage` takes it: the text, its format, and its link preview. */
|
|
81
|
+
export interface TelegramDeliverText {
|
|
82
|
+
readonly text: string;
|
|
83
|
+
readonly parse_mode?: "MarkdownV2";
|
|
84
|
+
readonly link_preview_options?: Readonly<Record<string, unknown>>;
|
|
85
|
+
readonly disable_web_page_preview?: boolean;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* One text message of a delivery: as Telegram formats it, and the same text without formatting,
|
|
89
|
+
* which is sent once in its place only when Telegram refuses the formatting.
|
|
90
|
+
*/
|
|
91
|
+
export interface TelegramDeliverPart {
|
|
92
|
+
readonly formatted: TelegramDeliverText;
|
|
93
|
+
readonly plain: TelegramDeliverText;
|
|
94
|
+
}
|
|
95
|
+
/** The most text messages one delivery carries. */
|
|
96
|
+
export declare const TELEGRAM_DELIVER_PARTS = 20;
|
|
97
|
+
/** One message for the agent's Telegram channel to send (`POST` {@link telegramDeliverRoute}). */
|
|
98
|
+
export interface TelegramDeliver {
|
|
99
|
+
/** The ontology's delivery, which the agent's diagnostics name. */
|
|
100
|
+
readonly operation_id: string;
|
|
101
|
+
/**
|
|
102
|
+
* The connection the ontology's `chat` names for the channel: the channel sends only through
|
|
103
|
+
* the same bot, and answers `channel_unknown` otherwise.
|
|
104
|
+
*/
|
|
105
|
+
readonly connection: string;
|
|
106
|
+
/** The chat, by its Bot API id. */
|
|
107
|
+
readonly chat_id: string;
|
|
108
|
+
/** The forum topic to send in (`message_thread_id`); null outside one. */
|
|
109
|
+
readonly thread_id: string | null;
|
|
110
|
+
/** The message each text message answers, which Telegram must quote; null for none. */
|
|
111
|
+
readonly reply_to: string | null;
|
|
112
|
+
/**
|
|
113
|
+
* The text messages, in order, each rendered from a text of at most
|
|
114
|
+
* {@link TELEGRAM_MESSAGE_LIMIT} characters; none for files alone. A single text that fits a
|
|
115
|
+
* caption goes as the caption of files sent as one message.
|
|
116
|
+
*/
|
|
117
|
+
readonly parts: ReadonlyArray<TelegramDeliverPart>;
|
|
118
|
+
/** The buttons under the last text message: Telegram's inline keyboard. */
|
|
119
|
+
readonly reply_markup?: Readonly<Record<string, unknown>>;
|
|
120
|
+
/** The files after the text, in order, each by its link. */
|
|
121
|
+
readonly files?: ReadonlyArray<ChannelDeliverFile>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The agent's answer once it sent something: `sent` when Telegram's answers prove every message,
|
|
125
|
+
* `uncertain` when some may not have reached the person, with what became of each file. A `4xx`
|
|
126
|
+
* with {@link TelegramDeliverRefused} sent nothing (`404` alone: an agent without the route);
|
|
127
|
+
* anything else may have sent it.
|
|
128
|
+
*/
|
|
129
|
+
export interface TelegramDelivered {
|
|
130
|
+
readonly status: "sent" | "uncertain";
|
|
131
|
+
/** The first message that reached the person. */
|
|
132
|
+
readonly message_id: string | null;
|
|
133
|
+
/** That message's link, for a group Telegram links to; null otherwise and with files. */
|
|
134
|
+
readonly url: string | null;
|
|
135
|
+
readonly files?: ReadonlyArray<ChannelDeliveredFile>;
|
|
136
|
+
/** The agent's diagnostic of what did not go, when something did not. */
|
|
137
|
+
readonly reference?: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Why the agent sent nothing (`4xx`): `channel_unknown` when it has no Telegram channel of the
|
|
141
|
+
* name on the connection, so the ontology sends the message itself; `provider_refused` when
|
|
142
|
+
* Telegram refused it; `thread_unavailable` when Telegram no longer has the topic;
|
|
143
|
+
* `connection_refused` when SegmentStream's connection refused the call; or the request's own
|
|
144
|
+
* fault, such as `invalid_request` or `file_unavailable`.
|
|
145
|
+
*/
|
|
146
|
+
export interface TelegramDeliverRefused {
|
|
147
|
+
readonly error: string;
|
|
148
|
+
/** Telegram's refusal for code to act on: a person who blocked the bot or never started it. */
|
|
149
|
+
readonly refusal?: "recipient_unreachable";
|
|
150
|
+
readonly reference?: string;
|
|
151
|
+
}
|
|
152
|
+
export type { ChannelCall, ChannelCallFile, ChannelDeliverFile, ChannelDeliveredFile, ChannelFile, ChannelFileRead, ChannelPart, ChannelReply, ChannelRequest, ChannelStoredFile, DefiniteFailure, } from "./channel-files.js";
|
|
70
153
|
export { CHANNEL_FILE_FRAME_BYTES, ChannelAnswerError } from "./channel-files.js";
|
|
71
154
|
export { sendTelegramFiles, telegramFileBytes, telegramFileRefusal, telegramFileType, telegramFormattingRefused, withCrlfLineBreaks, TELEGRAM_CAPTION_LIMIT, TELEGRAM_DOCUMENT_BYTES, TELEGRAM_FILES_PER_MESSAGE, TELEGRAM_INLINE_FILE_BYTES, TELEGRAM_PHOTO_BYTES, type TelegramEntity, type TelegramFilesTarget, type TelegramFormattedText, } from "./telegram-files.js";
|
package/dist/telegram.js
CHANGED
|
@@ -314,5 +314,19 @@ export function miniAppLink(url, origins, botUsername) {
|
|
|
314
314
|
export function isTelegramBotUsername(username) {
|
|
315
315
|
return /^[A-Za-z0-9_]{1,64}$/u.test(username);
|
|
316
316
|
}
|
|
317
|
+
/** The most characters Telegram takes in one message's text. */
|
|
318
|
+
export const TELEGRAM_MESSAGE_LIMIT = 4096;
|
|
319
|
+
/**
|
|
320
|
+
* Where the agent's Telegram channel `name` delivers a message the ontology sends: a draft a member
|
|
321
|
+
* approved, a reply its review let go, a member's or a service's post. The ontology decides and
|
|
322
|
+
* renders it, its exact text with its attribution, links and previews; the channel sends it
|
|
323
|
+
* through its bot, its parts in order in one operation. Only the deployment's own functions may
|
|
324
|
+
* call it.
|
|
325
|
+
*/
|
|
326
|
+
export function telegramDeliverRoute(name) {
|
|
327
|
+
return `/eve/segmentstream/v1/telegram/${name}/deliver`;
|
|
328
|
+
}
|
|
329
|
+
/** The most text messages one delivery carries. */
|
|
330
|
+
export const TELEGRAM_DELIVER_PARTS = 20;
|
|
317
331
|
export { CHANNEL_FILE_FRAME_BYTES, ChannelAnswerError } from "./channel-files.js";
|
|
318
332
|
export { sendTelegramFiles, telegramFileBytes, telegramFileRefusal, telegramFileType, telegramFormattingRefused, withCrlfLineBreaks, TELEGRAM_CAPTION_LIMIT, TELEGRAM_DOCUMENT_BYTES, TELEGRAM_FILES_PER_MESSAGE, TELEGRAM_INLINE_FILE_BYTES, TELEGRAM_PHOTO_BYTES, } from "./telegram-files.js";
|