Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sending binaryData media (photo, document, video etc.) issues on Telegram Node - N8N-3783 #3408

Merged
merged 14 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/nodes-base/credentials/TelegramApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -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',
},
];
test: ICredentialTestRequest = {
request: {
baseURL: `=https://api.telegram.org/bot{{$credentials?.accessToken}}`,
url: '/getMe',
method: 'GET',
},
};
}
79 changes: 38 additions & 41 deletions packages/nodes-base/nodes/Telegram/Telegram.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {

import {
IBinaryData,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
Expand All @@ -20,7 +17,6 @@ import {
getPropertyName,
} from './GenericFunctions';


export class Telegram implements INodeType {
description: INodeTypeDescription = {
displayName: 'Telegram',
Expand All @@ -39,7 +35,6 @@ export class Telegram implements INodeType {
{
name: 'telegramApi',
required: true,
testedBy: 'telegramBotTest',
},
],
properties: [
Expand Down Expand Up @@ -801,7 +796,6 @@ export class Telegram implements INodeType {
placeholder: '',
description: 'Name of the binary property that contains the data to upload',
},

{
displayName: 'Message ID',
name: 'messageId',
Expand Down Expand Up @@ -1151,6 +1145,7 @@ export class Telegram implements INodeType {
default: 'HTML',
description: 'How to parse the text',
},

],
},
],
Expand Down Expand Up @@ -1686,6 +1681,31 @@ export class Telegram implements INodeType {
default: 0,
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',
name: 'height',
Expand Down Expand Up @@ -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[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
Expand Down Expand Up @@ -2186,6 +2173,16 @@ export class Telegram implements INodeType {
const binaryData = items[i].binary![binaryPropertyName] as IBinaryData;
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
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';

Expand All @@ -2194,11 +2191,12 @@ export class Telegram implements INodeType {
[propertyName]: {
value: dataBuffer,
options: {
filename: binaryData.fileName,
filename,
contentType: binaryData.mimeType,
},
},
};

responseData = await apiRequest.call(this, requestMethod, endpoint, {}, qs, { formData });
} else {
responseData = await apiRequest.call(this, requestMethod, endpoint, body, qs);
Expand Down Expand Up @@ -2233,7 +2231,6 @@ export class Telegram implements INodeType {
// chat: responseData.result[0].message.chat,
// };
// }

returnData.push({ json: responseData });
} catch (error) {
if (this.continueOnFail()) {
Expand Down