/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
packages/stateless/src/lib/handler/chat.handler.ts
59 строк
2 KB
Adam Chmara
chore(api,worker): rename channel address to channel endpoint fixes NV-6647 (#9108)
10 сен 2025, 15:29
Не верифицирован
10 сен 2025, 15:29
3cdc021
Код
Авторство
О чём код?
import { HandlebarsContentEngine, IContentEngine } from '../content/content.engine'; import { ChannelData, ENDPOINT_TYPES } from '../provider/channel-data.type'; import { IChatProvider } from '../provider/provider.interface'; import { IMessage, ITriggerPayload } from '../template/template.interface'; export class ChatHandler { private readonly contentEngine: IContentEngine; constructor( private message: IMessage, private provider: IChatProvider, contentEngine?: IContentEngine ) { this.contentEngine = contentEngine ?? new HandlebarsContentEngine(); } async send(data: ITriggerPayload) { let content = ''; if (typeof this.message.template === 'string') { content = this.contentEngine.compileTemplate(this.message.template, data); } else { content = await this.message.template(data); } const channelData = this.getChannelData(data); return await this.provider.sendMessage( { channelData, content, }, {} ); } private getChannelData(data: ITriggerPayload): ChannelData { // If channelData is provided, use it directly (new format) if (data.$channelData) { return data.$channelData; } // If webhookUrl is provided, transform it to channelData format (legacy support) if (data.$webhookUrl) { return { type: ENDPOINT_TYPES.WEBHOOK, endpoint: { url: data.$webhookUrl, ...(data.$channel_id && { channel: data.$channel_id as string }), }, identifier: '-', }; } // Neither channelData nor webhookUrl provided throw new Error( 'Channel data is missing in trigger payload. To send a chat message you must specify either a channelData property or a webhookUrl property.' ); } }