Skip to content

Commit

Permalink
⚡ Add apiEndpoint to Telegram credentials
Browse files Browse the repository at this point in the history
Create an advanced option for setting custom Telegram's apiEndpoint.

Inspired by https://github.com/go-telegram-bot-api/telegram-bot-api/blob/3834565c356e9b2d94bd8080555aeaf795bbb0ea/bot.go#L100
  • Loading branch information
m2scared committed Dec 3, 2021
1 parent fe741bd commit 2c8c1b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/nodes-base/credentials/TelegramApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,25 @@ export class TelegramApi implements ICredentialType {
default: '',
description: 'Chat with the <a href="https://telegram.me/botfather">bot father</a> to obtain the access token.',
},
{
displayName: 'Advanced',
name: 'advanced',
type: 'boolean',
default: false,
},
{
displayName: 'APIEndpoint',
name: 'apiEndpoint',
type: 'string',
displayOptions: {
show: {
advanced: [
true,
],
},
},
default: 'https://api.telegram.org/bot{0}/{1}',
description: 'API endpoint. Use to redirect Telegram API calls to another server. First \'{0}\' is the \'accessToken\', second \'{1}\' is the endpoint method. See: <a href="https://core.telegram.org/bots">bot api</a>.',
},
];
}
13 changes: 12 additions & 1 deletion packages/nodes-base/nodes/Telegram/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}

const uri = formatString(`${credentials.apiEndpoint}`, `${credentials.accessToken}`, endpoint);

query = query || {};

const options: OptionsWithUri = {
headers: {},
method,
uri: `https://api.telegram.org/bot${credentials.accessToken}/${endpoint}`,
uri: uri,
body,
qs: query,
json: true,
Expand Down Expand Up @@ -196,3 +198,12 @@ export function getImageBySize(photos: IDataObject[], size: string): IDataObject
export function getPropertyName(operation: string) {
return operation.replace('send', '').toLowerCase();
}

function formatString(s: string, ...args : string[]): string {
return s.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
}

0 comments on commit 2c8c1b2

Please sign in to comment.