【问题标题】:Deploying microsoft bots nodejs to firebase functions将 microsoft bots nodejs 部署到 firebase 函数
【发布时间】:2020-01-03 00:03:47
【问题描述】:

我使用带有 nodejs 的 microsoft bot 框架开发了一个简单的 bot。代码只是两个文件,如下所示。

我在 google firebase 函数文档上看到 https://firebase.google.com/docs/functions/http-events#using_existing_express_apps

看来我可以简单地将我的 ms bot nodejs 部署到 firebase。问题是我应该怎么做。

app.js

'use strict';

var express = require('express');
var app = express();

// Adding a bot to our app
var bot = require('./bot');
bot.setup(app);

// Adding a messaging extension to our app
var messagingExtension = require('./messaging-extension');
messagingExtension.setup();

// Deciding which port to use
var port = process.env.PORT || 3333;

// Start our nodejs app
app.listen(port, function() {
    console.log(`App started listening on port ${port}`);
});

bot.js

'use strict';

module.exports.setup = function(app) {
    var builder = require('botbuilder');
    var teams = require('botbuilder-teams');
    var config = require('config');

    if (!config.has("bot.appId")) {
        process.env.NODE_CONFIG_DIR = "../config";
        delete require.cache[require.resolve('config')];
        config = require('config');
    }

    // Create a connector to handle the conversations
    var connector = new teams.TeamsChatConnector({
       appId: config.get("bot.appId"),
       appPassword: config.get("bot.appPassword")
    });

    var inMemoryBotStorage = new builder.MemoryBotStorage();

    var bot = new builder.UniversalBot(connector, async function(session) {

        session.send("Hello world");  

    }).set('storage', inMemoryBotStorage);

    // Setup an endpoint on the router for the bot to listen.
    // NOTE: This endpoint cannot be changed and must be api/messages
    app.post('/api/messages', connector.listen());

    // Export the connector for any downstream integration - e.g. registering a messaging extension
    module.exports.connector = connector;
};

【问题讨论】:

  • 如果你只有两个文件,那么'./messaging-extension'是什么?
  • Bot Builder v3 不再受支持。您会接受一个向您展示如何使用 Bot Builder v4 执行此操作的答案吗?
  • 可以,只要我可以运行它并部署在 firebase 功能上
  • 是的,即使这样,firebase 托管也很好。任何 firebase 解决方案都可以使用,因为与 firebase db 保持联系非常容易。无论是 firebase 功能还是 firebase 托管。

标签: node.js firebase google-cloud-functions botframework


【解决方案1】:

我将echo bot sample 用作 Firebase 功能。示例中的文件都应该放在您的 functions 文件夹中,其中 index.js、bot.js 和 .env 是重要的。我对 index.js 进行了一些修改,使其使用 express 而不是 restify,但目前还不清楚这是否真的有必要。我最终的 index.js 看起来像这样(注意使用 firebase-functions 包):

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);

您的机器人的 Microsoft 应用 ID 和密码需要在 .env 文件中,因为它们用于验证与 Microsoft Teams 之间的请求。为此,机器人必须向自然不属于 Google 的外部令牌服务器发出请求。您需要付费计划才能让您的函数调用这样的外部 API,如您在此处看到的:Use firebase cloud function to send POST request to non-google server

使用免费计划,您仍然可以使用firebase emulators:start 在本地测试该功能。但是,听起来您已经有了付费计划,所以我相信这对您来说应该不是问题。无论是在本地运行你的机器人还是部署,你的端点都应该以/bot/api/messages 结尾(如果你像我一样使用“bot”作为你的函数名)。部署机器人后,您可以在 Azure 中机器人资源的设置刀片中使用已部署的端点。

【讨论】:

  • 很抱歉花了很长时间才回到这里。有 2 个问题: 1. 我在上面的代码中遇到错误。 TypeError: BotFrameworkAdapter is not a constructor at Object. (/Users/moblizeit/code-repo/apps/business-card-scanner/functions/lib/index.js:73:19) 在 Module._compile (internal/ modules/cjs/loader.js:688:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) 在 Module.load (internal/modules/cjs/loader.js :598:32) Q2: ./bot.ts 文件中的最小代码是什么
  • @MobilizeIT - 您需要确保已安装所有必要的软件包,例如 botbuilder。您可以使用npm i 安装示例所需的包。我已经将您链接到回答您的第二个问题的示例。请记住接受这个答案。
  • 我确实做过 npm i botbuilder 但没有运气。我的 google firebase 函数仍然失败,并出现错误错误:解析函数触发器时发生错误。 TypeError: BotFrameworkAdapter is not a constructor at Object. (/Users/moblizeit/code-repo/apps/business-card-scanner/functions/lib/index.js:71:19) 在 Module._compile (internal/模块/cjs/loader.js:688:30)
  • @MoblizeIT - 您的项目安装了哪个版本的botbuilder
  • "botbuilder": "^3.16.0", "botbuilder-teams": "^0.2.7",
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2012-08-10
  • 2021-08-13
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 2017-01-04
相关资源
最近更新 更多