Download images and files with Telegram Trigger (#919)

*  Download images and files.

*  Improvements

*  Small improvements
This commit is contained in:
Ricardo Espinoza
2020-09-04 04:28:04 -04:00
committed by GitHub
parent dcded3b96b
commit a032c5448d
3 changed files with 156 additions and 10 deletions

View File

@@ -2,12 +2,16 @@ import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import { OptionsWithUri } from 'request';
import { IDataObject } from 'n8n-workflow';
import {
OptionsWithUri,
} from 'request';
import {
IDataObject,
} from 'n8n-workflow';
// Interface in n8n
export interface IMarkupKeyboard {
@@ -138,7 +142,7 @@ export function addAdditionalFields(this: IExecuteFunctions, body: IDataObject,
* @param {object} body
* @returns {Promise<any>}
*/
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, endpoint: string, body: object, query?: IDataObject, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('telegramApi');
if (credentials === undefined) {
@@ -157,9 +161,22 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
json: true,
};
if (Object.keys(option).length > 0) {
Object.assign(options, option);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
if (Object.keys(query).length === 0) {
delete options.qs;
}
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error
throw new Error('The Telegram credentials are not valid!');
@@ -175,3 +192,16 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
throw error;
}
}
export function getImageBySize(photos: IDataObject[], size: string): IDataObject | undefined {
const sizes = {
'small': 0,
'medium': 1,
'large': 2,
} as IDataObject;
const index = sizes[size] as number;
return photos[index];
}