fix(Telegram Node): Fix sending binaryData media (photo, document, video etc.) (#3408)

* fixed send media (photo, document, video etc.) issues on Telegram Node

* fixed send media (photo, document, video etc.) issues on Telegram Node

* file name is optional now

*  lock file and linter fix

*  improvements

*  fixes

*  Improvements

*  Add placeholder to File Name

*  Add error message

* 🔥 Remove requestWithAuthentication

*  Fix typo

* 👕 Fix linting issues

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
Taha Sönmez 2022-07-10 11:07:16 +03:00 committed by GitHub
parent dbfb8d56dc
commit af45a07f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 41 deletions

View file

@ -1,4 +1,5 @@
import { import {
ICredentialTestRequest,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -17,4 +18,11 @@ export class TelegramApi implements ICredentialType {
description: 'Chat with the <a href="https://telegram.me/botfather">bot father</a> to obtain the access token', description: 'Chat with the <a href="https://telegram.me/botfather">bot father</a> to obtain the access token',
}, },
]; ];
test: ICredentialTestRequest = {
request: {
baseURL: `=https://api.telegram.org/bot{{$credentials?.accessToken}}`,
url: '/getMe',
method: 'GET',
},
};
} }

View file

@ -4,10 +4,7 @@ import {
import { import {
IBinaryData, IBinaryData,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject, IDataObject,
INodeCredentialTestResult,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
@ -20,7 +17,6 @@ import {
getPropertyName, getPropertyName,
} from './GenericFunctions'; } from './GenericFunctions';
export class Telegram implements INodeType { export class Telegram implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Telegram', displayName: 'Telegram',
@ -39,7 +35,6 @@ export class Telegram implements INodeType {
{ {
name: 'telegramApi', name: 'telegramApi',
required: true, required: true,
testedBy: 'telegramBotTest',
}, },
], ],
properties: [ properties: [
@ -801,7 +796,6 @@ export class Telegram implements INodeType {
placeholder: '', placeholder: '',
description: 'Name of the binary property that contains the data to upload', description: 'Name of the binary property that contains the data to upload',
}, },
{ {
displayName: 'Message ID', displayName: 'Message ID',
name: 'messageId', name: 'messageId',
@ -1151,6 +1145,7 @@ export class Telegram implements INodeType {
default: 'HTML', default: 'HTML',
description: 'How to parse the text', description: 'How to parse the text',
}, },
], ],
}, },
], ],
@ -1686,6 +1681,31 @@ export class Telegram implements INodeType {
default: 0, default: 0,
description: 'Duration of clip in seconds', description: 'Duration of clip in seconds',
}, },
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
displayOptions: {
show: {
'/operation': [
'sendAnimation',
'sendAudio',
'sendDocument',
'sendPhoto',
'sendVideo',
'sendSticker',
],
'/resource': [
'message',
],
'/binaryData': [
true,
],
},
},
placeholder: 'image.jpeg',
},
{ {
displayName: 'Height', displayName: 'Height',
name: 'height', name: 'height',
@ -1819,39 +1839,6 @@ export class Telegram implements INodeType {
], ],
}; };
methods = {
credentialTest: {
async telegramBotTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
const credentials = credential.data;
const options = {
uri: `https://api.telegram.org/bot${credentials!.accessToken}/getMe`,
json: true,
};
try {
const response = await this.helpers.request(options);
if (!response.ok) {
return {
status: 'Error',
message: 'Token is not valid.',
};
}
} catch (err) {
return {
status: 'Error',
message: `Token is not valid; ${err.message}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: INodeExecutionData[] = []; const returnData: INodeExecutionData[] = [];
@ -2186,6 +2173,16 @@ export class Telegram implements INodeType {
const binaryData = items[i].binary![binaryPropertyName] as IBinaryData; const binaryData = items[i].binary![binaryPropertyName] as IBinaryData;
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const propertyName = getPropertyName(operation); const propertyName = getPropertyName(operation);
const fileName = this.getNodeParameter('additionalFields.fileName', 0, '') as string;
const filename = fileName || binaryData.fileName?.toString();
if (!fileName && !binaryData.fileName) {
throw new NodeOperationError(this.getNode(),
`File name is needed to ${operation}. Make sure the property that holds the binary data
has the file name property set or set it manually in the node using the File Name parameter under
Additional Fields.`);
}
body.disable_notification = body.disable_notification?.toString() || 'false'; body.disable_notification = body.disable_notification?.toString() || 'false';
@ -2194,11 +2191,12 @@ export class Telegram implements INodeType {
[propertyName]: { [propertyName]: {
value: dataBuffer, value: dataBuffer,
options: { options: {
filename: binaryData.fileName, filename,
contentType: binaryData.mimeType, contentType: binaryData.mimeType,
}, },
}, },
}; };
responseData = await apiRequest.call(this, requestMethod, endpoint, {}, qs, { formData }); responseData = await apiRequest.call(this, requestMethod, endpoint, {}, qs, { formData });
} else { } else {
responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs); responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs);
@ -2233,7 +2231,6 @@ export class Telegram implements INodeType {
// chat: responseData.result[0].message.chat, // chat: responseData.result[0].message.chat,
// }; // };
// } // }
returnData.push({ json: responseData }); returnData.push({ json: responseData });
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {