IMRANKHAN K
IMRANKHAN K

Reputation: 97

How to implement a welcome activity when the bot first starts - NLP is from Google Dialogflow

How to implement a welcome activity when the bot first starts - NLP is from Google Dialogflow.

I have designed the chatbot -intent, entities and NLP from Google Dialogflow and I have integrated successfully with the botframework webchat in a html file on referring this url.

The bot design and also the bot response is good to go. My most expected is am not getting the Bot response first here.

The welcome intent from Google Dialogflow has to get trigger from the following code as per the link given above.

But I am unable to get the bot trigger first here.

How to trigger the event of Google Dialogflow from the code.

I am expecting same like this

Note: Also referred this url

Upvotes: 1

Views: 551

Answers (1)

tdurnford
tdurnford

Reputation: 3712

When a user joins WebChat, a conversation update activity will be sent to the bot. Once the activity is received, you can check if a member was added and send the welcome message accordingly.

If you are using the Activity Handler that was released in v4.3, you can simply add an onMembersAdded handler and send the welcome message from there.

class Bot extends ActivityHandler{

    constructor() {
        super();

        this.onMembersAdded(async (context, next) => {
            const { membersAdded } = context.activity;

            for (let member of membersAdded) {
                if (member.id !== context.activity.recipient.id) {
                   await context.sendActivity("Welcome Message!");
                }
            }
            await next();
        });

        ...
    }
}

If you are not using the activity handler, in the bot's onTurn method, you can check if the incoming activity handler is a conversation update and if a member has been added.

async onTurn(turnContext) {
    if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
        if (turnContext.activity.membersAdded && turnContext.activity.membersAdded.length > 0) {
            for (let member of turnContext.activity.membersAdded) {
                if (member.id !== turnContext.activity.recipient.id) {
                     await turnContext.sendActivity("Welcome Message!");
                }
            }
        }
    } ...
}

For more details on sending welcome messages, please take a look at this sample.

Hope this helps!

Upvotes: 1

Related Questions