本文介绍了使用保存的对话引用并调用trustServiceUrl重新启动后,bot无法进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户安装My Teams应用程序和机器人后,我捕获初始对话事件,并使用该引用向用户主频道发送通知。

但是,如果我重新启动服务器并运行相同的代码,我会收到一个错误,提示该机器人未经授权。

我是在调用文档https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/16.proactive-messages#avoiding-permission-related-errors中指定的turnContext.sendActivity()之前调用MicrosoftAppCredentials.trustedServiceUrl()。在我调用turnContext.sendActivity之前,调用MicrosoftAppCredentials.isTrustedServiceUrl返回true

我基本上复制了此项目中的代码以用于我的实现。

{ Error: Authorization has been denied for this request.
    at new RestError (...../node_modules/@azure/ms-rest-js/lib/restError.ts:18:5)
    at ..../node_modules/@azure/ms-rest-js/lib/policies/deserializationPolicy.ts:117:27
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
  code: undefined,
  statusCode: 401,
  request: 
   WebResource {
     streamResponseBody: false,
     url: 'https://smba.trafficmanager.net/amer/v3/conversations/{convo ID}%40thread.skype/activities/{activiyID}',
     method: 'POST',
     headers: HttpHeaders { _headersMap: [Object] },
     body: '{"type":"message","serviceUrl":"https://smba.trafficmanager.net/amer/","channelId":"msteams","from":{"id":"{ID}","name":"CrossLead Dev Local"},"conversation":{"isGroup":true,"conversationType":"channel","id":"{ID}","tenantId":"{ID}"},"recipient":{"id":"{ID}","aadObjectId":"{ID}"},"text":"The bot encountered an error or bug.","inputHint":"acceptingInput","replyToId":"{ID}"}',
     query: undefined,
     formData: undefined,
     withCredentials: false,
     abortSignal: undefined,
     timeout: 0,
     onUploadProgress: undefined,
     onDownloadProgress: undefined,
     operationSpec: 
      { httpMethod: 'POST',
        path: 'v3/conversations/{conversationId}/activities/{activityId}',
        urlParameters: [Array],
        requestBody: [Object],
        responses: [Object],
        serializer: [Object] } },
  response: 
   { body: '{"message":"Authorization has been denied for this request."}',
     headers: HttpHeaders { _headersMap: [Object] },
     status: 401 },
  body: { message: 'Authorization has been denied for this request.' } }

这仍然是我的概念验证实现,因此所有值都是硬编码的,以使其更容易。

const msBotAdapter = new BotFrameworkAdapter({
  appId: {ID as string}
  appPassword:  {ID as string},
});

//Some people also said to do this, but never used it anywhere. It doesn't seem to help
const msCreditials = new MicrosoftAppCredentials(
  {ID as string},
  {ID as string}
);

//Save Conversation Reference from when the app was installed into teams
const conversationReference: {
  [key: string]: Partial<ConversationReference>;
} = { 
    activityId: '{activity ID}',
    //@ts-ignore
    user: {
      id:
        '{ID}',
      aadObjectId: '{ID}',
    },
    bot: {
      id: '{Bot Id}',
      name: '{Bot Name}',
    },
    //@ts-ignore
    conversation: {
      isGroup: true,
      conversationType: 'channel',
      tenantId: '{tenant ID}',
      id: '19:{ID}@thread.skype',
    },
    channelId: 'msteams',
    serviceUrl: 'https://smba.trafficmanager.net/amer/',
};


const postMessage = async () => {
      MicrosoftAppCredentials.trustServiceUrl(conversationReference.serviceUrl);
      await msBotAdapter.continueConversation(
        conversationReference as any,
        async turnContext => {
          // If you encounter permission-related errors when sending this message, see
          // https://aka.ms/BotTrustServiceUrl
          await turnContext.sendActivity('test message');
        }
      );
}

推荐答案

发现botbuilder依赖项已过时。我正在运行4.7.2,最新版本是4.8.0。令人担忧的是,如此次要的版本发布将使过去的版本变得毫无用处,而且没有任何通知。

这篇关于使用保存的对话引用并调用trustServiceUrl重新启动后,bot无法进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:59