You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义域部署Azure聊天Bot遇Directline 502错误(Node.js)

问题分析与解决指引

首先看你Application Insights里的异常信息:The remote name could not be resolved: 'devbot.corp.xxcloud.net'——这是核心问题:Azure公共的DirectLine服务没办法解析你Bot的内部域名devbot.corp.xxcloud.net,因为这个域名仅在公司内网可用,而DirectLine是Azure云端服务,无法访问内部网络资源。结合你提到的自定义域和Azure AD集成问题,给你整理了以下解决步骤:

1. 先修复Bot端点的可达性问题

  • 你的Bot后端已经部署到公网自定义域test.com,请务必确认Bot的配置端点已经更新为https://test.com/api/messages,而不是原来的内部域名devbot.corp.xxcloud.net。你可以在Azure门户的Bot资源 -> 配置页面里检查并修改这个端点地址
  • 手动验证公网端点的有效性:用Postman或者curl发送POST请求到https://test.com/api/messages,带上模拟的DirectLine请求体,比如:
    {
      "type": "message",
      "from": { "id": "test-user" },
      "text": "hello"
    }
    
    如果能收到正常响应,说明端点没问题;如果还是报错,先解决这个基础的网络可达性问题

2. Azure AD与DirectLine的集成配置

既然你给Bot启用了Azure AD,需要确保DirectLine和Bot之间的认证链路是通的:

Bot端(Node.js)配置认证中间件

在你的Node.js Bot代码中添加Azure AD认证逻辑,验证来自DirectLine的请求是否携带合法的Azure AD令牌:

const { AuthenticationConfiguration, JwtTokenValidation } = require('botframework-connector');
const restify = require('restify');

const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
  console.log(`\n${server.name} listening to ${server.url}`);
});

// 配置Azure AD认证参数
const authConfig = new AuthenticationConfiguration();
authConfig.requiredEndorsements = ['https://api.botframework.com']; // 必须包含这个背书,确保请求来自Bot Framework服务
authConfig.validateIssuer = true;
authConfig.issuer = `https://sts.windows.net/你的AzureAD租户ID/`; // 替换成你的租户ID
authConfig.audience = ['你的Bot应用注册ID']; // 替换成Bot的Azure AD应用ID

// 处理/api/messages请求时先做认证
server.post('/api/messages', async (req, res) => {
  try {
    // 验证请求中的Azure AD令牌
    await JwtTokenValidation.authenticateRequest(req, res, authConfig);
    // 认证通过后处理Bot逻辑
    await adapter.processActivity(req, res, async (context) => {
      await bot.run(context);
    });
  } catch (err) {
    console.error('Azure AD认证失败:', err);
    res.sendStatus(401);
  }
});

DirectLine端配置

在Azure门户的Bot资源 -> 频道 -> DirectLine频道里,找到"Azure AD认证"选项,启用它并关联你的Azure AD应用。这样DirectLine会使用Azure AD令牌与你的Bot通信,确保请求的合法性。

3. 自定义域下的DirectLine额外检查

  • 确认你的自定义域test.com已经配置了有效的SSL证书(Azure App Service的自定义域必须启用HTTPS,DirectLine只支持HTTPS端点)
  • 检查DirectLine令牌的有效性:确保你在customChatbot.html中使用的令牌是针对当前Bot资源生成的,并且没有过期。可以在Azure门户的DirectLine频道里重新生成一个临时令牌测试
  • 你的前端代码里的Bot ID要和Azure门户里的Bot ID一致:bot: { id: 'hpusstoredevbot' },确认这个ID是正确的

4. 进一步调试建议

  • 开启Bot的详细日志:在Application Insights中查看完整的请求轨迹,确认请求是否真的到达了你的Bot后端
  • 用DirectLine REST API测试:直接调用POST https://directline.botframework.com/v3/directline/conversations创建会话,再调用POST /v3/directline/conversations/{conversationId}/activities发送消息,排查是会话创建环节还是消息发送环节出问题
  • 检查公司防火墙规则:确保你的防火墙允许Azure DirectLine服务的IP段访问test.com的443端口

内容的提问来源于stack exchange,提问作者Shiva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 18:17:54