2019-10-04 03:35:06 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2020-09-04 01:28:04 -07:00
|
|
|
IWebhookFunctions,
|
2019-10-04 03:35:06 -07:00
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-09-04 01:28:04 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2019-10-04 03:35:06 -07:00
|
|
|
|
2020-09-04 01:28:04 -07:00
|
|
|
import {
|
2022-02-18 07:09:44 -08:00
|
|
|
IDataObject,
|
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-09-04 01:28:04 -07:00
|
|
|
} from 'n8n-workflow';
|
2019-10-04 03:35:06 -07:00
|
|
|
|
|
|
|
// Interface in n8n
|
|
|
|
export interface IMarkupKeyboard {
|
|
|
|
rows?: IMarkupKeyboardRow[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IMarkupKeyboardRow {
|
|
|
|
row?: IMarkupKeyboardRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IMarkupKeyboardRow {
|
|
|
|
buttons?: IMarkupKeyboardButton[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IMarkupKeyboardButton {
|
|
|
|
text: string;
|
|
|
|
additionalFields?: IDataObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Interface in Telegram
|
|
|
|
export interface ITelegramInlineReply {
|
|
|
|
inline_keyboard?: ITelegramKeyboardButton[][];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ITelegramKeyboardButton {
|
|
|
|
[key: string]: string | number | boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ITelegramReplyKeyboard extends IMarkupReplyKeyboardOptions {
|
|
|
|
keyboard: ITelegramKeyboardButton[][];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Shared interfaces
|
|
|
|
export interface IMarkupForceReply {
|
|
|
|
force_reply?: boolean;
|
|
|
|
selective?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IMarkupReplyKeyboardOptions {
|
|
|
|
one_time_keyboard?: boolean;
|
|
|
|
resize_keyboard?: boolean;
|
|
|
|
selective?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IMarkupReplyKeyboardRemove {
|
|
|
|
force_reply?: boolean;
|
|
|
|
selective?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the additional fields to the body
|
|
|
|
*
|
|
|
|
* @param {IExecuteFunctions} this
|
|
|
|
* @param {IDataObject} body The body object to add fields to
|
|
|
|
* @param {number} index The index of the item
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function addAdditionalFields(this: IExecuteFunctions, body: IDataObject, index: number) {
|
|
|
|
// Add the additional fields
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', index) as IDataObject;
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
2019-12-13 11:05:21 -08:00
|
|
|
const operation = this.getNodeParameter('operation', index) as string;
|
|
|
|
|
2019-10-04 03:35:06 -07:00
|
|
|
// Add the reply markup
|
2019-12-13 11:05:21 -08:00
|
|
|
let replyMarkupOption = '';
|
|
|
|
if (operation !== 'sendMediaGroup') {
|
|
|
|
replyMarkupOption = this.getNodeParameter('replyMarkup', index) as string;
|
|
|
|
if (replyMarkupOption === 'none') {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-04 03:35:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
body.reply_markup = {} as IMarkupForceReply | IMarkupReplyKeyboardRemove | ITelegramInlineReply | ITelegramReplyKeyboard;
|
|
|
|
if (['inlineKeyboard', 'replyKeyboard'].includes(replyMarkupOption)) {
|
|
|
|
let setParameterName = 'inline_keyboard';
|
|
|
|
if (replyMarkupOption === 'replyKeyboard') {
|
|
|
|
setParameterName = 'keyboard';
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyboardData = this.getNodeParameter(replyMarkupOption, index) as IMarkupKeyboard;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
(body.reply_markup as ITelegramInlineReply | ITelegramReplyKeyboard)[setParameterName] = [] as ITelegramKeyboardButton[][];
|
|
|
|
let sendButtonData: ITelegramKeyboardButton;
|
|
|
|
if (keyboardData.rows !== undefined) {
|
|
|
|
for (const row of keyboardData.rows) {
|
|
|
|
const sendRows: ITelegramKeyboardButton[] = [];
|
|
|
|
if (row.row === undefined || row.row.buttons === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const button of row.row.buttons) {
|
|
|
|
sendButtonData = {};
|
|
|
|
sendButtonData.text = button.text;
|
|
|
|
if (button.additionalFields) {
|
|
|
|
Object.assign(sendButtonData, button.additionalFields);
|
|
|
|
}
|
|
|
|
sendRows.push(sendButtonData);
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
((body.reply_markup as ITelegramInlineReply | ITelegramReplyKeyboard)[setParameterName] as ITelegramKeyboardButton[][]).push(sendRows);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (replyMarkupOption === 'forceReply') {
|
|
|
|
const forceReply = this.getNodeParameter('forceReply', index) as IMarkupForceReply;
|
|
|
|
body.reply_markup = forceReply;
|
|
|
|
} else if (replyMarkupOption === 'replyKeyboardRemove') {
|
|
|
|
const forceReply = this.getNodeParameter('replyKeyboardRemove', index) as IMarkupReplyKeyboardRemove;
|
|
|
|
body.reply_markup = forceReply;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replyMarkupOption === 'replyKeyboard') {
|
|
|
|
const replyKeyboardOptions = this.getNodeParameter('replyKeyboardOptions', index) as IMarkupReplyKeyboardOptions;
|
|
|
|
Object.assign(body.reply_markup, replyKeyboardOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Telegram
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2021-09-27 15:47:39 -07:00
|
|
|
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('telegramApi');
|
2019-10-04 03:35:06 -07:00
|
|
|
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2019-10-04 03:35:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
query = query || {};
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2021-09-27 15:47:39 -07:00
|
|
|
headers: {},
|
2019-10-04 03:35:06 -07:00
|
|
|
method,
|
2021-09-27 15:47:39 -07:00
|
|
|
uri: `https://api.telegram.org/bot${credentials.accessToken}/${endpoint}`,
|
2019-10-04 03:35:06 -07:00
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2020-09-04 01:28:04 -07:00
|
|
|
if (Object.keys(option).length > 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(query).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
2019-10-04 03:35:06 -07:00
|
|
|
try {
|
2019-10-05 04:44:25 -07:00
|
|
|
return await this.helpers.request!(options);
|
2019-10-04 03:35:06 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-10-04 03:35:06 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 01:28:04 -07:00
|
|
|
|
|
|
|
export function getImageBySize(photos: IDataObject[], size: string): IDataObject | undefined {
|
|
|
|
|
|
|
|
const sizes = {
|
|
|
|
'small': 0,
|
|
|
|
'medium': 1,
|
|
|
|
'large': 2,
|
2022-02-18 07:09:44 -08:00
|
|
|
'extraLarge': 3,
|
2020-09-04 01:28:04 -07:00
|
|
|
} as IDataObject;
|
|
|
|
|
|
|
|
const index = sizes[size] as number;
|
|
|
|
|
|
|
|
return photos[index];
|
|
|
|
}
|
2021-09-27 15:47:39 -07:00
|
|
|
|
|
|
|
export function getPropertyName(operation: string) {
|
|
|
|
return operation.replace('send', '').toLowerCase();
|
|
|
|
}
|