From 2c8c1b2ed512d0e8737c30f66c57d72de7877875 Mon Sep 17 00:00:00 2001
From: m2scared <93332316+m2scared@users.noreply.github.com>
Date: Fri, 3 Dec 2021 11:55:43 -0300
Subject: [PATCH] :zap: Add apiEndpoint to Telegram credentials
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
---
.../credentials/TelegramApi.credentials.ts | 20 +++++++++++++++++++
.../nodes/Telegram/GenericFunctions.ts | 13 +++++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/packages/nodes-base/credentials/TelegramApi.credentials.ts b/packages/nodes-base/credentials/TelegramApi.credentials.ts
index 4634f11b0eee7..19f8bd7b625e8 100644
--- a/packages/nodes-base/credentials/TelegramApi.credentials.ts
+++ b/packages/nodes-base/credentials/TelegramApi.credentials.ts
@@ -16,5 +16,25 @@ export class TelegramApi implements ICredentialType {
default: '',
description: 'Chat with the bot father 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: bot api.',
+ },
];
}
diff --git a/packages/nodes-base/nodes/Telegram/GenericFunctions.ts b/packages/nodes-base/nodes/Telegram/GenericFunctions.ts
index 4ed4eec279513..3feaa63d1bd12 100644
--- a/packages/nodes-base/nodes/Telegram/GenericFunctions.ts
+++ b/packages/nodes-base/nodes/Telegram/GenericFunctions.ts
@@ -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,
@@ -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
+ ;
+ });
+}