@decaf-ts/integrations 0.12.0 → 0.14.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/dist/integrations.cjs +1 -1
- package/lib/cjs/calendar/environment.cjs +48 -0
- package/lib/cjs/calendar/environment.cjs.map +1 -0
- package/lib/cjs/calendar/google-calendar/GoogleCalendarService.cjs +338 -0
- package/lib/cjs/calendar/google-calendar/GoogleCalendarService.cjs.map +1 -0
- package/lib/cjs/calendar/google-calendar/index.cjs +20 -0
- package/lib/cjs/calendar/google-calendar/index.cjs.map +1 -0
- package/lib/cjs/comms/discord/DiscordService.cjs +318 -0
- package/lib/cjs/comms/discord/DiscordService.cjs.map +1 -0
- package/lib/cjs/comms/discord/index.cjs +19 -0
- package/lib/cjs/comms/discord/index.cjs.map +1 -0
- package/lib/cjs/comms/environment.cjs +16 -0
- package/lib/cjs/comms/environment.cjs.map +1 -1
- package/lib/cjs/comms/index.cjs +3 -0
- package/lib/cjs/comms/index.cjs.map +1 -1
- package/lib/cjs/comms/outlook/MicrosoftOutlookService.cjs +325 -0
- package/lib/cjs/comms/outlook/MicrosoftOutlookService.cjs.map +1 -0
- package/lib/cjs/comms/outlook/index.cjs +19 -0
- package/lib/cjs/comms/outlook/index.cjs.map +1 -0
- package/lib/cjs/comms/whatsapp/WhatsAppService.cjs +274 -0
- package/lib/cjs/comms/whatsapp/WhatsAppService.cjs.map +1 -0
- package/lib/cjs/comms/whatsapp/index.cjs +19 -0
- package/lib/cjs/comms/whatsapp/index.cjs.map +1 -0
- package/lib/cjs/index.cjs +3 -3
- package/lib/cjs/io/confluence/ConfluenceService.cjs +317 -0
- package/lib/cjs/io/confluence/ConfluenceService.cjs.map +1 -0
- package/lib/cjs/io/confluence/index.cjs +19 -0
- package/lib/cjs/io/confluence/index.cjs.map +1 -0
- package/lib/cjs/io/environment.cjs +28 -0
- package/lib/cjs/io/environment.cjs.map +1 -1
- package/lib/cjs/io/google-docs/GoogleDocsService.cjs +275 -0
- package/lib/cjs/io/google-docs/GoogleDocsService.cjs.map +1 -0
- package/lib/cjs/io/google-docs/index.cjs +19 -0
- package/lib/cjs/io/google-docs/index.cjs.map +1 -0
- package/lib/cjs/io/index.cjs +5 -0
- package/lib/cjs/io/index.cjs.map +1 -1
- package/lib/cjs/io/jira/JiraService.cjs +354 -0
- package/lib/cjs/io/jira/JiraService.cjs.map +1 -0
- package/lib/cjs/io/jira/index.cjs +19 -0
- package/lib/cjs/io/jira/index.cjs.map +1 -0
- package/lib/cjs/io/microsoft-excel/MicrosoftExcelService.cjs +253 -0
- package/lib/cjs/io/microsoft-excel/MicrosoftExcelService.cjs.map +1 -0
- package/lib/cjs/io/microsoft-excel/index.cjs +19 -0
- package/lib/cjs/io/microsoft-excel/index.cjs.map +1 -0
- package/lib/cjs/io/microsoft-sharepoint/MicrosoftSharePointService.cjs +261 -0
- package/lib/cjs/io/microsoft-sharepoint/MicrosoftSharePointService.cjs.map +1 -0
- package/lib/cjs/io/microsoft-sharepoint/index.cjs +19 -0
- package/lib/cjs/io/microsoft-sharepoint/index.cjs.map +1 -0
- package/lib/esm/calendar/environment.js +44 -0
- package/lib/esm/calendar/environment.js.map +1 -0
- package/lib/esm/calendar/google-calendar/GoogleCalendarService.js +333 -0
- package/lib/esm/calendar/google-calendar/GoogleCalendarService.js.map +1 -0
- package/lib/esm/calendar/google-calendar/index.js +3 -0
- package/lib/esm/calendar/google-calendar/index.js.map +1 -0
- package/lib/esm/comms/discord/DiscordService.js +313 -0
- package/lib/esm/comms/discord/DiscordService.js.map +1 -0
- package/lib/esm/comms/discord/index.js +2 -0
- package/lib/esm/comms/discord/index.js.map +1 -0
- package/lib/esm/comms/environment.js +16 -0
- package/lib/esm/comms/environment.js.map +1 -1
- package/lib/esm/comms/index.js +3 -0
- package/lib/esm/comms/index.js.map +1 -1
- package/lib/esm/comms/outlook/MicrosoftOutlookService.js +320 -0
- package/lib/esm/comms/outlook/MicrosoftOutlookService.js.map +1 -0
- package/lib/esm/comms/outlook/index.js +2 -0
- package/lib/esm/comms/outlook/index.js.map +1 -0
- package/lib/esm/comms/whatsapp/WhatsAppService.js +269 -0
- package/lib/esm/comms/whatsapp/WhatsAppService.js.map +1 -0
- package/lib/esm/comms/whatsapp/index.js +2 -0
- package/lib/esm/comms/whatsapp/index.js.map +1 -0
- package/lib/esm/index.js +3 -3
- package/lib/esm/io/confluence/ConfluenceService.js +312 -0
- package/lib/esm/io/confluence/ConfluenceService.js.map +1 -0
- package/lib/esm/io/confluence/index.js +2 -0
- package/lib/esm/io/confluence/index.js.map +1 -0
- package/lib/esm/io/environment.js +28 -0
- package/lib/esm/io/environment.js.map +1 -1
- package/lib/esm/io/google-docs/GoogleDocsService.js +270 -0
- package/lib/esm/io/google-docs/GoogleDocsService.js.map +1 -0
- package/lib/esm/io/google-docs/index.js +2 -0
- package/lib/esm/io/google-docs/index.js.map +1 -0
- package/lib/esm/io/index.js +5 -0
- package/lib/esm/io/index.js.map +1 -1
- package/lib/esm/io/jira/JiraService.js +349 -0
- package/lib/esm/io/jira/JiraService.js.map +1 -0
- package/lib/esm/io/jira/index.js +2 -0
- package/lib/esm/io/jira/index.js.map +1 -0
- package/lib/esm/io/microsoft-excel/MicrosoftExcelService.js +248 -0
- package/lib/esm/io/microsoft-excel/MicrosoftExcelService.js.map +1 -0
- package/lib/esm/io/microsoft-excel/index.js +2 -0
- package/lib/esm/io/microsoft-excel/index.js.map +1 -0
- package/lib/esm/io/microsoft-sharepoint/MicrosoftSharePointService.js +256 -0
- package/lib/esm/io/microsoft-sharepoint/MicrosoftSharePointService.js.map +1 -0
- package/lib/esm/io/microsoft-sharepoint/index.js +2 -0
- package/lib/esm/io/microsoft-sharepoint/index.js.map +1 -0
- package/lib/types/calendar/environment.d.cts +48 -0
- package/lib/types/calendar/environment.d.mts +48 -0
- package/lib/types/calendar/google-calendar/GoogleCalendarService.d.cts +316 -0
- package/lib/types/calendar/google-calendar/GoogleCalendarService.d.mts +316 -0
- package/lib/types/calendar/google-calendar/index.d.cts +2 -0
- package/lib/types/calendar/google-calendar/index.d.mts +2 -0
- package/lib/types/comms/discord/DiscordService.d.cts +285 -0
- package/lib/types/comms/discord/DiscordService.d.mts +285 -0
- package/lib/types/comms/discord/index.d.cts +1 -0
- package/lib/types/comms/discord/index.d.mts +1 -0
- package/lib/types/comms/environment.d.cts +34 -8
- package/lib/types/comms/environment.d.mts +34 -8
- package/lib/types/comms/index.d.cts +3 -0
- package/lib/types/comms/index.d.mts +3 -0
- package/lib/types/comms/outlook/MicrosoftOutlookService.d.cts +282 -0
- package/lib/types/comms/outlook/MicrosoftOutlookService.d.mts +282 -0
- package/lib/types/comms/outlook/index.d.cts +1 -0
- package/lib/types/comms/outlook/index.d.mts +1 -0
- package/lib/types/comms/whatsapp/WhatsAppService.d.cts +258 -0
- package/lib/types/comms/whatsapp/WhatsAppService.d.mts +258 -0
- package/lib/types/comms/whatsapp/index.d.cts +1 -0
- package/lib/types/comms/whatsapp/index.d.mts +1 -0
- package/lib/types/index.d.cts +2 -2
- package/lib/types/index.d.mts +2 -2
- package/lib/types/io/confluence/ConfluenceService.d.cts +250 -0
- package/lib/types/io/confluence/ConfluenceService.d.mts +250 -0
- package/lib/types/io/confluence/index.d.cts +1 -0
- package/lib/types/io/confluence/index.d.mts +1 -0
- package/lib/types/io/environment.d.cts +52 -12
- package/lib/types/io/environment.d.mts +52 -12
- package/lib/types/io/google-docs/GoogleDocsService.d.cts +188 -0
- package/lib/types/io/google-docs/GoogleDocsService.d.mts +188 -0
- package/lib/types/io/google-docs/index.d.cts +1 -0
- package/lib/types/io/google-docs/index.d.mts +1 -0
- package/lib/types/io/index.d.cts +5 -0
- package/lib/types/io/index.d.mts +5 -0
- package/lib/types/io/jira/JiraService.d.cts +300 -0
- package/lib/types/io/jira/JiraService.d.mts +300 -0
- package/lib/types/io/jira/index.d.cts +1 -0
- package/lib/types/io/jira/index.d.mts +1 -0
- package/lib/types/io/microsoft-excel/MicrosoftExcelService.d.cts +230 -0
- package/lib/types/io/microsoft-excel/MicrosoftExcelService.d.mts +230 -0
- package/lib/types/io/microsoft-excel/index.d.cts +1 -0
- package/lib/types/io/microsoft-excel/index.d.mts +1 -0
- package/lib/types/io/microsoft-sharepoint/MicrosoftSharePointService.d.cts +226 -0
- package/lib/types/io/microsoft-sharepoint/MicrosoftSharePointService.d.mts +226 -0
- package/lib/types/io/microsoft-sharepoint/index.d.cts +1 -0
- package/lib/types/io/microsoft-sharepoint/index.d.mts +1 -0
- package/package.json +101 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../../../src/calendar/environment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AA0BtD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,iBAAiB,CAAC,UAAU,CAAC;IAC9D,QAAQ,EAAE;QACR,cAAc,EAAE;YACd,KAAK,EAAE,EAAE;YACT,MAAM,EAAE,EAAE;YACV,UAAU,EAAE,EAAE;YACd,OAAO,EAAE,EAAE;YACX,OAAO,EAAE,SAA8B;SACxC;KACF;CAC0B,CAAC,CAAC"}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module integrations/calendar/google-calendar/service
|
|
3
|
+
* @summary Google Calendar API integration service for calendars and events.
|
|
4
|
+
* @description Exposes Google Calendar `v3` operations (calendar list, event
|
|
5
|
+
* list/get/create/update/delete) as an undecorated client-based service built
|
|
6
|
+
* on {@link HttpApiService}. Configuration comes from an explicit
|
|
7
|
+
* `GoogleCalendarServiceConfig` or the `calendar.googleCalendar` environment
|
|
8
|
+
* slice. An OAuth access token is sent as a bearer token when present; an API
|
|
9
|
+
* key is added as the `key` query parameter when no token is available. A
|
|
10
|
+
* default `calendarId` may be configured and falls back to `"primary"`.
|
|
11
|
+
* Credentials are referenced from configuration (and, from as-graph nodes, from
|
|
12
|
+
* the resolved `google-calendar` credential secret) and are never embedded in
|
|
13
|
+
* source. Failures are mapped to decaf errors via {@link httpError}.
|
|
14
|
+
*/
|
|
15
|
+
import { InternalError } from "@decaf-ts/db-decorators";
|
|
16
|
+
import { CalendarEnvironment } from "./../environment.js";
|
|
17
|
+
import { envNumber, envString } from "./../../shared/environmentValue.js";
|
|
18
|
+
import { HttpApiService, httpError, } from "./../../shared/http/index.js";
|
|
19
|
+
/**
|
|
20
|
+
* @description Google Calendar API integration service
|
|
21
|
+
* @summary An undecorated HTTP service for listing calendars and events and
|
|
22
|
+
* for creating, reading, updating, and deleting events. Every operation needs
|
|
23
|
+
* either an OAuth token or an API key; missing credentials or identifiers raise
|
|
24
|
+
* `InternalError`, and transport failures are mapped to decaf errors via
|
|
25
|
+
* {@link httpError}.
|
|
26
|
+
* @class GoogleCalendarService
|
|
27
|
+
* @extends HttpApiService
|
|
28
|
+
* @category Calendar
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { GoogleCalendarService } from "@decaf-ts/integrations/calendar/google-calendar";
|
|
32
|
+
*
|
|
33
|
+
* const calendar = new GoogleCalendarService();
|
|
34
|
+
* await calendar.initialize({
|
|
35
|
+
* token: process.env.GRAPH_CREDENTIAL_GOOGLE_CALENDAR_TOKEN,
|
|
36
|
+
* calendarId: "primary",
|
|
37
|
+
* });
|
|
38
|
+
* await calendar.listEvents({ timeMin: new Date().toISOString() });
|
|
39
|
+
* ```
|
|
40
|
+
* @mermaid
|
|
41
|
+
* sequenceDiagram
|
|
42
|
+
* autonumber
|
|
43
|
+
* participant Caller
|
|
44
|
+
* participant Service as GoogleCalendarService
|
|
45
|
+
* participant API as Google Calendar API
|
|
46
|
+
* Caller->>Service: createEvent(input)
|
|
47
|
+
* activate Service
|
|
48
|
+
* Service->>Service: requireAuth()
|
|
49
|
+
* Service->>API: POST /calendars/{id}/events
|
|
50
|
+
* activate API
|
|
51
|
+
* API-->>Service: created event
|
|
52
|
+
* deactivate API
|
|
53
|
+
* Service-->>Caller: created event
|
|
54
|
+
* deactivate Service
|
|
55
|
+
* @memberOf module:integrations/calendar/google-calendar/service
|
|
56
|
+
*/
|
|
57
|
+
export class GoogleCalendarService extends HttpApiService {
|
|
58
|
+
constructor() {
|
|
59
|
+
super(...arguments);
|
|
60
|
+
/**
|
|
61
|
+
* @description The provider name used in error messages
|
|
62
|
+
*/
|
|
63
|
+
this.serviceName = "Google Calendar";
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @description The Google Calendar API base URL
|
|
67
|
+
* @summary Used when the resolved config carries no `baseUrl`.
|
|
68
|
+
* @returns {string} `"https://www.googleapis.com/calendar/v3"`.
|
|
69
|
+
*/
|
|
70
|
+
defaultBaseUrl() {
|
|
71
|
+
return "https://www.googleapis.com/calendar/v3";
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @description Builds the Google Calendar authorization headers
|
|
75
|
+
* @summary Returns `Authorization: Bearer <token>` when an OAuth token is
|
|
76
|
+
* configured, and no headers otherwise (API keys travel as query parameters).
|
|
77
|
+
* @param {GoogleCalendarServiceConfig} config - The resolved service configuration.
|
|
78
|
+
* @returns {Record<string, string>} The authorization headers (empty without a token).
|
|
79
|
+
*/
|
|
80
|
+
authHeaders(config) {
|
|
81
|
+
if (config.token)
|
|
82
|
+
return { Authorization: `Bearer ${config.token}` };
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @description Reads the Google Calendar credentials and defaults from the environment
|
|
87
|
+
* @summary Reads the `calendar.googleCalendar` slice and returns a partial
|
|
88
|
+
* config when a token or API key is configured, `undefined` otherwise. Values
|
|
89
|
+
* pass through `envString`/`envNumber` to reject unset-leaf proxies.
|
|
90
|
+
* @returns {Partial<GoogleCalendarServiceConfig> | undefined} A partial config, or `undefined` when no credential is configured.
|
|
91
|
+
*/
|
|
92
|
+
configFromEnvironment() {
|
|
93
|
+
const env = CalendarEnvironment.calendar.googleCalendar;
|
|
94
|
+
const token = envString(env?.token);
|
|
95
|
+
const apiKey = envString(env?.apiKey);
|
|
96
|
+
if (!token && !apiKey)
|
|
97
|
+
return undefined;
|
|
98
|
+
return {
|
|
99
|
+
token,
|
|
100
|
+
apiKey,
|
|
101
|
+
calendarId: envString(env.calendarId),
|
|
102
|
+
baseUrl: envString(env.baseUrl),
|
|
103
|
+
timeout: envNumber(env.timeout),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @description Asserts that an OAuth token or API key is configured
|
|
108
|
+
* @summary Called by every operation before it touches the network.
|
|
109
|
+
* @returns {void}
|
|
110
|
+
* @throws {InternalError} When neither a token nor an API key is configured.
|
|
111
|
+
*/
|
|
112
|
+
requireAuth() {
|
|
113
|
+
if (!this.config.token && !this.config.apiKey) {
|
|
114
|
+
throw new InternalError("Google Calendar requires an OAuth token or an API key");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @description Builds API-key query parameters when no OAuth token is set
|
|
119
|
+
* @summary Returns `{ key }` for API-key authentication, and an empty object
|
|
120
|
+
* when an OAuth token is used (or no API key is configured).
|
|
121
|
+
* @returns {Record<string, string>} The query parameters carrying the API key, if any.
|
|
122
|
+
*/
|
|
123
|
+
authParams() {
|
|
124
|
+
return this.config.token || !this.config.apiKey
|
|
125
|
+
? {}
|
|
126
|
+
: { key: this.config.apiKey };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @description Builds the encoded events path for a calendar
|
|
130
|
+
* @summary Resolves the calendar ID from the argument, the configured
|
|
131
|
+
* default, or `"primary"`, then encodes it into
|
|
132
|
+
* `/calendars/<id>/events`.
|
|
133
|
+
* @param {string} [calendarId] - Optional per-call calendar ID.
|
|
134
|
+
* @returns {string} The calendar events API path.
|
|
135
|
+
*/
|
|
136
|
+
calendarPath(calendarId) {
|
|
137
|
+
const id = calendarId ?? this.config.calendarId ?? "primary";
|
|
138
|
+
return `/calendars/${encodeURIComponent(id)}/events`;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @description Builds the Google Calendar start/end object
|
|
142
|
+
* @summary Emits `{ date }` for all-day values and `{ dateTime, timeZone? }`
|
|
143
|
+
* for values containing a `T` separator.
|
|
144
|
+
* @param {string} value - The date or date-time value.
|
|
145
|
+
* @param {string} [timeZone] - Optional IANA time zone for date-time values.
|
|
146
|
+
* @returns {Record<string, string>} The Calendar API time object.
|
|
147
|
+
*/
|
|
148
|
+
eventTime(value, timeZone) {
|
|
149
|
+
if (value.includes("T")) {
|
|
150
|
+
return { dateTime: value, ...(timeZone ? { timeZone } : {}) };
|
|
151
|
+
}
|
|
152
|
+
return { date: value };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* @description Lists the calendars visible to the authenticated user
|
|
156
|
+
* @summary `GET`s `/users/me/calendarList` and returns the calendar list.
|
|
157
|
+
* @returns {Promise<unknown>} The calendar list payload.
|
|
158
|
+
* @throws {InternalError} When no credential is configured.
|
|
159
|
+
* @throws {BadRequestError} When Google rejects the request.
|
|
160
|
+
*/
|
|
161
|
+
async listCalendars() {
|
|
162
|
+
this.requireAuth();
|
|
163
|
+
try {
|
|
164
|
+
const response = await this.client.get("/users/me/calendarList", {
|
|
165
|
+
params: this.authParams(),
|
|
166
|
+
});
|
|
167
|
+
return response.data;
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
throw httpError("Google Calendar", "list calendars", error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @description Lists events in a calendar
|
|
175
|
+
* @summary `GET`s the calendar's events applying the optional time window,
|
|
176
|
+
* result cap, free-text query, and single-events flag.
|
|
177
|
+
* @param {GoogleCalendarListEventsInput} [input] - The calendar and event query options.
|
|
178
|
+
* @returns {Promise<unknown>} The event collection payload.
|
|
179
|
+
* @throws {InternalError} When no credential is configured.
|
|
180
|
+
* @throws {NotFoundError} When the calendar does not exist (HTTP 404).
|
|
181
|
+
* @throws {BadRequestError} When Google rejects the query.
|
|
182
|
+
*/
|
|
183
|
+
async listEvents(input = {}) {
|
|
184
|
+
this.requireAuth();
|
|
185
|
+
try {
|
|
186
|
+
const response = await this.client.get(this.calendarPath(input.calendarId), {
|
|
187
|
+
params: {
|
|
188
|
+
...this.authParams(),
|
|
189
|
+
...(input.timeMin ? { timeMin: input.timeMin } : {}),
|
|
190
|
+
...(input.timeMax ? { timeMax: input.timeMax } : {}),
|
|
191
|
+
...(input.maxResults !== undefined
|
|
192
|
+
? { maxResults: input.maxResults }
|
|
193
|
+
: {}),
|
|
194
|
+
...(input.q ? { q: input.q } : {}),
|
|
195
|
+
...(input.singleEvents !== undefined
|
|
196
|
+
? { singleEvents: input.singleEvents }
|
|
197
|
+
: {}),
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
return response.data;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
throw httpError("Google Calendar", "list events", error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @description Creates a Google Calendar event
|
|
208
|
+
* @summary `POST`s the event to the resolved calendar; start/end values are
|
|
209
|
+
* normalized to Calendar time objects and attendees are mapped to
|
|
210
|
+
* `{ email }` entries.
|
|
211
|
+
* @param {GoogleCalendarEventInput} input - The event definition.
|
|
212
|
+
* @returns {Promise<unknown>} The created event payload.
|
|
213
|
+
* @throws {InternalError} When no credential is configured, or the summary, start, or end is missing.
|
|
214
|
+
* @throws {NotFoundError} When the calendar does not exist (HTTP 404).
|
|
215
|
+
* @throws {BadRequestError} When Google rejects the event.
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* await calendar.createEvent({
|
|
219
|
+
* summary: "Release review",
|
|
220
|
+
* start: "2026-11-01T09:00:00Z",
|
|
221
|
+
* end: "2026-11-01T10:00:00Z",
|
|
222
|
+
* attendees: ["team@example.com"],
|
|
223
|
+
* });
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
async createEvent(input) {
|
|
227
|
+
this.requireAuth();
|
|
228
|
+
if (!input.summary || !input.start || !input.end) {
|
|
229
|
+
throw new InternalError("Google Calendar createEvent requires summary, start, and end");
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
const response = await this.client.post(this.calendarPath(input.calendarId), {
|
|
233
|
+
summary: input.summary,
|
|
234
|
+
...(input.description ? { description: input.description } : {}),
|
|
235
|
+
...(input.location ? { location: input.location } : {}),
|
|
236
|
+
start: this.eventTime(input.start, input.timeZone),
|
|
237
|
+
end: this.eventTime(input.end, input.timeZone),
|
|
238
|
+
...(input.attendees?.length
|
|
239
|
+
? { attendees: input.attendees.map((email) => ({ email })) }
|
|
240
|
+
: {}),
|
|
241
|
+
}, { params: this.authParams() });
|
|
242
|
+
return response.data;
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
throw httpError("Google Calendar", "create event", error);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @description Fetches a single Google Calendar event
|
|
250
|
+
* @summary `GET`s the event from the given calendar.
|
|
251
|
+
* @param {string} calendarId - The calendar ID (empty falls back to the configured value or `primary`).
|
|
252
|
+
* @param {string} eventId - The event ID.
|
|
253
|
+
* @returns {Promise<unknown>} The event payload.
|
|
254
|
+
* @throws {InternalError} When no credential is configured or the event ID is empty.
|
|
255
|
+
* @throws {NotFoundError} When the event does not exist (HTTP 404).
|
|
256
|
+
* @throws {BadRequestError} When Google rejects the request.
|
|
257
|
+
*/
|
|
258
|
+
async getEvent(calendarId, eventId) {
|
|
259
|
+
this.requireAuth();
|
|
260
|
+
if (!eventId) {
|
|
261
|
+
throw new InternalError("Google Calendar requires an event id");
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
const response = await this.client.get(`${this.calendarPath(calendarId)}/${encodeURIComponent(eventId)}`, { params: this.authParams() });
|
|
265
|
+
return response.data;
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
throw httpError("Google Calendar", "get event", error);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* @description Partially updates a Google Calendar event
|
|
273
|
+
* @summary `PATCH`es only the supplied fields on the event, normalizing
|
|
274
|
+
* start/end and attendee values; omitted fields are left unchanged.
|
|
275
|
+
* @param {string} eventId - The event ID to update.
|
|
276
|
+
* @param {GoogleCalendarEventUpdate} input - The fields to change.
|
|
277
|
+
* @returns {Promise<unknown>} The updated event payload.
|
|
278
|
+
* @throws {InternalError} When no credential is configured or the event ID is empty.
|
|
279
|
+
* @throws {NotFoundError} When the event does not exist (HTTP 404).
|
|
280
|
+
* @throws {BadRequestError} When Google rejects the update.
|
|
281
|
+
*/
|
|
282
|
+
async updateEvent(eventId, input) {
|
|
283
|
+
this.requireAuth();
|
|
284
|
+
if (!eventId) {
|
|
285
|
+
throw new InternalError("Google Calendar requires an event id");
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
const response = await this.client.patch(`${this.calendarPath(input.calendarId)}/${encodeURIComponent(eventId)}`, {
|
|
289
|
+
...(input.summary ? { summary: input.summary } : {}),
|
|
290
|
+
...(input.description ? { description: input.description } : {}),
|
|
291
|
+
...(input.location ? { location: input.location } : {}),
|
|
292
|
+
...(input.start
|
|
293
|
+
? { start: this.eventTime(input.start, input.timeZone) }
|
|
294
|
+
: {}),
|
|
295
|
+
...(input.end
|
|
296
|
+
? { end: this.eventTime(input.end, input.timeZone) }
|
|
297
|
+
: {}),
|
|
298
|
+
...(input.attendees?.length
|
|
299
|
+
? { attendees: input.attendees.map((email) => ({ email })) }
|
|
300
|
+
: {}),
|
|
301
|
+
}, { params: this.authParams() });
|
|
302
|
+
return response.data;
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
throw httpError("Google Calendar", "update event", error);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* @description Deletes a Google Calendar event
|
|
310
|
+
* @summary `DELETE`s the event from the given calendar and returns
|
|
311
|
+
* `{ deleted: true, eventId }`.
|
|
312
|
+
* @param {string} calendarId - The calendar ID (empty falls back to the configured value or `primary`).
|
|
313
|
+
* @param {string} eventId - The event ID to delete.
|
|
314
|
+
* @returns {Promise<unknown>} `{ deleted: true, eventId }` on success.
|
|
315
|
+
* @throws {InternalError} When no credential is configured or the event ID is empty.
|
|
316
|
+
* @throws {NotFoundError} When the event does not exist (HTTP 404).
|
|
317
|
+
* @throws {BadRequestError} When Google rejects the deletion.
|
|
318
|
+
*/
|
|
319
|
+
async deleteEvent(calendarId, eventId) {
|
|
320
|
+
this.requireAuth();
|
|
321
|
+
if (!eventId) {
|
|
322
|
+
throw new InternalError("Google Calendar requires an event id");
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
await this.client.delete(`${this.calendarPath(calendarId)}/${encodeURIComponent(eventId)}`, { params: this.authParams() });
|
|
326
|
+
return { deleted: true, eventId };
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
throw httpError("Google Calendar", "delete event", error);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
//# sourceMappingURL=GoogleCalendarService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GoogleCalendarService.js","sourceRoot":"","sources":["../../../../src/calendar/google-calendar/GoogleCalendarService.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,4BAAuB;AACrD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,2CAAsC;AACrE,OAAO,EACL,cAAc,EAEd,SAAS,GACV,qCAA0B;AAoJ3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,OAAO,qBAAsB,SAAQ,cAA2C;IAAtF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C,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,SAAS,CAAC,iBAAiB,EAAE,cAAc,EAAE,KAAK,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/calendar/google-calendar/index.ts"],"names":[],"mappings":"AAAA,oCAA+B;AAC/B,2CAAwC"}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module integrations/comms/discord/service
|
|
3
|
+
* @summary Discord Bot API integration service for messages, channels, and reactions.
|
|
4
|
+
* @description Exposes the Discord Bot REST API (v10) as an undecorated
|
|
5
|
+
* client-based service built on {@link HttpApiService}. Configuration comes
|
|
6
|
+
* from an explicit `DiscordServiceConfig` or the `comms.discord` environment
|
|
7
|
+
* slice, and the bot token is sent as `Authorization: Bot <token>` on every
|
|
8
|
+
* call. Credentials are referenced from configuration (and, from as-graph
|
|
9
|
+
* nodes, from the resolved `discord` credential secret) and are never embedded
|
|
10
|
+
* in source. Failures are mapped to decaf errors via {@link httpError}.
|
|
11
|
+
*
|
|
12
|
+
* `core.comms.discord` is a callable messaging service/node. It is distinct
|
|
13
|
+
* from the mastra agent channel kinds `core.agent.trigger.discord` and
|
|
14
|
+
* `core.agent.response.discord`, which receive inbound conversations and post
|
|
15
|
+
* agent replies through host-supplied channel adapters.
|
|
16
|
+
*/
|
|
17
|
+
import { InternalError } from "@decaf-ts/db-decorators";
|
|
18
|
+
import { CommsEnvironment } from "./../environment.js";
|
|
19
|
+
import { envNumber, envString } from "./../../shared/environmentValue.js";
|
|
20
|
+
import { HttpApiService, httpError, } from "./../../shared/http/index.js";
|
|
21
|
+
const DEFAULT_CHANNEL_TYPE = 0;
|
|
22
|
+
/**
|
|
23
|
+
* @description Discord Bot API integration service
|
|
24
|
+
* @summary An undecorated HTTP service for channel messages
|
|
25
|
+
* (`sendMessage`, `getMessage`, `listMessages`), channels (`getChannel`,
|
|
26
|
+
* `listChannels`, `createChannel`, `deleteChannel`), and reactions
|
|
27
|
+
* (`addReaction`). Every operation requires a bot token and throws
|
|
28
|
+
* `InternalError` when it is missing or a required identifier is empty;
|
|
29
|
+
* transport failures are mapped to decaf errors via {@link httpError}. This is
|
|
30
|
+
* the callable comms surface (`core.comms.discord`), not the mastra agent
|
|
31
|
+
* trigger/response kinds.
|
|
32
|
+
* @class DiscordService
|
|
33
|
+
* @extends HttpApiService
|
|
34
|
+
* @category Comms
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { DiscordService } from "@decaf-ts/integrations/comms/discord";
|
|
38
|
+
*
|
|
39
|
+
* const discord = new DiscordService();
|
|
40
|
+
* await discord.initialize({ token: process.env.GRAPH_CREDENTIAL_DISCORD_TOKEN });
|
|
41
|
+
* await discord.sendMessage({
|
|
42
|
+
* channelId: process.env.DISCORD_CHANNEL_ID ?? "",
|
|
43
|
+
* content: "Deployment finished",
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
* @mermaid
|
|
47
|
+
* sequenceDiagram
|
|
48
|
+
* autonumber
|
|
49
|
+
* participant Caller
|
|
50
|
+
* participant Service as DiscordService
|
|
51
|
+
* participant Discord as Discord Bot API
|
|
52
|
+
* Caller->>Service: sendMessage(input)
|
|
53
|
+
* activate Service
|
|
54
|
+
* Service->>Service: requireBotToken()
|
|
55
|
+
* Service->>Discord: POST /channels/{id}/messages
|
|
56
|
+
* activate Discord
|
|
57
|
+
* Discord-->>Service: message payload
|
|
58
|
+
* deactivate Discord
|
|
59
|
+
* Service-->>Caller: message payload
|
|
60
|
+
* deactivate Service
|
|
61
|
+
* @memberOf module:integrations/comms/discord/service
|
|
62
|
+
*/
|
|
63
|
+
export class DiscordService extends HttpApiService {
|
|
64
|
+
constructor() {
|
|
65
|
+
super(...arguments);
|
|
66
|
+
/**
|
|
67
|
+
* @description The provider name used in error messages
|
|
68
|
+
*/
|
|
69
|
+
this.serviceName = "Discord";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @description The Discord Bot API base URL
|
|
73
|
+
* @summary Used when the resolved config carries no `baseUrl`.
|
|
74
|
+
* @returns {string} `"https://discord.com/api/v10"`.
|
|
75
|
+
*/
|
|
76
|
+
defaultBaseUrl() {
|
|
77
|
+
return "https://discord.com/api/v10";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @description Builds the Discord bot authorization header
|
|
81
|
+
* @summary Returns `Authorization: Bot <token>` when the config carries a
|
|
82
|
+
* token, and no headers otherwise.
|
|
83
|
+
* @param {DiscordServiceConfig} config - The resolved service configuration.
|
|
84
|
+
* @returns {Record<string, string>} The authorization headers (empty without a token).
|
|
85
|
+
*/
|
|
86
|
+
authHeaders(config) {
|
|
87
|
+
return config.token ? { Authorization: `Bot ${config.token}` } : {};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @description Reads the Discord credentials and defaults from the environment
|
|
91
|
+
* @summary Reads the `comms.discord` environment slice and returns a partial
|
|
92
|
+
* config when a bot token is configured, `undefined` otherwise. Values pass
|
|
93
|
+
* through `envString`/`envNumber` to reject unset-leaf proxies.
|
|
94
|
+
* @returns {Partial<DiscordServiceConfig> | undefined} A partial config, or `undefined` when no Discord token is configured.
|
|
95
|
+
*/
|
|
96
|
+
configFromEnvironment() {
|
|
97
|
+
const env = CommsEnvironment.comms.discord;
|
|
98
|
+
const token = envString(env?.token);
|
|
99
|
+
if (!token)
|
|
100
|
+
return undefined;
|
|
101
|
+
return {
|
|
102
|
+
token,
|
|
103
|
+
baseUrl: envString(env.baseUrl),
|
|
104
|
+
timeout: envNumber(env.timeout),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @description Asserts that a Discord bot token is configured
|
|
109
|
+
* @summary Called by every operation before it touches the network.
|
|
110
|
+
* @returns {void}
|
|
111
|
+
* @throws {InternalError} When the resolved config has no `token`.
|
|
112
|
+
*/
|
|
113
|
+
requireBotToken() {
|
|
114
|
+
if (!this.config.token) {
|
|
115
|
+
throw new InternalError("Discord requires a bot token");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @description Builds the encoded path for a channel resource
|
|
120
|
+
* @summary Validates the channel ID and URL-encodes it into `/channels/<id>`.
|
|
121
|
+
* @param {string} channelId - The Discord channel ID.
|
|
122
|
+
* @returns {string} The channel API path.
|
|
123
|
+
* @throws {InternalError} When `channelId` is empty.
|
|
124
|
+
*/
|
|
125
|
+
channelPath(channelId) {
|
|
126
|
+
if (!channelId) {
|
|
127
|
+
throw new InternalError("Discord requires a channel id");
|
|
128
|
+
}
|
|
129
|
+
return `/channels/${encodeURIComponent(channelId)}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @description Sends a message to a Discord channel
|
|
133
|
+
* @summary `POST`s the content (and optional `tts`/`embeds`) to the channel
|
|
134
|
+
* messages endpoint and returns Discord's message payload.
|
|
135
|
+
* @param {DiscordMessageInput} input - The destination channel and message content.
|
|
136
|
+
* @returns {Promise<unknown>} The created message payload.
|
|
137
|
+
* @throws {InternalError} When no bot token is configured.
|
|
138
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* await discord.sendMessage({ channelId: "1234567890", content: "Hello" });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
async sendMessage(input) {
|
|
145
|
+
this.requireBotToken();
|
|
146
|
+
try {
|
|
147
|
+
const response = await this.client.post(`${this.channelPath(input.channelId)}/messages`, {
|
|
148
|
+
content: input.content,
|
|
149
|
+
tts: input.tts ?? false,
|
|
150
|
+
...(input.embeds ? { embeds: input.embeds } : {}),
|
|
151
|
+
});
|
|
152
|
+
return response.data;
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
throw httpError("Discord", "send message", error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @description Fetches a single Discord message
|
|
160
|
+
* @summary `GET`s the message by channel and message ID and returns the
|
|
161
|
+
* message payload.
|
|
162
|
+
* @param {string} channelId - The channel containing the message.
|
|
163
|
+
* @param {string} messageId - The message ID to fetch.
|
|
164
|
+
* @returns {Promise<unknown>} The message payload.
|
|
165
|
+
* @throws {InternalError} When no bot token is configured or the message ID is empty.
|
|
166
|
+
* @throws {NotFoundError} When the message does not exist (HTTP 404).
|
|
167
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
168
|
+
*/
|
|
169
|
+
async getMessage(channelId, messageId) {
|
|
170
|
+
this.requireBotToken();
|
|
171
|
+
if (!messageId) {
|
|
172
|
+
throw new InternalError("Discord requires a message id");
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const response = await this.client.get(`${this.channelPath(channelId)}/messages/${encodeURIComponent(messageId)}`);
|
|
176
|
+
return response.data;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw httpError("Discord", "get message", error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @description Lists messages in a Discord channel
|
|
184
|
+
* @summary `GET`s the channel messages with the optional `limit` and
|
|
185
|
+
* `before` cursor and returns Discord's message page.
|
|
186
|
+
* @param {DiscordMessageListInput} input - The channel and paging options.
|
|
187
|
+
* @returns {Promise<unknown>} The message page payload.
|
|
188
|
+
* @throws {InternalError} When no bot token is configured.
|
|
189
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
190
|
+
*/
|
|
191
|
+
async listMessages(input) {
|
|
192
|
+
this.requireBotToken();
|
|
193
|
+
try {
|
|
194
|
+
const response = await this.client.get(`${this.channelPath(input.channelId)}/messages`, {
|
|
195
|
+
params: {
|
|
196
|
+
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
|
197
|
+
...(input.before ? { before: input.before } : {}),
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
return response.data;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
throw httpError("Discord", "list messages", error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @description Fetches a Discord channel
|
|
208
|
+
* @summary `GET`s the channel by ID and returns its payload.
|
|
209
|
+
* @param {string} channelId - The channel ID to fetch.
|
|
210
|
+
* @returns {Promise<unknown>} The channel payload.
|
|
211
|
+
* @throws {InternalError} When no bot token is configured or the channel ID is empty.
|
|
212
|
+
* @throws {NotFoundError} When the channel does not exist (HTTP 404).
|
|
213
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
214
|
+
*/
|
|
215
|
+
async getChannel(channelId) {
|
|
216
|
+
this.requireBotToken();
|
|
217
|
+
try {
|
|
218
|
+
const response = await this.client.get(this.channelPath(channelId));
|
|
219
|
+
return response.data;
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
throw httpError("Discord", "get channel", error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* @description Lists every channel in a Discord guild
|
|
227
|
+
* @summary `GET`s `/guilds/<id>/channels` and returns the channel list.
|
|
228
|
+
* @param {string} guildId - The guild whose channels are listed.
|
|
229
|
+
* @returns {Promise<unknown>} The guild channel list payload.
|
|
230
|
+
* @throws {InternalError} When no bot token is configured or the guild ID is empty.
|
|
231
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
232
|
+
*/
|
|
233
|
+
async listChannels(guildId) {
|
|
234
|
+
this.requireBotToken();
|
|
235
|
+
if (!guildId) {
|
|
236
|
+
throw new InternalError("Discord requires a guild id");
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const response = await this.client.get(`/guilds/${encodeURIComponent(guildId)}/channels`);
|
|
240
|
+
return response.data;
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
throw httpError("Discord", "list channels", error);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* @description Creates a channel in a Discord guild
|
|
248
|
+
* @summary `POST`s the channel `name`, numeric `type` (default text), and
|
|
249
|
+
* optional `topic`/`parentId` to the guild channels endpoint.
|
|
250
|
+
* @param {DiscordChannelInput} input - The guild and new channel definition.
|
|
251
|
+
* @returns {Promise<unknown>} The created channel payload.
|
|
252
|
+
* @throws {InternalError} When no bot token is configured.
|
|
253
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
254
|
+
*/
|
|
255
|
+
async createChannel(input) {
|
|
256
|
+
this.requireBotToken();
|
|
257
|
+
try {
|
|
258
|
+
const response = await this.client.post(`/guilds/${encodeURIComponent(input.guildId)}/channels`, {
|
|
259
|
+
name: input.name,
|
|
260
|
+
type: input.type ?? DEFAULT_CHANNEL_TYPE,
|
|
261
|
+
...(input.topic ? { topic: input.topic } : {}),
|
|
262
|
+
...(input.parentId ? { parent_id: input.parentId } : {}),
|
|
263
|
+
});
|
|
264
|
+
return response.data;
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
throw httpError("Discord", "create channel", error);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* @description Deletes a Discord channel
|
|
272
|
+
* @summary `DELETE`s the channel and returns the response payload, falling
|
|
273
|
+
* back to `{ deleted: true, channelId }` when Discord returns no body.
|
|
274
|
+
* @param {string} channelId - The channel ID to delete.
|
|
275
|
+
* @returns {Promise<unknown>} The deletion payload.
|
|
276
|
+
* @throws {InternalError} When no bot token is configured or the channel ID is empty.
|
|
277
|
+
* @throws {NotFoundError} When the channel does not exist (HTTP 404).
|
|
278
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
279
|
+
*/
|
|
280
|
+
async deleteChannel(channelId) {
|
|
281
|
+
this.requireBotToken();
|
|
282
|
+
try {
|
|
283
|
+
const response = await this.client.delete(this.channelPath(channelId));
|
|
284
|
+
return response.data ?? { deleted: true, channelId };
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
throw httpError("Discord", "delete channel", error);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* @description Adds a reaction to a Discord message as the bot
|
|
292
|
+
* @summary `PUT`s the emoji to the message's `@me` reaction endpoint and
|
|
293
|
+
* echoes the channel and message IDs on success.
|
|
294
|
+
* @param {DiscordReactionInput} input - The channel, message, and emoji.
|
|
295
|
+
* @returns {Promise<unknown>} `{ channelId, messageId }` on success.
|
|
296
|
+
* @throws {InternalError} When no bot token is configured, or the message ID or emoji is empty.
|
|
297
|
+
* @throws {BadRequestError} When Discord rejects the request.
|
|
298
|
+
*/
|
|
299
|
+
async addReaction(input) {
|
|
300
|
+
this.requireBotToken();
|
|
301
|
+
if (!input.messageId || !input.emoji) {
|
|
302
|
+
throw new InternalError("Discord requires a message id and emoji");
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
await this.client.put(`${this.channelPath(input.channelId)}/messages/${encodeURIComponent(input.messageId)}/reactions/${encodeURIComponent(input.emoji)}/@me`);
|
|
306
|
+
return { channelId: input.channelId, messageId: input.messageId };
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
throw httpError("Discord", "add reaction", error);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
//# sourceMappingURL=DiscordService.js.map
|