gpt4 book ai didi

node.js - 如何正确处理context.sendActivity?

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:53 26 4
gpt4 key购买 nike

我只想问两个简单的问题,然后出示卡片。问题是,在第二个“sendActivity”中,永远重复“请提供密码”。我尝试在函数之后甚至内部放置另一个 onTurn,结果最差或相同。不想仅仅为了两个问题就实现整个 waterfall 。哪个 ActivityHandler 更适合我想要实现的目标?

async processLogin(context, next, res) {
await context.sendActivity({
text: 'please give username'
})
const SelectedCard2 = CARDS2[0];
this.onTurn(async (context, next, res) => {
let txt = `"${context.activity.text}"`;
if (txt) {
var name = JSON.parse(txt);
console.log(name)
}

await context.sendActivity({
text: 'please give password'
})
let txt2 = `"${context.activity.text}"`;
if (txt2) {
var password = JSON.parse(txt2);
console.log(password)
res = password;
}

await next();
});
}

最佳答案

enter link description here如果您只是想通过简单的方式从用户那里收集一些信息,您可以一步使用自适应卡,请尝试以下代码:

const { ActivityHandler,CardFactory } = require('botbuilder');


class EchoBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.

var adaptiveCard = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": 2,
"items": [
{
"type": "TextBlock",
"text": "Pls type your info here . Don't worry, we'll never share or sell your information.",
"isSubtle": true,
"wrap": true,
"size": "Small"
},
{
"type": "TextBlock",
"text": "Username",
"wrap": true
},
{
"type": "Input.Text",
"id": "username",
"placeholder": "your user name here"
},
{
"type": "TextBlock",
"text": "Password",
"wrap": true
},
{
"type": "Input.Text",
"id": "password",
"placeholder": "makre sure no one is around you ..."
}
]
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit"
}
]
};


this.onMessage(async (context, next) => {

if(context.activity.text==="login"){

await context.sendActivity({ attachments: [CardFactory.adaptiveCard(adaptiveCard)] });

}else if(context.activity.value != undefined){
var user = context.activity.value;
await context.sendActivity("hello , your username : " + user.username + ",password :" + user.password);

}else {
await context.sendActivity("send login to do test");
}






await next();
});

this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});

}
}





module.exports.EchoBot = EchoBot;

此代码基于官方nodejs echo bot,只需覆盖bot.js文件的内容即可测试: enter image description here

希望有帮助。

关于node.js - 如何正确处理context.sendActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com