通过


通知代理

重要提示

你需要是边境预览计划的一部分,才能提前访问 Microsoft Agent 365。 边界将你直接与Microsoft最新的 AI 创新联系起来。 边境预览版受客户协议现有预览条款的约束。 由于这些功能仍在开发中,其可用性和功能可能会随时间而变化。

通知模块使开发人员能够生成可以响应来自 Microsoft 365 应用程序的事件和通知的代理。 通过使用通知支持,客服人员可以通过电子邮件、文档评论或其他协作场景与他们互动时接收并处理警报。

通知工作流

按照此工作流为 AI 代理应用程序启用通知:

  1. 安装通知包

  2. 导入通知组件

    • 导入通知类和处理程序
    • 导入活动类型和通道标识符
  3. 注册通知处理程序

    • 使用通知处理程序方法注册路由
    • 针对特定通知类型配置处理程序(例如:电子邮件、Word、Excel、PowerPoint)
  4. 在代理代码中处理通知

    • 代理从 Microsoft 365 应用程序接收通知
    • 处理传入通知并适当响应

通知类型

Agent 365 SDK 支持以下通知类型:

通知类型 Description 子通道 ID
电子邮件 代理人会收到一封邮件,邮件中提到或地址说明了他们 email
Word Word 文档中的注释中提到代理 word
Excel Excel 文档中的注释中提到代理 excel
和 PowerPoint PowerPoint 文档中的注释中提到代理 powerpoint
生命周期事件 代理生命周期通知(用户标识已创建、工作负载载入、用户已删除) 不可用

页面生命周期事件

代理生命周期事件使代理能够响应与代理用户标识管理相关的特定系统事件。 SDK 当前支持三个生命周期事件:

活动类型 事件 ID Description
已创建用户标识 agenticUserIdentityCreated 创建代理用户标识时触发
工作负载载入已更新 agenticUserWorkloadOnboardingUpdated 在更新代理用户的工作负载载入状态时触发
用户已删除 agenticUserDeleted 删除代理用户标识时触发

这些事件允许代理执行初始化任务、清理作或状态管理,以响应用户生命周期更改。

通知有效载荷引用

当你的代理收到通知时,有效载荷包含针对该通知类型的结构化数据。 理解这些负载有助于你提取有效处理通知所需的信息。

电子邮件通知载荷

当用户向你的代理人发送电子邮件或在邮件中提及代理人时,代理人会收到以下结构的电子邮件通知:

{
  "id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
  "timestamp": "2026-02-06T17:45:20.740Z",
  "channelId": "agents",
  "serviceUrl": "http://localhost:56150/_connector",
  "recipient": {
    "id": "AgentName@contoso.onmicrosoft.com",
    "name": "My Agent",
    "agenticUserId": "<agentic-user-id>",
    "agenticAppId": "<agentic-app-id>",
    "tenantId": "<tenant-id>",
    "role": "agenticUser"
  },
  "conversation": {
    "id": "<conversation-id>",
    "conversationType": "personal",
    "tenantId": "<tenant-id>"
  },
  "from": {
    "id": "sender@contoso.onmicrosoft.com",
    "name": "Sender Name",
    "role": "user"
  },
  "type": "message",
  "channelData": {
    "tenant": {
      "id": "<tenant-id>"
    }
  },
  "locale": "en-US",
  "name": "emailNotification",
  "entities": [
    {
      "type": "clientInfo",
      "locale": "en-US",
      "timezone": null
    },
    {
      "id": "email",
      "type": "productInfo"
    },
    {
      "type": "emailNotification",
      "id": "<email-id>",
      "conversationId": "<conversation-id>",
      "htmlBody": "<body dir=\"ltr\">\n<div class=\"elementToProof\">Your email message content here</div>\n</body>"
    }
  ]
}

文档评论通知载荷(Word、Excel、PowerPoint)

当用户在Word、Excel或PowerPoint文档中的评论中提及您的代理时,您的代理会收到WPX(Word、PowerPoint、Excel)评论通知:

{
  "id": "bbbbbbbb-1111-2222-3333-cccccccccccc",
  "timestamp": "2026-02-06T17:46:02.248Z",
  "channelId": "agents",
  "serviceUrl": "http://localhost:56150/_connector",
  "recipient": {
    "id": "AgentName@contoso.onmicrosoft.com",
    "name": "My Agent",
    "agenticUserId": "<agentic-user-id>",
    "agenticAppId": "<agentic-app-id>",
    "tenantId": "<tenant-id>",
    "role": "agenticUser"
  },
  "conversation": {
    "id": "<conversation-id>",
    "conversationType": "personal",
    "tenantId": "<tenant-id>",
    "topic": "<document-topic>"
  },
  "from": {
    "id": "sender@contoso.onmicrosoft.com",
    "name": "Sender Name",
    "role": "user"
  },
  "type": "message",
  "channelData": {
    "tenant": {
      "id": "<tenant-id>"
    },
    "productContext": "Word"
  },
  "locale": "en-US",
  "textFormat": "plain",
  "text": "<at>My Agent</at> - Please review this section\n",
  "attachments": [
    {
      "contentUrl": "<document-url>",
      "name": "<document-name>",
      "content": {
        "uniqueId": "<document-unique-id>",
        "fileType": "docx"
      },
      "contentType": "application/vnd.microsoft.teams.file.download.info"
    }
  ],
  "entities": [
    {
      "type": "clientInfo",
      "locale": "en-US",
      "timezone": null
    },
    {
      "mentioned": {
        "id": "AgentName@contoso.onmicrosoft.com",
        "name": "@My Agent"
      },
      "text": "<at>My Agent</at>",
      "type": "mention"
    },
    {
      "id": "Word",
      "type": "productInfo"
    },
    {
      "parentCommentId": "<parent-comment-id>",
      "commentId": "<comment-id>",
      "documentId": "<document-id>",
      "type": "wpxcomment"
    }
  ]
}

将通知添加到代理

按照以下步骤在现有代理中启用通知处理:

导入通知组件

将这些导入添加到代理文件:

from microsoft_agents_a365 import AgentApplication
from microsoft_agents_a365.notifications import (
    AgentNotification,
    AgentNotificationActivity,
    NotificationTypes
)
from microsoft_agents.activity import ChannelId
from microsoft_agents.hosting.core import Authorization, TurnContext
  • AgentApplication:用于生成 Agent365 应用程序的基类;提供路由活动、管理状态和处理请求的核心功能
  • AgentNotification:用于使用修饰器方法注册通知处理程序的类。 提供 on_agent_notification()on_email()on_word()和其他方便修饰器
  • AgentNotificationActivity:包含具有类型化属性的已分析通知数据的包装器,以及email_notificationwpx_comment_notification包含特定于通知的元数据(ID、聊天详细信息、文档引用)
  • NotificationTypes:支持的通知类型的枚举(EMAIL_NOTIFICATIONWPX_COMMENTAGENT_LIFECYCLE
  • ChannelId:用于指定通知通道(例如, ChannelId(channel="agents", sub_channel="*")
  • 授权:用于处理通知的授权上下文
  • TurnContext:代理 SDK 中的当前对话轮次上下文

在代理中注册通知处理程序

将通知处理程序添加到代理的初始化:

class YourAgent(AgentApplication):
    def __init__(self, app):
        # Create notification handler
        agent_notification = AgentNotification(app)
        
        # Register handler for all notifications
        @agent_notification.on_agent_notification(
            ChannelId(channel="agents", sub_channel="*")
        )
        async def handle_all_notifications(context, state, notification):
            # Route based on notification type
            if notification.notification_type == NotificationTypes.EMAIL_NOTIFICATION:
                await self.handle_email_notification(context, state, notification)
            elif notification.notification_type == NotificationTypes.WPX_COMMENT:
                await self.handle_comment_notification(context, state, notification)
            else:
                await context.send_activity('Notification type not yet implemented.')

实现特定的通知处理程序

为每个通知类型添加处理程序方法:

class YourAgent(AgentApplication):
    # ... __init__ from above ...
    
    async def handle_email_notification(self, context, state, notification):
        """Handle email notifications"""
        email = notification.email_notification
        
        if not email:
            await context.send_activity('No email data found')
            return
        
        # Process the email
        await context.send_activity(
            f'Received email notification. Email ID: {email.id}'
        )
        
        # Your email processing logic here
    
    async def handle_comment_notification(self, context, state, notification):
        """Handle document comment notifications"""
        comment = notification.wpx_comment_notification
        
        if not comment:
            await context.send_activity('No comment data found')
            return
        
        # Process the comment
        await context.send_activity(
            f'Received comment notification. Document ID: {comment.document_id}'
        )
        
        # Your comment processing logic here

专用通知处理程序

设置 基本通知路由后,使用专用处理程序方法进行更精细的控制。 这些应用可便于:

  • 为同一通知类型注册多个处理程序
  • 使用排名设置处理程序优先级
  • 为每个处理程序配置自动身份验证

备注

对于大多数用例,泛型处理程序模式就足够了。 需要高级路由或多个相同通知类型的处理程序时,请使用这些专用处理程序。

所有通知的专用处理程序

注册处理所有通知类型的更多处理程序:

from microsoft_agents_a365.notifications import (
    AgentNotification,
    NotificationTypes
)
from microsoft_agents.activity import ChannelId

# Create notification handler
agent_notification = AgentNotification(app)

# Register handler for all notifications
@agent_notification.on_agent_notification(
    ChannelId(channel="agents", sub_channel="*")
)
async def handle_all_notifications(context, state, notification):
    if notification.notification_type == NotificationTypes.EMAIL_NOTIFICATION:
        if notification.email_notification:
            await context.send_activity(f"Received email: {notification.email_notification.id}")
    elif notification.notification_type == NotificationTypes.WPX_COMMENT:
        if notification.wpx_comment_notification:
            await context.send_activity(f"Received comment: {notification.wpx_comment_notification.comment_id}")

用于电子邮件通知的专用处理程序

为电子邮件通知注册更多处理程序:

from microsoft_agents_a365.notifications import AgentNotification
from microsoft_agents.activity import ChannelId, AgentSubChannel

# Create notification handler
agent_notification = AgentNotification(app)

# Use the convenience method for email notifications
@agent_notification.on_email()
async def handle_email(context, state, notification):
    email = notification.email_notification
    
    if not email:
        await context.send_activity('No email found')
        return
    
    # Process the email
    email_id = email.id
    conversation_id = email.conversation_id
    
    # Send response
    await context.send_activity('Thank you for your email!')

文档注释的专用处理程序

为 Word、Excel 和 PowerPoint 注释通知注册更多处理程序:

from microsoft_agents_a365.notifications import AgentNotification

# Create notification handler
agent_notification = AgentNotification(app)

# Use convenience methods for document notifications
@agent_notification.on_word()
async def handle_word(context, state, notification):
    comment = notification.wpx_comment_notification
    
    if comment:
        document_id = comment.document_id
        comment_id = comment.comment_id
        await context.send_activity(f'Processing Word comment: {comment_id}')

@agent_notification.on_excel()
async def handle_excel(context, state, notification):
    comment = notification.wpx_comment_notification
    
    if comment:
        await context.send_activity('Processing Excel comment')

@agent_notification.on_powerpoint()
async def handle_powerpoint(context, state, notification):
    comment = notification.wpx_comment_notification
    
    if comment:
        await context.send_activity('Processing PowerPoint comment')

生命周期事件的专用处理程序

为代理生命周期事件(例如用户标识创建、工作负荷载入和用户删除)注册更多处理程序:

from microsoft_agents_a365.notifications import AgentNotification

# Create notification handler
agent_notification = AgentNotification(app)

# Handle all lifecycle events
@agent_notification.on_agent_lifecycle_notification("*")
async def handle_lifecycle(context, state, notification):
    lifecycle_notification = notification.agent_lifecycle_notification
    if lifecycle_notification:
        event_type = lifecycle_notification.lifecycle_event_type
        
        if event_type == "agenticUserIdentityCreated":
            await context.send_activity('User identity created')
        elif event_type == "agenticUserWorkloadOnboardingUpdated":
            await context.send_activity('Workload onboarding completed')
        elif event_type == "agenticUserDeleted":
            await context.send_activity('User identity deleted')

高级配置

本部分介绍用于微调通知处理程序的高级配置选项。 这些配置允许你控制处理程序执行顺序、管理身份验证要求,以及优化复杂方案的通知处理。

处理程序优先级和排名

使用多个专用处理程序时,可以使用排名值指定优先级顺序。 值越低,优先级越高。

from microsoft_agents_a365.notifications import AgentNotification
from microsoft_agents.activity import ChannelId, AgentSubChannel

# Create notification handler
agent_notification = AgentNotification(app)

# Higher priority handler (processed first)
@agent_notification.on_email(rank=100)
async def high_priority_email(context, state, notification):
    # Handle with high priority
    pass

# Lower priority handler (processed after higher priority)
@agent_notification.on_email(rank=200)
async def low_priority_email(context, state, notification):
    # Handle with lower priority
    pass

身份验证处理程序

为需要身份验证的通知配置自动登录处理程序:

from microsoft_agents_a365.notifications import AgentNotification
from microsoft_agents.activity import ChannelId, AgentSubChannel

# Create notification handler
agent_notification = AgentNotification(app)

# Handler with automatic authentication
@agent_notification.on_email(auto_sign_in_handlers=['agentic'])
async def authenticated_email(context, state, notification):
    # Authentication is handled automatically
    pass

测试和监视

使用通知测试代理

实现通知处理程序后,测试代理以确保它正确接收和处理不同的通知类型。 按照测试指南设置环境,然后主要关注“使用通知活动测试”部分,使用代理身份验证验证通知。

监视通知处理

通过添加可观测性功能监视代理的通知处理。 跟踪通知处理、响应时间和错误率,以了解代理性能。 详细了解如何实现跟踪和监视