javascript - 路径有问题(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
更新:变量路径名不能用作欢迎类中的参数。
解决方案
您构建代码的方式存在一些错误。如果要显示图像,则需要使用卡片。在下面的示例中,我使用的是英雄卡。
此外,“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 存储库上的各种示例。每个示例都建立在前一个示例的基础上,并介绍了核心思想和最佳实践。
希望有帮助!
推荐阅读
- sanity - Groq:加入文档字段
- python - 如何处理其中一个字段中有逗号的 csv 文件中的数据?
- instagram - 如何将我自己的特定 end_cursor 传递给 Instagram 的 API(从关注者列表中的任意位置获取结果)?
- python - 根据熊猫中的条件将年份添加到日期列
- java - 无法让我的 Java 程序再次请求输入
- kubernetes - Kubernetes AWS NLB 服务创建过多的安全组规则
- ios - 如何最好地在 Swift 中存储地图/数组数据以在 Firestore 中使用
- python - 我可以在 python 函数的参数中强制执行 lambda 吗?
- javascript - 阻止或侦听用户在开发人员工具中删除事件侦听器?
- ios - Swift TableViewCell 客户 didSelect