首页 > 解决方案 > 路径有问题(nodejs botbuilder)

问题描述

我正在使用 Bot Framework、.env 文件和 JSON 文件开发机器人。问题是我似乎无法显示图标,除非我手动设置路径,如下所示:


var invite = new Welcome(process.env.IconUrl = "C:/Users/2203609/Desktop/Mybot/bot.jpg");

这不是一种实用的方法,因为我们每次转移到另一台计算机时都需要手动更改路径。所以我想出了这个主意。我将展示我的 .js、.env 和 .json 文件。

我创建了3个变量,即:

.js:

const loc = '\\bot.jpg';
const pathname = __dirname;
const assa = pathname + loc;

class welcome(){
    constructor(IconUrl, botVersion) {
    this.IconUrl = IconUrl
    this.BotVersion = botVersion
}

}
async Menu(turnContext) {
    var invite = new Welcome(process.env.IconUrl = assa);
    await turnContext.sendActivity({
        attachments: [invite.welcome()]
    });
}

.env

IconUrl =

"items": [{
     "type": "Image",
     "style": "Person",
     "url": "%IconUrl%",
     "size": "Large"
  }],

输出是:

[onTurnError]: SyntaxError: 位置 633 的 JSON 中的意外标记 U

更新:变量路径名不能用作欢迎类中的参数。

标签: javascriptnode.jspathbotframeworkdirname

解决方案


您构建代码的方式存在一些错误。如果要显示图像,则需要使用卡片。在下面的示例中,我使用的是英雄卡。

此外,“onTurn”方法必须保留该名称。您可以在类中创建具有自己名称的其他方法。这些将反映瀑布对话框中的不同步骤。您可以在此处阅读有关瀑布对话框的更多信息。

const { ActivityTypes, CardFactory } = require('botbuilder');
const loc = '\\bot.jpg';
const pathname = __dirname;
const assa = pathname + loc;

const hero = CardFactory.heroCard(
    'Hero Card',
    CardFactory.images([assa])
);

class Welcome {
    constructor(IconUrl, botVersion) {
        this.IconUrl = IconUrl;
        this.BotVersion = botVersion;
    }

    async onTurn(turnContext) {
        if (turnContext.activity.type === ActivityTypes.Message) {
            if (!turnContext.responded) {
                const reply = { type: ActivityTypes.Message };
                reply.attachments = [hero];
                return await turnContext.sendActivity(reply);
            }
        }
    }
}

module.exports.Welcome = Welcome;

我建议通读文档。更重要的是,我建议您查看Botbuilder-Samples GitHub 存储库上的各种示例。每个示例都建立在前一个示例的基础上,并介绍了核心思想和最佳实践。

希望有帮助!


推荐阅读