mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
✨ Add Telegram-Nodes
This commit is contained in:
parent
295bd212ce
commit
6173149646
182
packages/nodes-base/nodes/Telegram/GenericFunctions.ts
Normal file
182
packages/nodes-base/nodes/Telegram/GenericFunctions.ts
Normal file
|
@ -0,0 +1,182 @@
|
|||
import {
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import { OptionsWithUri } from 'request';
|
||||
import { IDataObject } from 'n8n-workflow';
|
||||
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
// Add the reply markup
|
||||
const replyMarkupOption = this.getNodeParameter('replyMarkup', index) as string;
|
||||
if (replyMarkupOption === 'none') {
|
||||
return;
|
||||
}
|
||||
|
||||
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>}
|
||||
*/
|
||||
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
||||
console.log('');
|
||||
console.log('============ apiRequest ===========');
|
||||
|
||||
const credentials = this.getCredentials('telegramApi');
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
query = query || {};
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
},
|
||||
method,
|
||||
body,
|
||||
qs: query,
|
||||
uri: `https://api.telegram.org/bot${credentials.accessToken}/${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
console.log(JSON.stringify(options, null, 2));
|
||||
|
||||
try {
|
||||
console.log('MAKE CALL');
|
||||
const response = await this.helpers.request!(options);
|
||||
console.log('____response:');
|
||||
console.log(JSON.stringify(response, null, 2));
|
||||
|
||||
return response;
|
||||
// return this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
if (error.statusCode === 401) {
|
||||
// Return a clear error
|
||||
throw new Error('The Telegram credentials are not valid!');
|
||||
}
|
||||
|
||||
if (error.response && error.response.body && error.response.body.error_code) {
|
||||
// Try to return the error prettier
|
||||
const airtableError = error.response.body;
|
||||
throw new Error(`Telegram error response [${airtableError.error_code}]: ${airtableError.description}`);
|
||||
}
|
||||
|
||||
// Expected error data did not get returned so throw the actual error
|
||||
throw error;
|
||||
}
|
||||
}
|
1287
packages/nodes-base/nodes/Telegram/Telegram.node.ts
Normal file
1287
packages/nodes-base/nodes/Telegram/Telegram.node.ts
Normal file
File diff suppressed because it is too large
Load diff
160
packages/nodes-base/nodes/Telegram/TelegramTrigger.node.ts
Normal file
160
packages/nodes-base/nodes/Telegram/TelegramTrigger.node.ts
Normal file
|
@ -0,0 +1,160 @@
|
|||
import {
|
||||
IHookFunctions,
|
||||
IWebhookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
INodeTypeDescription,
|
||||
INodeType,
|
||||
IWebhookResonseData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
apiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
|
||||
export class TelegramTrigger implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Telegram Trigger',
|
||||
name: 'telegramTrigger',
|
||||
icon: 'file:telegram.png',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
subtitle: '=Updates: {{$parameter["updates"].join(", ")}}',
|
||||
description: 'Starts the workflow on a Telegram update.',
|
||||
defaults: {
|
||||
name: 'Telegram Trigger',
|
||||
color: '#0088cc',
|
||||
},
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'telegramApi',
|
||||
required: true,
|
||||
}
|
||||
],
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
httpMethod: 'POST',
|
||||
responseMode: 'onReceived',
|
||||
path: 'webhook',
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Updates',
|
||||
name: 'updates',
|
||||
type: 'multiOptions',
|
||||
options: [
|
||||
{
|
||||
name: '*',
|
||||
value: '*',
|
||||
description: 'All updates.',
|
||||
},
|
||||
{
|
||||
name: 'message',
|
||||
value: 'message',
|
||||
description: 'Trigger on new incoming message of any kind — text, photo, sticker, etc..',
|
||||
},
|
||||
{
|
||||
name: 'edited_message',
|
||||
value: 'edited_message',
|
||||
description: 'Trigger on new version of a channel post that is known to the bot and was edited.',
|
||||
},
|
||||
{
|
||||
name: 'channel_post',
|
||||
value: 'channel_post',
|
||||
description: 'Trigger on new incoming channel post of any kind — text, photo, sticker, etc..',
|
||||
},
|
||||
{
|
||||
name: 'edited_channel_post',
|
||||
value: 'edited_channel_post',
|
||||
description: 'Trigger on new version of a channel post that is known to the bot and was edited.',
|
||||
},
|
||||
{
|
||||
name: 'inline_query',
|
||||
value: 'inline_query',
|
||||
description: 'Trigger on new incoming inline query.',
|
||||
},
|
||||
{
|
||||
name: 'callback_query',
|
||||
value: 'callback_query',
|
||||
description: 'Trigger on new incoming callback query.',
|
||||
},
|
||||
|
||||
{
|
||||
name: 'shipping_query',
|
||||
value: 'shipping_query',
|
||||
description: 'Trigger on new incoming shipping query. Only for invoices with flexible price.',
|
||||
},
|
||||
{
|
||||
name: 'pre_checkout_query',
|
||||
value: 'pre_checkout_query',
|
||||
description: 'Trigger on new incoming pre-checkout query. Contains full information about checkout.',
|
||||
},
|
||||
{
|
||||
name: 'poll',
|
||||
value: 'poll',
|
||||
description: 'Trigger on new poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot.',
|
||||
},
|
||||
],
|
||||
required: true,
|
||||
default: ['*'],
|
||||
description: 'The update types to listen to.',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// @ts-ignore (because of request)
|
||||
webhookMethods = {
|
||||
default: {
|
||||
async create(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||
|
||||
let allowedUpdates = this.getNodeParameter('updates') as string[];
|
||||
|
||||
if (allowedUpdates.includes('*')) {
|
||||
allowedUpdates = [];
|
||||
}
|
||||
|
||||
const endpoint = 'setWebhook';
|
||||
|
||||
const body = {
|
||||
url: webhookUrl,
|
||||
allowed_updates: allowedUpdates,
|
||||
};
|
||||
|
||||
await apiRequest.call(this, 'POST', endpoint, body);
|
||||
|
||||
return true;
|
||||
},
|
||||
async delete(this: IHookFunctions): Promise<boolean> {
|
||||
const endpoint = 'deleteWebhook';
|
||||
const body = {};
|
||||
|
||||
try {
|
||||
await apiRequest.call(this, 'POST', endpoint, body);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResonseData> {
|
||||
const bodyData = this.getBodyData();
|
||||
|
||||
return {
|
||||
workflowData: [
|
||||
this.helpers.returnJsonArray([bodyData])
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
BIN
packages/nodes-base/nodes/Telegram/telegram.png
Normal file
BIN
packages/nodes-base/nodes/Telegram/telegram.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
|
@ -46,6 +46,7 @@
|
|||
"dist/credentials/Redis.credentials.js",
|
||||
"dist/credentials/SlackApi.credentials.js",
|
||||
"dist/credentials/Smtp.credentials.js",
|
||||
"dist/credentials/TelegramApi.credentials.js",
|
||||
"dist/credentials/TrelloApi.credentials.js",
|
||||
"dist/credentials/TwilioApi.credentials.js"
|
||||
],
|
||||
|
@ -91,6 +92,8 @@
|
|||
"dist/nodes/Slack/Slack.node.js",
|
||||
"dist/nodes/SpreadsheetFile.node.js",
|
||||
"dist/nodes/Start.node.js",
|
||||
"dist/nodes/Telegram/Telegram.node.js",
|
||||
"dist/nodes/Telegram/TelegramTrigger.node.js",
|
||||
"dist/nodes/Trello/Trello.node.js",
|
||||
"dist/nodes/Trello/TrelloTrigger.node.js",
|
||||
"dist/nodes/Twilio/Twilio.node.js",
|
||||
|
|
Loading…
Reference in a new issue