microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.5k stars 2.45k forks source link

(node:14) UnhandledPromiseRejectionWarning: Error: BotFrameworkAdapter.processActivity(): 401 ERROR #6567

Closed barissecen closed 1 year ago

barissecen commented 1 year ago

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

"botbuilder": "^4.4.0"

Describe the bug

Suddenly started to getting an error from BotFrameworkAdapter.processActivity(): 401 ERROR.

Here is the general explanation of What I am trying to do; I have a services where getting post calls from teams chatbot using azure bot services in order to reply queries coming from end users. This service has been. working for a while but couple of days ago, it started to getting error. My code block and logs details were pasted below. Thanks for any update.

const {ActivityTypes,
    CardFactory,
    MessageFactory,
    BotFrameworkAdapter} = require('botbuilder');
  const protoToJson = require('../botlib/proto_to_json.js');
  const dialogflowSessionClient =
   require('../botlib/dialogflow_session_client.js');
  const filterResponses = require('../botlib/filter_responses.js');
  const express = require('express');
  const projectId = 'myprojectid';
  const appId = 'myappid';
  const appPassword = 'myapppassword';
  const sessionClient = new dialogflowSessionClient(projectId);

  // Create bot adapter, which defines how the bot sends and receives messages.
  let adapter = new BotFrameworkAdapter({
    appId: appId,
    appPassword: appPassword
  });

  const port = process.env.PORT;

  const app = express();
  const listener = app.listen(port, () => {
    console.log('Your Skype integration server is listening on port '
        + listener.address().port);
  });
  app.post('/', (req, res) => {
    // Use the adapter to process the incoming web request into a TurnContext object.
        adapter.processActivity(req, res, async (turnContext) => {
            await bot.run(context);
            if (isMessage(turnContext)) {
                const utterance = getMessageText(turnContext);
                const senderId = turnContext.activity.from.id;
                const payload = turnContext.activity;
                const responses = (await sessionClient.detectIntent(
                    utterance, senderId, payload)).fulfillmentMessages;
                const replies = await convertToSkypeMessage(turnContext, responses);
                await turnContext.sendActivities(replies);
              } else if(isMemberAdded(turnContext)) {
                for (let idx in turnContext.activity.membersAdded) {
                  if (turnContext.activity.membersAdded[idx].id !==
                      turnContext.activity.recipient.id) {
                    const result = await sessionClient.detectIntentWithEvent('SKYPE_WELCOME',
                        projectId);
                    const replies = await convertToSkypeMessage(turnContext,
                        result.fulfillmentMessages);
                    await turnContext.sendActivity(replies);
                  }
                }
              }
        });
  });

Error message;

(node:14) UnhandledPromiseRejectionWarning: Error: BotFrameworkAdapter.processActivity(): 401 ERROR
TypeError: Right-hand side of 'instanceof' is not an object
at /usr/src/app/node_modules/jsonwebtoken/verify.js:120:58
at getSecret (/usr/src/app/node_modules/jsonwebtoken/verify.js:97:14)
at Object.module.exports [as verify] (/usr/src/app/node_modules/jsonwebtoken/verify.js:101:10)
at JwtTokenExtractor.<anonymous> (/usr/src/app/node_modules/botframework-connector/lib/auth/jwtTokenExtractor.js:130:54)
at Generator.next (<anonymous>)
at fulfilled (/usr/src/app/node_modules/botframework-connector/lib/auth/jwtTokenExtractor.js:12:58)
at process._tickCallback (internal/process/next_tick.js:68:7).......
Screenshot 2023-01-17 at 14 36 30
anishprasad01 commented 1 year ago

Hi @barissecen,

It's possible that this is caused by old packages that are incompatible with newer Azure Bot Service updates. Could you try updating your Bot Builder SDK packages in your dev environment and see if that resolves the issue?

Also note that your Node version must be greater than 11. If you are using restify, note that you must use version 10.0.0 if your Node version is 18 or greater.

I would recommend periodically updating your software to avoid potential incompatibilities in the future. For now, please try updating the Bot Builder SDK (and the Node and restify versions if needed) and let me know the results.

barissecen commented 1 year ago

Hi @barissecen,

It's possible that this is caused by old packages that are incompatible with newer Azure Bot Service updates. Could you try updating your Bot Builder SDK packages in your dev environment and see if that resolves the issue?

Also note that your Node version must be greater than 11. If you are using restify, note that you must use version 10.0.0 if your Node version is 18 or greater.

I would recommend periodically updating your software to avoid potential incompatibilities in the future. For now, please try updating the Bot Builder SDK (and the Node and restify versions if needed) and let me know the results.

I have updated node engine to 11 and botbuilder to 4.19.0 but still the same error. ; Error finding key for token. Available keys: -----BEGIN RSA PUBLIC KEY-----... (node:14) UnhandledPromiseRejectionWarning: Error: BotFrameworkAdapter.processActivity(): 401 ERROR

anishprasad01 commented 1 year ago

My apologies, I forgot to mention that upgrading to newer SDKs also requires transitioning from the outdated BotFrameworkAdapter to CloudAdapter.

The easiest way to accomplish this would be to compare the index.js and other config files in your project with those of a clean sample, such as JS Sample 02: Echo Bot.

If you can, please try this and let me know if there is any change.

barissecen commented 1 year ago

My apologies, I forgot to mention that upgrading to newer SDKs also requires transitioning from the outdated BotFrameworkAdapter to CloudAdapter.

The easiest way to accomplish this would be to compare the index.js and other config files in your project with those of a clean sample, such as JS Sample 02: Echo Bot.

If you can, please try this and let me know if there is any change.

This time getting following error; POST400 864 B 2.9 s Microsoft-SkypeBotApi (Microsoft-BotFramework/3.0).

anishprasad01 commented 1 year ago

Is this a Bot Framework v3 bot?

barissecen commented 1 year ago

Bot Framework v3

hmm Where can I check it?

anishprasad01 commented 1 year ago

Package versions would be the most likely. v3 package versions start with 3, v4 starts with 4. If your screenshot above is all the packages in your solution, then you have v4, so that should be ok.

What is Microsoft-SkypeBotApi? Is it a package you downloaded, or is it code you wrote? You're getting a 400 error after updating to CloudAdapter, so this package might be incompatible with the newer versions.

I'm trying to narrow down where exactly the error is coming from, and if a service-side change is the cause.

barissecen commented 1 year ago

Package versions would be the most likely. v3 package versions start with 3, v4 starts with 4. If your screenshot above is all the packages in your solution, then you have v4, so that should be ok.

What is Microsoft-SkypeBotApi? Is it a package you downloaded, or is it code you wrote? You're getting a 400 error after updating to CloudAdapter, so this package might be incompatible with the newer versions.

I'm trying to narrow down where exactly the error is coming from, and if a service-side change is the cause.

I have no idea where this comes from. But just paste the error below; By the way, I also put my package.json.

httpRequest: { latency: "4.050338439s" protocol: "HTTP/1.1" remoteIp: "52.113.199.3" requestMethod: "POST" requestSize: "2475" requestUrl: "https://dialogflow-team-7ytcvganva-ew.a.run.app/" responseSize: "864" serverIp: "216.239.38.53" status: 400 userAgent: "Microsoft-SkypeBotApi (Microsoft-BotFramework/3.0)" }


package.json;

{ "name": "skype-integration", "version": "0.0.1", "description": "A skype bot with dialogflow integration.", "main": "server.js", "scripts": { "start": "node server.js", "test": "mocha" }, "dependencies": { "botbuilder": "4.19.0", "dialogflow": "1.2.0", "express": "4.18.2", "dotenv": "^8.2.0" }, "devDependencies": { "mocha": "6.1.4" }, "engines": { "node": "18.13.0" }, "repository": { "url": "insert url here" }, "license": "Apache-2.0" }

barissecen commented 1 year ago

Also I have put my .env;

MicrosoftAppType=MultiTenant MicrosoftAppId=5.... MicrosoftAppPassword=...... MicrosoftAppTenantId=

anishprasad01 commented 1 year ago

Let's recap for a moment for clarity.

As I understand it:

Now, have you checked to see what is wrong with the request to Dialogflow? What is the full text of the error message from Dialogflow?

barissecen commented 1 year ago

I have solved the problem by migrating to restify from express.