mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat(Google Chat Node): Updates (#12827)
Co-authored-by: Dana <152518854+dana-gill@users.noreply.github.com>
This commit is contained in:
@@ -9,48 +9,70 @@ import type {
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
import { getSendAndWaitConfig } from '../../../utils/sendAndWait/utils';
|
||||
import { getGoogleAccessToken } from '../GenericFunctions';
|
||||
|
||||
async function googleServiceAccountApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
options: IRequestOptions,
|
||||
noCredentials = false,
|
||||
): Promise<any> {
|
||||
if (noCredentials) {
|
||||
return await this.helpers.request(options);
|
||||
}
|
||||
|
||||
const credentials = await this.getCredentials('googleApi');
|
||||
|
||||
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'chat');
|
||||
options.headers!.Authorization = `Bearer ${access_token}`;
|
||||
|
||||
return await this.helpers.request(options);
|
||||
}
|
||||
|
||||
export async function googleApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
resource: string,
|
||||
|
||||
body: any = {},
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
noCredentials = false,
|
||||
encoding?: null | undefined,
|
||||
): Promise<any> {
|
||||
) {
|
||||
const options: IRequestOptions = {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method,
|
||||
body,
|
||||
qs,
|
||||
uri: uri || `https://chat.googleapis.com${resource}`,
|
||||
qsStringifyOptions: {
|
||||
arrayFormat: 'repeat',
|
||||
},
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(body as IDataObject).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
if (encoding === null) {
|
||||
options.encoding = null;
|
||||
}
|
||||
|
||||
let responseData: IDataObject | undefined;
|
||||
try {
|
||||
if (noCredentials) {
|
||||
responseData = await this.helpers.request(options);
|
||||
} else {
|
||||
const credentials = await this.getCredentials('googleApi');
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'chat');
|
||||
options.headers!.Authorization = `Bearer ${access_token}`;
|
||||
responseData = await this.helpers.request(options);
|
||||
let responseData;
|
||||
|
||||
try {
|
||||
if (noCredentials || this.getNodeParameter('authentication', 0) === 'serviceAccount') {
|
||||
responseData = await googleServiceAccountApiRequest.call(this, options, noCredentials);
|
||||
} else {
|
||||
responseData = await this.helpers.requestWithAuthentication.call(
|
||||
this,
|
||||
'googleChatOAuth2Api',
|
||||
options,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') {
|
||||
@@ -59,6 +81,7 @@ export async function googleApiRequest(
|
||||
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
|
||||
if (Object.keys(responseData as IDataObject).length !== 0) {
|
||||
return responseData;
|
||||
} else {
|
||||
@@ -134,3 +157,26 @@ export function getPagingParameters(resource: string, operation = 'getAll') {
|
||||
];
|
||||
return pagingParameters;
|
||||
}
|
||||
|
||||
export function createSendAndWaitMessageBody(context: IExecuteFunctions) {
|
||||
const config = getSendAndWaitConfig(context);
|
||||
|
||||
const instanceId = context.getInstanceId();
|
||||
const attributionText = '_This_ _message_ _was_ _sent_ _automatically_ _with_';
|
||||
const link = `https://n8n.io/?utm_source=n8n-internal&utm_medium=powered_by&utm_campaign=${encodeURIComponent(
|
||||
'n8n-nodes-base.telegram',
|
||||
)}${instanceId ? '_' + instanceId : ''}`;
|
||||
const attribution = `${attributionText} _<${link}|n8n>_`;
|
||||
|
||||
const buttons: string[] = config.options.map(
|
||||
(option) => `*<${`${config.url}?approved=${option.value}`}|${option.label}>*`,
|
||||
);
|
||||
|
||||
const text = `${config.message}\n\n\n${buttons.join(' ')}\n\n${attribution}`;
|
||||
|
||||
const body = {
|
||||
text,
|
||||
};
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user