45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
'use strict';
|
||
|
||
const OutboundBase = require('./base');
|
||
|
||
/**
|
||
* Webhook 渠道出站实现。
|
||
* 直接 HTTP POST 推送 JSON 到目标 webhook URL。
|
||
*/
|
||
class WebhookDispatch extends OutboundBase {
|
||
/**
|
||
* 通过 webhook 渠道推送消息。
|
||
* @param {Object} record 消息记录(Sequelize Model 实例)。
|
||
* @return {Promise<{status: number, data: *}>} 推送响应。
|
||
*/
|
||
async push(record) {
|
||
const { ctx } = this;
|
||
|
||
const body = {
|
||
ref_code: record.ref_code,
|
||
ext_data: record.ext_data,
|
||
message_id: record.ext_id,
|
||
direction: record.direction,
|
||
source_type: record.source_type,
|
||
created_at: record.created_at,
|
||
};
|
||
|
||
let targetUrl = record.target_url;
|
||
|
||
// API 类型需要签名
|
||
if (record.target_type === 'api') {
|
||
const apiApp = await this.getApiApp(record.warehouse_id);
|
||
if (apiApp) {
|
||
targetUrl = this.signRequest(targetUrl, body, apiApp);
|
||
} else {
|
||
ctx.logger.warn(`[inbox webhook] 仓库 ${record.warehouse_id} 没有可用 ApiApp,跳过签名`);
|
||
}
|
||
}
|
||
|
||
ctx.logger.info(`[inbox webhook] 推送消息 ext_id=${record.ext_id} url=${targetUrl}`);
|
||
return await this.post(targetUrl, body);
|
||
}
|
||
}
|
||
|
||
module.exports = WebhookDispatch;
|