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 6 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',
},
};
}
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Telegram/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
}

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'telegramApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
Expand Down
114 changes: 55 additions & 59 deletions packages/nodes-base/nodes/Telegram/Telegram.node.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import FormData from 'form-data';

import {
IExecuteFunctions,
} from 'n8n-core';

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


export class Telegram implements INodeType {
description: INodeTypeDescription = {
displayName: 'Telegram',
Expand All @@ -39,7 +37,6 @@ export class Telegram implements INodeType {
{
name: 'telegramApi',
required: true,
testedBy: 'telegramBotTest',
},
],
properties: [
Expand Down Expand Up @@ -394,7 +391,7 @@ export class Telegram implements INodeType {
name: 'disable_notification',
type: 'boolean',
default: false,
description: 'Do not send a notification to all chat members about the new pinned message',
description: 'Whether to not send a notification to all chat members about the new pinned message',
},
],
},
Expand Down Expand Up @@ -530,7 +527,7 @@ export class Telegram implements INodeType {
name: 'show_alert',
type: 'boolean',
default: false,
description: 'If true, an alert will be shown by the client instead of a notification at the top of the chat screen',
description: 'Whether an alert will be shown by the client instead of a notification at the top of the chat screen',
},
{
displayName: 'Text',
Expand Down Expand Up @@ -623,7 +620,7 @@ export class Telegram implements INodeType {
name: 'show_alert',
type: 'boolean',
default: false,
description: 'If true, an alert will be shown by the client instead of a notification at the top of the chat screen',
description: 'Whether an alert will be shown by the client instead of a notification at the top of the chat screen',
},
{
displayName: 'Text',
Expand Down Expand Up @@ -687,7 +684,7 @@ export class Telegram implements INodeType {
},
},
default: true,
description: 'Download the file',
description: 'Whether to download the file',
},

// ----------------------------------
Expand Down Expand Up @@ -772,7 +769,7 @@ export class Telegram implements INodeType {
],
},
},
description: 'If the data to upload should be taken from binary field',
description: 'Whether the data to upload should be taken from binary field',
},
{
displayName: 'Binary Property',
Expand Down Expand Up @@ -801,7 +798,31 @@ export class Telegram implements INodeType {
placeholder: '',
description: 'Name of the binary property that contains the data to upload',
},

{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
hint: 'Optional file name, only required if your input file name is empty',
displayOptions: {
show: {
operation: [
'sendAnimation',
'sendAudio',
'sendDocument',
'sendPhoto',
'sendVideo',
'sendSticker',
],
resource: [
'message',
],
binaryData: [
true,
],
},
},
},
{
displayName: 'Message ID',
name: 'messageId',
Expand Down Expand Up @@ -1151,6 +1172,7 @@ export class Telegram implements INodeType {
default: 'HTML',
description: 'How to parse the text',
},

],
},
],
Expand Down Expand Up @@ -1333,14 +1355,14 @@ export class Telegram implements INodeType {
name: 'force_reply',
type: 'boolean',
default: false,
description: 'Shows reply interface to the user, as if they manually selected the bots message and tapped Reply',
description: 'Whether to show reply interface to the user, as if they manually selected the bot\'s message and tapped \'Reply',
},
{
displayName: 'Selective',
name: 'selective',
type: 'boolean',
default: false,
description: 'Use this parameter if you want to force reply from specific users only',
description: 'Whether to force reply from specific users only',
},
],
},
Expand Down Expand Up @@ -1412,7 +1434,7 @@ export class Telegram implements INodeType {
name: 'pay',
type: 'boolean',
default: false,
description: 'Specify True, to send a Pay button',
description: 'Whether to send a Pay button',
},
{
displayName: 'Switch Inline Query Current Chat',
Expand Down Expand Up @@ -1502,14 +1524,14 @@ export class Telegram implements INodeType {
name: 'request_contact',
type: 'boolean',
default: false,
description: 'If True, the user\'s phone number will be sent as a contact when the button is pressed.Available in private chats only',
description: 'Whether the user\'s phone number will be sent as a contact when the button is pressed.Available in private chats only',
},
{
displayName: 'Request Location',
name: 'request_location',
type: 'boolean',
default: false,
description: 'If True, the user\'s request_location',
description: 'Whether to use the user\'s request_location',
},
],
},
Expand Down Expand Up @@ -1541,21 +1563,21 @@ export class Telegram implements INodeType {
name: 'resize_keyboard',
type: 'boolean',
default: false,
description: 'Requests clients to resize the keyboard vertically for optimal fit',
description: 'Whether to request clients to resize the keyboard vertically for optimal fit',
},
{
displayName: 'One Time Keyboard',
name: 'one_time_keyboard',
type: 'boolean',
default: false,
description: 'Requests clients to hide the keyboard as soon as it\'s been used',
description: 'Whether to request clients to hide the keyboard as soon as it\'s been used',
},
{
displayName: 'Selective',
name: 'selective',
type: 'boolean',
default: false,
description: 'Use this parameter if you want to show the keyboard to specific users only',
description: 'Whether to show the keyboard to specific users only',
},
],
},
Expand All @@ -1579,14 +1601,14 @@ export class Telegram implements INodeType {
name: 'remove_keyboard',
type: 'boolean',
default: false,
description: 'Requests clients to remove the custom keyboard',
description: 'Whether to request clients to remove the custom keyboard',
},
{
displayName: 'Selective',
name: 'selective',
type: 'boolean',
default: false,
description: 'Use this parameter if you want to force reply from specific users only',
description: 'Whether to force reply from specific users only',
},
],
},
Expand Down Expand Up @@ -1650,7 +1672,7 @@ export class Telegram implements INodeType {
],
},
},
description: 'Sends the message silently. Users will receive a notification with no sound.',
description: 'Whether to send the message silently. Users will receive a notification with no sound.',
},
{
displayName: 'Disable WebPage Preview',
Expand All @@ -1665,7 +1687,7 @@ export class Telegram implements INodeType {
},
},
default: false,
description: 'Disables link previews for links in this message',
description: 'Whether to disable link previews for links in this message',
},
{
displayName: 'Duration',
Expand Down Expand Up @@ -1819,39 +1841,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 @@ -2187,19 +2176,27 @@ export class Telegram implements INodeType {
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const propertyName = getPropertyName(operation);

const fileNameUI = this.getNodeParameter('fileName',0) as string;
const filename = fileNameUI || binaryData.fileName?.toString();

body.disable_notification = body.disable_notification?.toString() || 'false';

const formData = {
...body,
[propertyName]: {
value: dataBuffer,
options: {
filename: binaryData.fileName,
filename,
contentType: binaryData.mimeType,
},
},
};
responseData = await apiRequest.call(this, requestMethod, endpoint, {}, qs, { formData });

const formDataOptions = new FormData(formData);

const options = {headers: formDataOptions.getHeaders(), formData};

responseData = await apiRequest.call(this, requestMethod, endpoint, {}, qs, options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tahasonmez
Can you please explain those changes to how formData is passed? Isn't It working as it was before?

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

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