Add File->Get to Telegram Node (#1242)

* Add option `Include Download Link`

*  Add File->Get to Telegram Node

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2020-12-29 07:28:50 -05:00 committed by GitHub
parent bfd967258c
commit baa1ee1ad2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 5 deletions

View file

@ -9,4 +9,5 @@ export interface IEvent {
file_id: string; file_id: string;
}, },
}; };
download_link?: string;
} }

View file

@ -50,6 +50,10 @@ export class Telegram implements INodeType {
name: 'Callback', name: 'Callback',
value: 'callback', value: 'callback',
}, },
{
name: 'File',
value: 'file',
},
{ {
name: 'Message', name: 'Message',
value: 'message', value: 'message',
@ -129,6 +133,28 @@ export class Telegram implements INodeType {
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'file',
],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get a file.',
},
],
default: 'get',
description: 'The operation to perform.',
},
{ {
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
@ -390,6 +416,51 @@ export class Telegram implements INodeType {
// ----------------------------------
// file
// ----------------------------------
// ----------------------------------
// file:get/download
// ----------------------------------
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
displayOptions: {
show: {
operation: [
'get',
],
resource: [
'file',
],
},
},
required: true,
description: 'The ID of the file.',
},
{
displayName: 'Download',
name: 'download',
type: 'boolean',
displayOptions: {
show: {
operation: [
'get',
],
resource: [
'file',
],
},
},
default: true,
description: 'Download the file.',
},
// ---------------------------------- // ----------------------------------
// message // message
// ---------------------------------- // ----------------------------------
@ -1398,7 +1469,7 @@ export class Telegram implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: INodeExecutionData[] = [];
// For Post // For Post
let body: IDataObject; let body: IDataObject;
@ -1484,6 +1555,18 @@ export class Telegram implements INodeType {
} }
} else if (resource === 'file') {
if (operation === 'get') {
// ----------------------------------
// file:get
// ----------------------------------
endpoint = 'getFile';
body.file_id = this.getNodeParameter('fileId', i) as string;
}
} else if (resource === 'message') { } else if (resource === 'message') {
if (operation === 'editMessageText') { if (operation === 'editMessageText') {
@ -1638,9 +1721,34 @@ export class Telegram implements INodeType {
} }
const responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); const responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs);
returnData.push(responseData);
if (resource === 'file' && operation === 'get') {
if (this.getNodeParameter('download', i, false) as boolean === true) {
const filePath = responseData.result.file_path;
const credentials = this.getCredentials('telegramApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const file = await apiRequest.call(this, 'GET', '', {}, {}, { json: false, encoding: null, uri: `https://api.telegram.org/file/bot${credentials.accessToken}/${filePath}`, resolveWithFullResponse: true });
const fileName = filePath.split('/').pop();
const binaryData = await this.helpers.prepareBinaryData(Buffer.from(file.body as string), fileName);
returnData.push({
json: responseData,
binary: {
data: binaryData,
},
});
continue;
}
} }
return [this.helpers.returnJsonArray(returnData)]; returnData.push({ json: responseData });
}
return this.prepareOutputData(returnData);
} }
} }

View file

@ -122,7 +122,7 @@ export class TelegramTrigger implements INodeType {
name: 'download', name: 'download',
type: 'boolean', type: 'boolean',
default: false, default: false,
description: `Telegram develiers the image in 3 sizes.<br> description: `Telegram delivers the image in 3 sizes.<br>
By default, just the larger image would be downloaded.<br> By default, just the larger image would be downloaded.<br>
if you want to change the size set the field 'Image Size'`, if you want to change the size set the field 'Image Size'`,
}, },
@ -225,7 +225,6 @@ export class TelegramTrigger implements INodeType {
if (additionalFields.imageSize) { if (additionalFields.imageSize) {
imageSize = additionalFields.imageSize as string; imageSize = additionalFields.imageSize as string;
} }
let fileId; let fileId;