程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法发送活动:Bot 返回错误 502大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法发送活动:Bot 返回错误 502?

开发过程中遇到无法发送活动:Bot 返回错误 502的问题如何解决?下面主要结合日常开发的经验,给出你关于无法发送活动:Bot 返回错误 502的解决方法建议,希望对你解决无法发送活动:Bot 返回错误 502有所启发或帮助;

我总是看到以下错误。

POST https://directline.botframework.com/v3/directline/conversations/Jmw1dSA6scX3fV08d7wiJ7-6/activities 502

以下是我用于为部署在 SharePoint 网站上的聊天机器人实现 SSO 的代码。当我打开我的聊天机器人时,我总是得到提示登录 Oauth 卡。单击登录按钮后,我将仅通过传递令牌号进行身份验证。我正在寻找的是在没有登录卡的情况下自动登录,因为我已经登录到 Sharepoint 网站。

@EnableSharedInjection
//**************** All functions are in this block **********************

function onSignin(IDToken)
{
  alert("InsIDe onSignin: " + IDToken);
  let user = clIEntApplication.getAccount();
  alert("User.name: " + user.Name);
  document.getElementByID("username").INNERHTML = "Currently logged in as " + user.name;
  let requestObj1 = {
    scopes: ["user.read",'openID','profile']
  };
}

function onSignInClick()
{
  //console.log("InsIDe onSignInClick");
  let requestObj = {
    scopes: ["user.read",'profile']
  };

  clIEntApplication.loginPopup(requestObj).then(onSignin).catch(function (error) {Console.log(error) });
}

function getoAuthCardresourceUri(activity) {
  if (activity && activity.attachments && activity.attachments[0] &&
       activity.attachments[0].ContentType === 'application/vnd.microsoft.card.oauth' &&
       activity.attachments[0].content.tokenExchangeresourcE) {
     // asking for token exchange with AAD
         return activity.attachments[0].content.tokenExchangeresource.uri;
   }
}

function exchangetokenAsync(resourceUri) {
  let user = clIEntApplication.getAccount();
  if (user) {
     let requestObj = {
       scopes: [resourceUri]
     };
  return clIEntApplication.acquiretokenSilent(requestObj).then(function (tokenResponsE) {
    return tokenResponse.accesstoken;
     })
     .catch(function (error) {
       console.log(error);
     });
     }
     else {
     return Promise.resolve(null);
   }
}

async function fetchJsON(url,options = {}) {
  console.log("url: " + url);
  console.log("options: " + options);
    const res = await fetch(url,{
      ...options,headers: {
           ...options.headers,accept: 'application/Json'
      }});

      if (!res.ok)
      {
        throw new Error(`Failed to fetch JsON due to ${res.status}`);
      }
      console.log("res: " + JsON.Stringify(res));
      return await res.Json();
  }

//**************** All functions are in this block **********************


     console.log('InsIDe MSAL function');

     var clIEntApplication;
     (function ()
     {
       var msalConfig = {
         auth:{
               clIEntID: '<Have removed the clIEnt ID>',authority: 'https://login.microsoftonline.com/<Have removed the directory ID>'
         },cache:{
               cacheLOCATIOn: 'localstorage',storeAuthStateIncookie: true
         }};
       if (!clIEntApplication)
       {
            clIEntApplication = new Msal.UserAgentApplication(msalConfig);
       }
     } ());

(async function main() {

  // Add your BOT ID below

  var BOT_ID = "<Have removed the BOT ID>";
  var theURL = "https://powerva.microsoft.com/API/botmanagement/v1/directline/directlinetoken?botID=" + BOT_ID;

    var userID = clIEntApplication.account?.accountIDentifIEr != null ?
                    ("You-customized-prefix" + clIEntApplication.account.accountIDentifIEr).substr(0,64)
                    : (Math.random().toString() + Date.Now().toString()).substr(0,64);

  // const { token } = await fetchJsON(theURL);
  const {token}  = await fetchJsON(theURL);
  console.log("KMT - Token insIDe main: " + token);

  const directline = window.WebChat.createDirectline({ token });
  console.log("KMT - directline insIDe main: " + JsON.Stringify(directlinE));

  const store = WebChat.createStore({},({ dispatch }) => next => action => {Const { type } = action;
  console.log("KMT - store insIDe main: " + typE);

  if (action.type === 'DIRECT_liNE/CONNECT_FulFILLED')
  {
           dispatch({
              type: 'WEB_CHAT/SEND_EVENT',payload:
             {
                  name: 'startConversation',type: 'event',value:
                {
                    text: "Hello"
                }
               }
              });
               return next(action);
   }
   if (action.type === 'DIRECT_liNE/INCOMING_ACTIVITY')
   {
         const activity = action.payload.activity;
         let resourceUri;
         if (activity.from && activity.from.role === 'bot' && (resourceUri = getoAuthCardresourceUri(activity)))
       {
            exchangetokenAsync(resourceUri).then(function (token) {
            if (token)
          {
            //console.log("InsIDe if token: " + token);
                   directline.postActivity({
                         type: 'invoke',name: 'signin/tokenExchange',value:
                     {
                             ID: activity.attachments[0].content.tokenExchangeresource.ID,connectionname: activity.attachments[0].content.connectionname,token
                         },"from":
                     {
                             ID: userID,name: clIEntApplication.account.name,role: "user"
                         }
                       }).subscribe(ID => {
                            if (ID === 'retry')
                        {   // bot was not able to handle the invoke,so display the oauthCard
                                return next(action);
                            }   // else: tokenexchange successful and we do not display the oauthCard
                         },error => {
                                // an error occurred to display the oauthCard
                                return next(action);
                         }
          );
          return;
        }
      else return next(action);
    });
    }
  else return next(action);
  }
  else return next(action);
  });

  const styLeoptions = {
     // Add styLeoptions to customize Web Chat canvas
     hIDeUploadbutton: true
  };


    window.WebChat.renderWebChat({
            directline: directline,store,userID:userID,styLeoptions
          },document.getElementByID('webchat')
    );
})().catch(err => console.error("An error occurred: " + err));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的无法发送活动:Bot 返回错误 502全部内容,希望文章能够帮你解决无法发送活动:Bot 返回错误 502所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。