Node.js 如何正确处理context.sendActivity?
我只想问两个简单的问题,然后出示卡片。问题是,在第二个“sendActivity”中,始终重复“请提供密码”。我试图在函数之后甚至在函数内部放置另一个onTurn,结果是最差或相同的。不要只为了两个问题就实现一个完整的瀑布。哪个ActivityHandler更适合我的目标Node.js 如何正确处理context.sendActivity?,node.js,botframework,Node.js,Botframework,我只想问两个简单的问题,然后出示卡片。问题是,在第二个“sendActivity”中,始终重复“请提供密码”。我试图在函数之后甚至在函数内部放置另一个onTurn,结果是最差或相同的。不要只为了两个问题就实现一个完整的瀑布。哪个ActivityHandler更适合我的目标 async processLogin(context, next, res) { await context.sendActivity({ text: 'please give user
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();
});
}
如果您只想通过一个简单的方法从用户处收集一些信息,您可以一步使用自适应卡,请尝试以下代码:
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;
const{ActivityHandler,CardFactory}=require('botbuilder');
类EchoBot扩展了ActivityHandler{
构造函数(){
超级();
//看https://aka.ms/about-bot-activity-message 了解有关消息和其他活动类型的详细信息。
var自适应卡={
“$schema”:”http://adaptivecards.io/schemas/adaptive-card.json",
“类型”:“AdaptiveCard”,
“版本”:“1.0”,
“正文”:[
{
“类型”:“列集”,
“栏目”:[
{
“类型”:“列”,
“宽度”:2,
“项目”:[
{
“类型”:“文本块”,
“文本”:“请在此处键入您的信息。别担心,我们永远不会共享或出售您的信息。”,
“isSubtle”:没错,
“包装”:正确,
“大小”:“小”
},
{
“类型”:“文本块”,
“文本”:“用户名”,
“包装”:正确
},
{
“类型”:“Input.Text”,
“id”:“用户名”,
“占位符”:“此处为您的用户名”
},
{
“类型”:“文本块”,
“文本”:“密码”,
“包装”:正确
},
{
“类型”:“Input.Text”,
“id”:“密码”,
“占位符”:“确保周围没有人…”
}
]
}
]
}
],
“行动”:[
{
“类型”:“操作.提交”,
“标题”:“提交”
}
]
};
this.onMessage(异步(上下文,下一步)=>{
if(context.activity.text==“登录”){
wait context.sendActivity({附件:[CardFactory.adaptiveCard(adaptiveCard)]});
}else if(context.activity.value!=未定义){
var user=context.activity.value;
wait context.sendActivity(“您好,您的用户名:+user.username+”,密码:+user.password);
}否则{
wait context.sendActivity(“发送登录到do测试”);
}
等待下一个();
});
this.onMembersAdded(异步(上下文,下一步)=>{
const membersAdded=context.activity.membersAdded;
对于(设cnt=0;cnt
此代码基于官方nodejs echo bot,只需覆盖bot.js文件的内容即可对其进行测试:
希望能有帮助 哇!最后一个从自适应卡获得结果的好方法是,我不知道这个文档。谢谢,欢迎光临@DynaBlaster,你能把这个答案标记为奖励吗?LOL:)