Dyna Blaster
Dyna Blaster

Reputation: 35

How to properly handle context.sendActivity?

I just want to ask two simple questions and then show the card. Problem is, in the second "sendActivity" keeps on repeating "please give password" just forever. I tried to place another onTurn after and even inside the function, with worst or same results. Dont want to implement a whole waterfall just for 2 questions. Which ActivityHandler fits better what am trying to achieve?

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();
            });
            }

Upvotes: 1

Views: 1584

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

enter link description hereIf you just want to collect some info from user by an easy , you can use adaptive card in one step, try the code below :

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;

This code is based on official nodejs echo bot , just cover the content of bot.js file to test it : enter image description here

Hope it helps .

Upvotes: 3

Related Questions