1

I am using ms bot framework and created a new bot. and deployed the bot connector part on the google firebase functions. But below code does not respond. Below is the code:

const builder = require('botbuilder');
const teams = require('botbuilder-teams');


import * as util from 'util';
const restify = require('restify');


let connector = new teams.TeamsChatConnector({
    appId: 'my app id',
    appPassword: 'my app secret'
});

var server = restify.createServer(); 
server.listen(80, function () {    
  console.log('%s listening to %s', server.name, util.inspect(server.address())); 
});   

server.post('/api/messages', connector.listen());


new builder.UniversalBot(connector, function (session:any) {
    console.log("inside bot got something dude:::::" + session.message.text);

});

the end point in bot settings is configured as:


https://us-central1-xxxx.cloudfunctions.net/api/messages

Expected Behavior: when i send a message to the bot using bot tester then i should be able to see console message "inside bot got something dude:::::" in the firebase functions log.

however, it is never invoked so i am not sure what is missing here.

If you are wondering why we are using google firebase functions rather azure then the answer is we use firebase DB and our bot response need to go into the same firedb. Also we dont have to pay/subscribe for one more cloud just to run a simple node base code.

Moblize IT
  • 1,140
  • 2
  • 18
  • 44
  • On which pricing plan are you? – Renaud Tarnec Aug 11 '19 at 05:48
  • it is . pay as you go blaze plan. that is not the issue as in that case u . can see in the logs for blocked calls – Moblize IT Aug 11 '19 at 07:46
  • Your title seems to say the bot is not responding to a message from Firebase. Then your question seems to say that your bot is sending a message to Firebase and then Firebase isn't responding. Which is it? – Kyle Delaney Aug 12 '19 at 18:26
  • hi sorry for the confusion. Firebase is able to receive the message sent using the bot connected to ms teams. however, the new builder.UniversalBot does not get called which is a problem here – Moblize IT Aug 12 '19 at 22:28
  • It's still unclear what kind of setup you're using. When you say `new builder.UniversalBot` does not get called do you mean the default dialog is never reached? If that's the case then how could the bot send a message? And more importantly, what are you doing with Firebase if you're using Microsoft Teams? Have you read the Azure Bot Service documentation? https://learn.microsoft.com/en-us/azure/bot-service/bot-service-overview-introduction – Kyle Delaney Aug 13 '19 at 20:01
  • I am updating more details on setup etc. – Moblize IT Aug 14 '19 at 04:13
  • What is bot tester? Is that a part of Firebase? Do you mean Robo test? – Kyle Delaney Aug 21 '19 at 01:18
  • Bot tester is part of ms bots framework – Moblize IT Aug 21 '19 at 02:08
  • Are you getting response when you try out bot code [locally using ngrok](https://learn.microsoft.com/en-us/microsoftteams/platform/get-started/get-started-nodejs-app-studio#host-locally-using-ngrok)? – Wajeed Shaikh Aug 21 '19 at 14:01
  • Are you talking about the Bot Framework Emulator? Are you talking about the "Test in Web Chat" blade in Azure? – Kyle Delaney Aug 22 '19 at 01:18
  • well if you create a bot using https://dev.botframework.com/bots/new then you can see a test your bot area. i am talking about that – Moblize IT Aug 22 '19 at 02:44
  • 1
    You're talking about the Web Chat test in the dev portal? https://i.stack.imgur.com/Z5noO.png – Kyle Delaney Aug 22 '19 at 21:58
  • The dev portal is obsolete. All dev portal bot registrations should be migrated to the Azure portal. Don't worry, bot channels registrations in Azure are free. Why are you using the dev portal? – Kyle Delaney Aug 22 '19 at 22:01
  • I'm wondering, do you see the first `console.log` '%s listening to %s' in your Firebase Functions log? – Kyle Delaney Aug 22 '19 at 22:59
  • yes i see the first log listening – Moblize IT Aug 24 '19 at 01:46
  • Can you explain how you've deployed a restify app to Firebase without using the firebase-functions or firebase-admin packages? All the Firebase Functions documentation I've seen has said it's necessary to use those packages in order to deploy to Firebase Functions. – Kyle Delaney Aug 27 '19 at 00:07
  • well i have firebase functions. just did a firebase deploy --only functions and it deployes restify as well along with it – Moblize IT Aug 27 '19 at 04:28
  • If you believe this is possible when it is in fact not possible then you will never solve your issue. If it is possible, I would like to know how so that I may help you. Using your code, `firebase deploy --only functions` did not deploy any functions for me because there were no exports. To help me understand how you are deploying this restify app into Firebase functions, please update your question with screenshots of what you're doing and what happens when you do it. Knowing your folder structure would also help. And please link to some documentation about deploying restify apps to Firebase. – Kyle Delaney Aug 29 '19 at 22:56
  • Is there any chance at all that the problem has to do with you putting a TypeScript type (`any`) into your JavaScript? – Kyle Delaney Aug 29 '19 at 23:03
  • thanks for hanging on this with me. no i think this is just to figure out how to let firebase function handle and route the requests – Moblize IT Aug 30 '19 at 04:53
  • I've submitted an answer to your other question: https://stackoverflow.com/questions/57714788/deploying-microsoft-bots-nodejs-to-firebase-functions – Kyle Delaney Sep 04 '19 at 16:05
  • Possible duplicate of [Deploying microsoft bots nodejs to firebase functions](https://stackoverflow.com/questions/57714788/deploying-microsoft-bots-nodejs-to-firebase-functions) – Kyle Delaney Sep 04 '19 at 16:05
  • Is my answer acceptable? – Kyle Delaney Sep 11 '19 at 22:29

2 Answers2

2

Not sure if this is the issue, but all my Firebase Functions are called using the format

https://us-central1-name_of_app.cloudfunctions.net/name_of_function

whereas you're calling it using the format

https://us-central1-name_of_app.cloudfunctions.net/api/name_of_function

(with the added /api/) which makes it seem like you're calling another website and not the actual function.

You can find the URL your function is hosted at in the main Firebase Functions page by the way.

Hopefully that helps!

PabloJ
  • 199
  • 1
  • 9
  • You are right on that however this is not a function call and it should basically call the restify server rather one of the firebase function. i have tried without api/message as well but no luck – Moblize IT Aug 20 '19 at 04:15
0

From my answer to your other question: Deploying microsoft bots nodejs to firebase functions

I got the echo bot sample to work as a Firebase function. The files from the sample should all go in your functions folder with index.js, bot.js, and .env being the important ones. I made some modifications to index.js so that it uses express and not restify, though it's unclear if that's really necessary. My final index.js looks like this (note the use of the firebase-functions package):

const functions = require('firebase-functions');
const dotenv = require('dotenv');
const path = require('path');
const express = require('express');

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');

// This bot's main dialog.
const { EchoBot } = require('./bot');

// Import required bot configuration.
const ENV_FILE = path.join(__dirname, '.env');
dotenv.config({ path: ENV_FILE });

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new BotFrameworkAdapter({
  appId: process.env.MicrosoftAppId,
  appPassword: process.env.MicrosoftAppPassword
});

// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
  // This check writes out errors to console log .vs. app insights.
  console.error(`\n [onTurnError]: ${error}`);
  // Send a message to the user
  await context.sendActivity(`Oops. Something went wrong!`);
};

// Create the main dialog.
const bot = new EchoBot();

// Listen for incoming requests.    
const app = express();
app.post('/api/messages', (req, res) => {
  adapter.processActivity(req, res, async (context) => {
      // Route to main dialog.
      await bot.run(context);
  });
});

// Expose Express API as a single Cloud Function:
exports.bot = functions.https.onRequest(app);

Your bot's Microsoft app ID and password will need to be in the .env file because they're used to authenticate requests to and from Microsoft Teams. In order to do this, the bot must make requests to an external token server which is naturally not a part of Google. You'll need a paid plan in order for your function to call an external API like that, as you can see here: Use firebase cloud function to send POST request to non-google server

With a free plan, you can still test the function locally with firebase emulators:start. However, it sounds like you already have a paid plan so I believe this shouldn't be a problem for you. Whether running your bot locally or deployed, your endpoint should end with /bot/api/messages (if you use "bot" as your function name like I have). Once you deploy your bot, you can use the deployed endpoint in the settings blade of your bot resource in Azure.

As you can see, you can't just deploy anything to Firebase. You need to include specific Firebase-related code in order to establish the specific cloud functions that are meant to run on Firebase.

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66