n8n
Version:
n8n Workflow Automation Tool
122 lines • 5.57 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TelegramIntegration = void 0;
const backend_common_1 = require("@n8n/backend-common");
const di_1 = require("@n8n/di");
const conflict_error_1 = require("../../../../errors/response-errors/conflict.error");
const url_service_1 = require("../../../../services/url.service");
const agent_repository_1 = require("../../repositories/agent.repository");
const agent_chat_integration_1 = require("../agent-chat-integration");
const esm_loader_1 = require("../esm-loader");
let TelegramIntegration = class TelegramIntegration extends agent_chat_integration_1.AgentChatIntegration {
constructor(logger, urlService, agentRepository) {
super();
this.logger = logger;
this.urlService = urlService;
this.agentRepository = agentRepository;
this.type = 'telegram';
this.credentialTypes = ['telegramApi'];
this.displayLabel = 'Telegram';
this.displayIcon = 'telegram';
this.supportedComponents = ['section', 'button', 'divider', 'fields'];
this.needsShortCallbackData = true;
this.disableStreaming = true;
this.formatThreadId = {
fromSdk: (thread) => {
const adapter = thread.adapter;
const botUserId = adapter.botUserId;
if (!botUserId) {
throw new Error('Telegram bot user ID is not set');
}
return `chat:${botUserId}-${thread.id}`;
},
toSdk: (threadId) => {
if (!threadId.includes('-')) {
return threadId;
}
return threadId.split('-').slice(1).join('-');
},
};
}
async createAdapter(ctx) {
const botToken = this.extractBotToken(ctx.credential);
const mode = this.getMode();
const { createTelegramAdapter } = await (0, esm_loader_1.loadTelegramAdapter)();
return createTelegramAdapter({ botToken, mode });
}
requiresLeader() {
return this.getMode() === 'polling';
}
async onBeforeConnect(ctx) {
const others = await this.agentRepository.findByIntegrationCredential(this.type, ctx.credentialId, ctx.projectId, ctx.agentId);
if (others.length > 0) {
throw new conflict_error_1.ConflictError(`Telegram credential is already connected to agent "${others[0].name}"`);
}
}
async onAfterConnect(ctx) {
if (this.getMode() !== 'webhook')
return;
const botToken = this.extractBotToken(ctx.credential);
const webhookUrl = ctx.webhookUrlFor('telegram');
const resp = await fetch(`https://api.telegram.org/bot${botToken}/setWebhook`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: webhookUrl }),
});
if (!resp.ok) {
throw new Error(`Failed to register Telegram webhook: ${await resp.text()}`);
}
this.logger.info(`[TelegramIntegration] Webhook registered: ${webhookUrl}`);
}
normalizeComponents(components) {
const normalized = [];
for (const c of components) {
switch (c.type) {
case 'select':
case 'radio_select':
for (const opt of c.options ?? []) {
normalized.push({ type: 'button', label: opt.label, value: opt.value });
}
break;
case 'image':
if (c.url) {
normalized.push({ type: 'section', text: `[${c.altText ?? 'Image'}](${c.url})` });
}
break;
default:
normalized.push(c);
}
}
return normalized;
}
getMode() {
const baseUrl = this.urlService.getWebhookBaseUrl();
const isPublic = baseUrl.startsWith('https://') && !baseUrl.includes('localhost');
return isPublic ? 'webhook' : 'polling';
}
extractBotToken(credential) {
const token = credential.accessToken;
if (typeof token === 'string' && token) {
return token;
}
throw new Error('Could not extract a bot token from the Telegram credential. ' +
'Please ensure the credential has a valid access token from BotFather.');
}
};
exports.TelegramIntegration = TelegramIntegration;
exports.TelegramIntegration = TelegramIntegration = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [backend_common_1.Logger,
url_service_1.UrlService,
agent_repository_1.AgentRepository])
], TelegramIntegration);
//# sourceMappingURL=telegram-integration.js.map