【问题标题】:Having problem with path (nodejs botbuilder)路径有问题(nodejs botbuilder)
【发布时间】:2019-03-04 05:52:01
【问题描述】:

我正在使用 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

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

【问题讨论】:

    标签: javascript node.js path botframework dirname


    【解决方案1】:

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

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

    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;
    
    

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

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多