@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
package/dist/whatsapp-files.d.ts
CHANGED
|
@@ -33,10 +33,34 @@ export interface WhatsAppLinkedFile {
|
|
|
33
33
|
readonly contentType: string;
|
|
34
34
|
readonly index?: number;
|
|
35
35
|
}
|
|
36
|
+
/** Meta's refusals of a message that code acts on, as `WHATSAPP_REFUSALS` names them by code. */
|
|
37
|
+
export type WhatsAppRefusal = "outside_reply_window" | "recipient_unreachable" | "recipient_opted_out";
|
|
38
|
+
/** Meta's message types: a file's, a text, a text with buttons, or an approved template. */
|
|
39
|
+
export type WhatsAppMessageType = WhatsAppFileType | "text" | "interactive" | "template";
|
|
36
40
|
/** A message WhatsApp accepted, with Meta's type of it and its text or caption. */
|
|
37
41
|
export interface WhatsAppSentPart extends ChannelPart {
|
|
38
|
-
readonly type:
|
|
42
|
+
readonly type: WhatsAppMessageType;
|
|
43
|
+
/** Meta's refusal of a `failed` message, when its code is one that code acts on. */
|
|
44
|
+
readonly refusal?: WhatsAppRefusal;
|
|
39
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* One message to `recipient`, `{ to }` by phone number or `{ recipient }` by business-scoped user
|
|
48
|
+
* id: Meta's `type` and the object of that type, such as `text` and `{ body, preview_url }`, in one
|
|
49
|
+
* `POST /phone/messages` on the connection's `graph` API, never retried; `replyTo` answers a
|
|
50
|
+
* message. It is `sent` when Meta names it; `failed` when Meta refused it with an error code, or
|
|
51
|
+
* when the connection refused the call before relaying it, as `definite` says; and `uncertain`
|
|
52
|
+
* otherwise, as on a 5xx, a lost answer or a success without the message's id.
|
|
53
|
+
*/
|
|
54
|
+
export declare function sendWhatsAppMessage(options: {
|
|
55
|
+
readonly request: ChannelRequest;
|
|
56
|
+
readonly recipient: Readonly<Record<string, string>>;
|
|
57
|
+
readonly type: WhatsAppMessageType;
|
|
58
|
+
readonly message: Readonly<Record<string, unknown>>;
|
|
59
|
+
readonly replyTo?: string;
|
|
60
|
+
readonly definite?: DefiniteFailure;
|
|
61
|
+
/** The files and the text the message carries, as its part names them; a text by default. */
|
|
62
|
+
readonly part?: Pick<ChannelPart, "files" | "text">;
|
|
63
|
+
}): Promise<WhatsAppSentPart>;
|
|
40
64
|
/**
|
|
41
65
|
* Sends one message per file to `recipient`, `{ to }` by phone number or `{ recipient }` by
|
|
42
66
|
* business-scoped user id, `caption` with the first; `replyTo` answers a message with the first.
|
package/dist/whatsapp-files.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ChannelAnswerError, isRecord, jsonCall, replyJson, unrelayed, } from "./channel-files.js";
|
|
2
|
+
import { REFUSALS } from "./service/chat/providers/whatsapp-refusals.js";
|
|
2
3
|
/** The most bytes WhatsApp fetches of a file of each type. */
|
|
3
4
|
export const WHATSAPP_FILE_BYTES = {
|
|
4
5
|
image: 5 * 1024 * 1024,
|
|
@@ -38,6 +39,60 @@ export function whatsappFileRefusal(file) {
|
|
|
38
39
|
}
|
|
39
40
|
return undefined;
|
|
40
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* One message to `recipient`, `{ to }` by phone number or `{ recipient }` by business-scoped user
|
|
44
|
+
* id: Meta's `type` and the object of that type, such as `text` and `{ body, preview_url }`, in one
|
|
45
|
+
* `POST /phone/messages` on the connection's `graph` API, never retried; `replyTo` answers a
|
|
46
|
+
* message. It is `sent` when Meta names it; `failed` when Meta refused it with an error code, or
|
|
47
|
+
* when the connection refused the call before relaying it, as `definite` says; and `uncertain`
|
|
48
|
+
* otherwise, as on a 5xx, a lost answer or a success without the message's id.
|
|
49
|
+
*/
|
|
50
|
+
export async function sendWhatsAppMessage(options) {
|
|
51
|
+
const { type } = options;
|
|
52
|
+
const part = options.part ?? { files: [], text: true };
|
|
53
|
+
let reply;
|
|
54
|
+
try {
|
|
55
|
+
reply = await options.request(jsonCall("graph", "/phone/messages", {
|
|
56
|
+
messaging_product: "whatsapp",
|
|
57
|
+
recipient_type: "individual",
|
|
58
|
+
...options.recipient,
|
|
59
|
+
...(options.replyTo === undefined ? {} : { context: { message_id: options.replyTo } }),
|
|
60
|
+
type,
|
|
61
|
+
[type]: options.message,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return { ...unrelayed(part, error, options.definite), type };
|
|
66
|
+
}
|
|
67
|
+
const answer = replyJson(reply);
|
|
68
|
+
const id = isRecord(answer)
|
|
69
|
+
? isRecord(Array.isArray(answer.messages) ? answer.messages[0] : undefined)
|
|
70
|
+
? answer.messages[0].id
|
|
71
|
+
: undefined
|
|
72
|
+
: undefined;
|
|
73
|
+
if (reply.status >= 200 && reply.status < 300 && typeof id === "string" && WAMID.test(id)) {
|
|
74
|
+
return { ...part, type, status: "sent", messageIds: [id] };
|
|
75
|
+
}
|
|
76
|
+
const error = isRecord(answer) && isRecord(answer.error) ? answer.error : undefined;
|
|
77
|
+
const refused = reply.status >= 400 && reply.status < 500 && Number.isInteger(error?.code);
|
|
78
|
+
const refusal = refused ? REFUSALS[Number(error.code)] : undefined;
|
|
79
|
+
return {
|
|
80
|
+
...part,
|
|
81
|
+
type,
|
|
82
|
+
status: refused ? "failed" : "uncertain",
|
|
83
|
+
messageIds: [],
|
|
84
|
+
code: reply.status === 413
|
|
85
|
+
? "too_large"
|
|
86
|
+
: reply.status === 401 || reply.status === 403
|
|
87
|
+
? "forbidden"
|
|
88
|
+
: reply.status >= 200 && reply.status < 300
|
|
89
|
+
? "invalid_response"
|
|
90
|
+
: "send_failed",
|
|
91
|
+
cause: new ChannelAnswerError(`WhatsApp answered with status ${reply.status}` +
|
|
92
|
+
(Number.isInteger(error?.code) ? ` and code ${String(error.code)}.` : "."), reply.status, reply),
|
|
93
|
+
...(refusal === undefined ? {} : { refusal }),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
41
96
|
/**
|
|
42
97
|
* Sends one message per file to `recipient`, `{ to }` by phone number or `{ recipient }` by
|
|
43
98
|
* business-scoped user id, `caption` with the first; `replyTo` answers a message with the first.
|
|
@@ -51,48 +106,20 @@ export async function sendWhatsAppFiles(options) {
|
|
|
51
106
|
let caption = options.caption === "" ? undefined : options.caption;
|
|
52
107
|
const first = options.files[0];
|
|
53
108
|
async function send(part, type, message) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
messaging_product: "whatsapp",
|
|
58
|
-
recipient_type: "individual",
|
|
59
|
-
...options.recipient,
|
|
60
|
-
...(replyTo === undefined ? {} : { context: { message_id: replyTo } }),
|
|
61
|
-
type,
|
|
62
|
-
[type]: message,
|
|
63
|
-
}));
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
return { ...unrelayed(part, error, options.definite), type };
|
|
67
|
-
}
|
|
68
|
-
replyTo = undefined;
|
|
69
|
-
const answer = replyJson(reply);
|
|
70
|
-
const id = isRecord(answer)
|
|
71
|
-
? isRecord(Array.isArray(answer.messages) ? answer.messages[0] : undefined)
|
|
72
|
-
? answer.messages[0].id
|
|
73
|
-
: undefined
|
|
74
|
-
: undefined;
|
|
75
|
-
if (reply.status >= 200 && reply.status < 300 && typeof id === "string" && WAMID.test(id)) {
|
|
76
|
-
return { ...part, type, status: "sent", messageIds: [id] };
|
|
77
|
-
}
|
|
78
|
-
const error = isRecord(answer) && isRecord(answer.error) ? answer.error : undefined;
|
|
79
|
-
return {
|
|
80
|
-
...part,
|
|
109
|
+
const sent = await sendWhatsAppMessage({
|
|
110
|
+
request: options.request,
|
|
111
|
+
recipient: options.recipient,
|
|
81
112
|
type,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
: "send_failed",
|
|
93
|
-
cause: new ChannelAnswerError(`WhatsApp answered with status ${reply.status}` +
|
|
94
|
-
(Number.isInteger(error?.code) ? ` and code ${String(error.code)}.` : "."), reply.status, reply),
|
|
95
|
-
};
|
|
113
|
+
message,
|
|
114
|
+
...(replyTo === undefined ? {} : { replyTo }),
|
|
115
|
+
...(options.definite === undefined ? {} : { definite: options.definite }),
|
|
116
|
+
part,
|
|
117
|
+
});
|
|
118
|
+
// A message Meta answered, whatever it answered, carried the reply: the next ones do not.
|
|
119
|
+
if (sent.cause === undefined || sent.cause instanceof ChannelAnswerError) {
|
|
120
|
+
replyTo = undefined;
|
|
121
|
+
}
|
|
122
|
+
return sent;
|
|
96
123
|
}
|
|
97
124
|
if (caption !== undefined &&
|
|
98
125
|
(caption.length > WHATSAPP_CAPTION_LIMIT ||
|
package/dist/whatsapp.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { ChannelDeliverFile, ChannelDeliveredFile } from "./channel-files.js";
|
|
2
|
+
import type { WhatsAppRefusal } from "./whatsapp-files.js";
|
|
1
3
|
/**
|
|
2
|
-
* WhatsApp's text for an exact chat post. The ontology service
|
|
4
|
+
* WhatsApp's text for an exact chat post. The ontology service renders it and the agent's
|
|
3
5
|
* confirmation computes it from the same function, so the digest the member approves and the
|
|
4
6
|
* text delivered are the same.
|
|
5
7
|
*/
|
|
@@ -18,6 +20,84 @@ export declare function renderWhatsApp(text: string, attribution: string, separa
|
|
|
18
20
|
* status's refusal in the message's `failure` with the same code the ontology keeps a send's with.
|
|
19
21
|
*/
|
|
20
22
|
export { REFUSALS as WHATSAPP_REFUSALS } from "./service/chat/providers/whatsapp-refusals.js";
|
|
21
|
-
|
|
23
|
+
/** The most files one message the ontology sends carries, each a WhatsApp message of its own. */
|
|
24
|
+
export declare const WHATSAPP_DELIVER_FILES = 10;
|
|
25
|
+
/**
|
|
26
|
+
* Where the agent's WhatsApp channel `name` delivers a message the ontology sends: a draft a member
|
|
27
|
+
* approved, a reply its review let go, a member's or a service's post. The ontology decides and
|
|
28
|
+
* renders it, its exact text with its attribution, its buttons or its approved template with the
|
|
29
|
+
* values; the channel sends it from its number as given, its messages in order in one operation.
|
|
30
|
+
* Only the deployment's own functions may call it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function whatsappDeliverRoute(name: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* One message as WhatsApp's Cloud API takes it, `type` naming the field that holds it: a text
|
|
35
|
+
* (`{ body, preview_url }`), a text with its buttons (`interactive`: reply buttons or one link), or
|
|
36
|
+
* an approved template with its values (`{ name, language: { code }, components? }`).
|
|
37
|
+
*/
|
|
38
|
+
export type WhatsAppDeliverMessage = {
|
|
39
|
+
readonly type: "text";
|
|
40
|
+
readonly text: {
|
|
41
|
+
readonly body: string;
|
|
42
|
+
readonly preview_url: boolean;
|
|
43
|
+
};
|
|
44
|
+
} | {
|
|
45
|
+
readonly type: "interactive";
|
|
46
|
+
readonly interactive: Readonly<Record<string, unknown>>;
|
|
47
|
+
} | {
|
|
48
|
+
readonly type: "template";
|
|
49
|
+
readonly template: Readonly<Record<string, unknown>>;
|
|
50
|
+
};
|
|
51
|
+
/** One message for the agent's WhatsApp channel to send (`POST` {@link whatsappDeliverRoute}). */
|
|
52
|
+
export interface WhatsAppDeliver {
|
|
53
|
+
/** The ontology's delivery, which the agent's diagnostics name. */
|
|
54
|
+
readonly operation_id: string;
|
|
55
|
+
/**
|
|
56
|
+
* The connection the ontology's `chat` names for the channel: the channel sends only from the
|
|
57
|
+
* same number, and answers `channel_unknown` otherwise.
|
|
58
|
+
*/
|
|
59
|
+
readonly connection: string;
|
|
60
|
+
/** The person, by their WhatsApp id: their number, digits only. */
|
|
61
|
+
readonly to: string;
|
|
62
|
+
/** The message, for one without files. */
|
|
63
|
+
readonly message?: WhatsAppDeliverMessage;
|
|
64
|
+
/**
|
|
65
|
+
* The text of a message with files: the first file's caption, or a text message of its own
|
|
66
|
+
* before them when it is longer than a caption or the first file is audio, which takes none.
|
|
67
|
+
*/
|
|
68
|
+
readonly caption?: string;
|
|
69
|
+
/**
|
|
70
|
+
* The files, in order, each a message of its own that WhatsApp fetches by the file's link, at
|
|
71
|
+
* most {@link WHATSAPP_DELIVER_FILES}.
|
|
72
|
+
*/
|
|
73
|
+
readonly files?: ReadonlyArray<ChannelDeliverFile>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The agent's answer once it sent something: `sent` when Meta named every message, `uncertain` when
|
|
77
|
+
* some may not have reached the person, with what became of each file. A `4xx` with
|
|
78
|
+
* {@link WhatsAppDeliverRefused} sent nothing (`404` alone: an agent without the route); anything
|
|
79
|
+
* else may have sent it.
|
|
80
|
+
*/
|
|
81
|
+
export interface WhatsAppDelivered {
|
|
82
|
+
readonly status: "sent" | "uncertain";
|
|
83
|
+
/** The first message that reached the person, by Meta's id of it (`wamid.…`). */
|
|
84
|
+
readonly message_id: string | null;
|
|
85
|
+
readonly files?: ReadonlyArray<ChannelDeliveredFile>;
|
|
86
|
+
/** The agent's diagnostic of what did not go, when something did not. */
|
|
87
|
+
readonly reference?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Why the agent sent nothing (`4xx`): `channel_unknown` when it has no WhatsApp channel of the name
|
|
91
|
+
* on the connection, so the ontology sends the message itself; `provider_refused` when Meta refused
|
|
92
|
+
* it; `connection_refused` when SegmentStream's connection refused the call; or the request's own
|
|
93
|
+
* fault, such as `invalid_request` or `file_too_large`.
|
|
94
|
+
*/
|
|
95
|
+
export interface WhatsAppDeliverRefused {
|
|
96
|
+
readonly error: string;
|
|
97
|
+
/** Meta's refusal of a message without files, for code to act on, by `WHATSAPP_REFUSALS`. */
|
|
98
|
+
readonly refusal?: WhatsAppRefusal;
|
|
99
|
+
readonly reference?: string;
|
|
100
|
+
}
|
|
101
|
+
export type { ChannelCall, ChannelDeliverFile, ChannelDeliveredFile, ChannelPart, ChannelReply, ChannelRequest, DefiniteFailure, } from "./channel-files.js";
|
|
22
102
|
export { ChannelAnswerError } from "./channel-files.js";
|
|
23
|
-
export { sendWhatsAppFiles, whatsappFileRefusal, whatsappFileType, WHATSAPP_CAPTION_LIMIT, WHATSAPP_FILE_BYTES, type WhatsAppFileType, type WhatsAppLinkedFile, type WhatsAppSentPart, } from "./whatsapp-files.js";
|
|
103
|
+
export { sendWhatsAppFiles, sendWhatsAppMessage, whatsappFileRefusal, whatsappFileType, WHATSAPP_CAPTION_LIMIT, WHATSAPP_FILE_BYTES, type WhatsAppFileType, type WhatsAppLinkedFile, type WhatsAppMessageType, type WhatsAppRefusal, type WhatsAppSentPart, } from "./whatsapp-files.js";
|
package/dist/whatsapp.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* WhatsApp's text for an exact chat post. The ontology service
|
|
2
|
+
* WhatsApp's text for an exact chat post. The ontology service renders it and the agent's
|
|
3
3
|
* confirmation computes it from the same function, so the digest the member approves and the
|
|
4
4
|
* text delivered are the same.
|
|
5
5
|
*/
|
|
@@ -35,5 +35,17 @@ export function renderWhatsApp(text, attribution, separator = "\n\n") {
|
|
|
35
35
|
* status's refusal in the message's `failure` with the same code the ontology keeps a send's with.
|
|
36
36
|
*/
|
|
37
37
|
export { REFUSALS as WHATSAPP_REFUSALS } from "./service/chat/providers/whatsapp-refusals.js";
|
|
38
|
+
/** The most files one message the ontology sends carries, each a WhatsApp message of its own. */
|
|
39
|
+
export const WHATSAPP_DELIVER_FILES = 10;
|
|
40
|
+
/**
|
|
41
|
+
* Where the agent's WhatsApp channel `name` delivers a message the ontology sends: a draft a member
|
|
42
|
+
* approved, a reply its review let go, a member's or a service's post. The ontology decides and
|
|
43
|
+
* renders it, its exact text with its attribution, its buttons or its approved template with the
|
|
44
|
+
* values; the channel sends it from its number as given, its messages in order in one operation.
|
|
45
|
+
* Only the deployment's own functions may call it.
|
|
46
|
+
*/
|
|
47
|
+
export function whatsappDeliverRoute(name) {
|
|
48
|
+
return `/eve/segmentstream/v1/whatsapp/${name}/deliver`;
|
|
49
|
+
}
|
|
38
50
|
export { ChannelAnswerError } from "./channel-files.js";
|
|
39
|
-
export { sendWhatsAppFiles, whatsappFileRefusal, whatsappFileType, WHATSAPP_CAPTION_LIMIT, WHATSAPP_FILE_BYTES, } from "./whatsapp-files.js";
|
|
51
|
+
export { sendWhatsAppFiles, sendWhatsAppMessage, whatsappFileRefusal, whatsappFileType, WHATSAPP_CAPTION_LIMIT, WHATSAPP_FILE_BYTES, } from "./whatsapp-files.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.185.0",
|
|
4
4
|
"description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|