注意:以下翻译的准确性尚未经过验证。这是使用 AIP ↗ 从原始英文文本进行的机器翻译。

配置通知

函数可以被用于灵活地配置应该在平台中发送的通知,包括发送到用户电子邮件地址的外部通知。

在函数中配置通知需要使用主体(表示用户)和notification类型。在阅读本节时,这些参考可能会有帮助:

定义自定义通知

假设Ontology包括一个可以指派给用户的Issue对象。我们希望编写一个函数,以定义应该发送给指定用户的通知,并包含有关Issue的详细信息。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import { EmailNotificationContent, Function, Notification, ShortNotification, User } from "@foundry/functions-api"; import { Issue } from "@foundry/ontology-api"; export class NotificationFunctions { @Function() public createIssueNotification(issue: Issue, user: User): Notification { // 创建一个简短通知,将在平台内显示 const shortNotification = ShortNotification.builder() .heading("New issue") // 通知标题 .content("A new issue has been assigned to you.") // 通知内容 // 链接到平台中的 Issue 对象 .addObjectLink("Issue", issue) .build(); // 定义电子邮件正文。电子邮件正文可以包含无头 HTML,例如数据表 // 注意,我们可以在内容中访问用户和问题的属性 const emailBody = `Hello, ${user.firstName}, A new issue has been assigned to you: ${issue.description}.`; const emailNotificationContent = EmailNotificationContent.builder() .subject("New issue") // 邮件主题 .body(emailBody) // 邮件正文 .addObjectLink("Issue", issue) // 链接到 Issue 对象 .build(); return Notification.builder() .shortNotification(shortNotification) // 设置简短通知 .emailNotificationContent(emailNotificationContent) // 设置电子邮件通知内容 .build(); } }

检索用户和组

除了将用户传递到函数中,您还可以按需检索用户或组。假设 Issue Object 有一个包含 userId 的 指派人 字段,我们想实现一个通知,提醒用户有关该 Issue。 我们可以通过以下方式实现:

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import { EmailNotificationContent, Function, Notification, ShortNotification, User, Users } from "@foundry/functions-api"; import { Issue } from "@foundry/ontology-api"; export class NotificationFunctions { @Function() public async createIssueReminderNotification(issue: Issue): Promise<Notification> { if (!issue.assignee) { // 如果问题没有指派人,则抛出错误 throw new UserFacingError("Cannot create notification for issue without an assignee."); } // 异步获取指派人的用户信息 const user = await Users.getUserByIdAsync(issue.assignee); // 构建电子邮件正文 const emailBody = `Hello, ${user.firstName}, This is a reminder to investigate the following issue: ${issue.description}`. // 你也可以使用这种结构内联构建整个通知 return Notification.builder() .shortNotification(ShortNotification.builder() .heading("Issue reminder") // 简短通知标题 .content("Investigate this issue.") // 简短通知内容 .addObjectLink("Issue", issue) // 添加问题的链接 .build()) .emailNotificationContent(EmailNotificationContent.builder() .subject("New issue") // 邮件主题 .body(emailBody) // 邮件正文 .addObjectLink("Issue", issue) // 添加问题的链接 .build()) .build(); } }

返回接收者

上述记录的 Notification API 允许您返回自定义通知内容。您可以使用函数配置通知的另一种方法是返回通知的接收者列表。为此,只需编写一个返回一个或多个 主体(例如 用户)的函数。在下面的示例中,函数返回了报告问题的用户和当前被指派处理该问题的用户。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import { Function, User, Users } from "@foundry/functions-api"; import { Issue } from "@foundry/ontology-api"; export class NotificationFunctions { /** * 给定一个 Issue,返回代表当前该 Issue 的负责人和最初报告该问题的用户的 Users。 */ @Function() public async getIssueAssigneeAndReporter(issue: Issue): Promise<User[]> { // 检查问题是否有负责人和报告者,如果没有则抛出错误 if (!issue.assignee || !issue.reporter) { throw new UserFacingError("无法为没有负责人或报告者的问题创建通知。"); } // 异步获取问题负责人的用户信息 const user = await Users.getUserByIdAsync(issue.assignee); // 异步获取问题报告者的用户信息 const issueReporter = await Users.getUserByIdAsync(issue.reporter); // 返回包含负责人和报告者的用户数组 return [user, issueReporter]; } }