面向客户的警报的Slack集成
Adobe Experience Platform允许您在Adobe App Builder上使用webhook代理在中接收Adobe I/O EventsSlack。 代理处理Adobe的验证握手并将事件负载转换为Slack消息,以便您能够获取发送到工作区的面向客户的警报。
先决条件 prerequisites
开始之前,请确保您满足以下条件:
- Adobe Developer Console访问权限:启用了App Builder的组织中的系统管理员或开发人员角色。
- Node.js和npm: Node.js(建议使用LTS),其中包含用于安装Adobe CLI和项目依赖项的npm。 有关详细信息,请参阅下载Node.js和npm快速入门指南。
- Adobe I/O CLI:从终端安装Adobe I/O CLI:
npm install -g @adobe/aio-cli。 - 带有传入Webhook的Slack应用程序:您的工作区中启用了 传入Webhook 的Slack应用程序。 请参阅创建Slack应用程序和Slack传入Webhook指南,以创建该应用程序并获取webhook URL(格式:
https://hooks.slack.com/...)。
设置模板化项目 templated-project
要设置模板化项目,请登录到Adobe Developer Console,然后从 Create project from template 选项卡中选择Home。
选择 App Builder 模板,然后输入 Project Title 并选择Add workspace。 最后,选择Save。
您将收到项目已创建并移至 Project overview 选项卡的确认。 从此处,您可以添加Project description。
初始化项目 initialize-project
设置模板化项目后,初始化该项目。
-
打开终端并输入以下命令以登录Adobe I/O。
code language-bash aio login -
初始化应用程序并提供名称。
code language-bash aio app init slack-webhook-proxy -
使用箭头键选择您的
Organization,然后选择您之前在Developer Console中创建的Project。 选择要搜索的模板Only Templates Supported By My Org。
-
接下来,按 Enter 跳过模板并安装独立应用程序。
-
指定要为此项目启用的Adobe I/O应用程序功能。 使用箭头键滚动并选择
Actions: Deploy Runtime actions。
-
使用箭头键滚动并选择要创建的示例操作类型的
Adobe Experience Platform: Realtime Customer Profile。
-
为要添加到模板的UI滚动并选择
Pure HTML/JS。 按 Enter 保留示例操作为默认值,然后再次按 Enter 保留该名称为默认值。
您会收到应用程序初始化已完成的确认。
-
导航到项目目录。
code language-bash cd slack-webhook-proxy -
添加Web操作。
code language-bash aio app add action -
选择
Only Action Templates Supported By My Org。此时将显示模板列表。
-
通过按空格键选择模板,然后使用您的
@adobe/generator-add-publish-events向上 和 向下箭头导航到。 最后,按 空格键 并按 Enter 来选择模板。
将显示已安装
npm package @adobe/generator-add-publish-events的确认。 -
命名操作
webhook-proxy。
此时将显示模板已安装的确认消息。
创建文件操作并部署 create-file-actions
添加代理代码,设置环境变量,然后部署。 该操作随后将在Developer Console中可用于注册。
实施运行时代理 runtime-proxy
导航到项目文件夹并打开文件actions/webhook-proxy/index.js。 删除内容并替换为以下内容:
const fetch = require("node-fetch");
const { Core } = require("@adobe/aio-sdk");
/**
* Adobe I/O Events to Slack Runtime Proxy
*
* Receives events from Adobe I/O Events and forwards them to Slack.
* Signature verification and challenge handling are automatic when
* using Runtime Action registration (non-web action).
*/
async function main(params) {
const logger = Core.Logger("runtime-proxy", { level: params.LOG_LEVEL || "info" });
try {
logger.info(`Event received: ${JSON.stringify(params)}`);
// Forward to Slack
return forwardToSlack(params, params.SLACK_WEBHOOK_URL, logger);
} catch (error) {
logger.error(`Error: ${error.message}`);
return { statusCode: 500, body: { error: "Internal server error" } };
}
}
/**
* Forwards the event payload to Slack
*/
async function forwardToSlack(payload, webhookUrl, logger) {
if (!webhookUrl) {
logger.error("SLACK_WEBHOOK_URL not configured");
return { statusCode: 500, body: { error: "Server configuration error" } };
}
// Extract Adobe headers passed to runtime action
const headers = {
"x-adobe-event-code": payload["x-adobe-event-code"],
"x-adobe-event-id": payload["x-adobe-event-id"],
"x-adobe-provider": payload["x-adobe-provider"]
};
const slackMessage = buildSlackMessage(payload, headers);
const response = await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(slackMessage)
});
if (!response.ok) {
const errorText = await response.text();
logger.error(`Slack API error: ${response.status} - ${errorText}`);
return { statusCode: response.status, body: { error: errorText } };
}
logger.info("Event forwarded to Slack");
return { statusCode: 200, body: { success: true } };
}
/**
* Builds a Slack Block Kit message from the event payload
*/
function buildSlackMessage(payload, headers) {
// Adobe passes event code as x-adobe-event-code header (available in params for runtime actions)
const eventType = headers["x-adobe-event-code"] ||
payload["x-adobe-event-code"] ||
payload.event_code ||
payload.type ||
payload.event_type ||
"Adobe Event";
const eventId = headers["x-adobe-event-id"] || payload["x-adobe-event-id"] || payload.event_id || payload.id || "N/A";
const eventData = payload.data || payload.event || payload;
return {
blocks: [
{
type: "header",
text: { type: "plain_text", text: `Event: ${eventType}`, emoji: true }
},
{
type: "section",
fields: formatDataFields(eventData)
},
{ type: "divider" },
{
type: "context",
elements: [{
type: "mrkdwn",
text: `*Event ID:* ${eventId} | *Time:* ${new Date().toISOString()}`
}]
}
]
};
}
/**
* Formats event data as Slack mrkdwn fields
*/
function formatDataFields(data, maxFields = 10) {
if (typeof data !== "object" || data === null) {
return [{ type: "mrkdwn", text: `*Payload:*\n${String(data)}` }];
}
const entries = Object.entries(data);
if (entries.length === 0) {
return [{ type: "mrkdwn", text: "_No data provided_" }];
}
return entries.slice(0, maxFields).map(([key, value]) => ({
type: "mrkdwn",
text: `*${key}:*\n${typeof value === "object" ? `\`\`\`${JSON.stringify(value)}\`\`\`` : value}`
}));
}
exports.main = main;
在app.config.yaml中配置操作 app-config
app.config.yaml中的操作配置是关键的。 您必须使用web: no创建可以在Developer Console中注册为运行时操作的非Web操作。导航到项目文件夹并打开app.config.yaml。 将内容替换为以下内容:
application:
runtimeManifest:
packages:
slack-webhook-proxy:
license: Apache-2.0
actions:
webhook-proxy:
function: actions/webhook-proxy/index.js
web: no
runtime: nodejs:22
inputs:
LOG_LEVEL: info
SLACK_WEBHOOK_URL: $SLACK_WEBHOOK_URL
annotations:
require-adobe-auth: false
final: true
环境变量 environment-variables
要安全地管理凭据,请使用环境变量。 在项目的根目录中修改.env文件并添加:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBHOOK/URL
部署操作 deploy-action
设置环境变量后,部署操作。 在终端中运行此命令时,请确保您位于项目的根目录下(slack-webhook-proxy):
aio app deploy
将显示部署成功的确认。
向Adobe I/O Events注册操作 register-events
部署操作后,将其注册为Adobe I/O Events的目标。
在Developer Console中,打开您的App Builder项目,然后选择您的Workspace。
在Workspace概述页面上,选择 Add service 和Event。
在“添加事件”页面上,选择 Experience Platform 和Platform notifications,然后选择Next。
选择要接收通知的事件,然后选择Next。
选择您的服务器到服务器身份验证凭据,然后选择Next。
为注册输入 Event registration name 和清除Event registration description,然后选择Next。
选择 Runtime Action 作为传递方式以及您创建的slack-webhook-proxy/runtime-proxy操作,然后选择Save configured events。
webhook代理现已配置完成。 您将返回到Webhook代理页面。 您可以通过选择任何已配置事件旁边的 Send sample event 图标,端到端地测试整个流。