5

I send an event named "locationRequest" from my bot (.NET SDK)

            Activity activity = new Activity
            {
                Type = ActivityTypes.Event,
                Name = "locationRequest"
            };
            await stepContext.Context.SendActivityAsync(activity, cancellationToken);

I want to catch this event in the WebChat client application, based on the minizable-web-chat coded in React, and set a boolean locationRequested to True:

const store = createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
      }
    });
  }
  else if(action.name == 'locationRequest'){
    this.setState(() => ({
      locationRequested: true
    }));
  }
  return next(action);
});

I am unable to catch this event, any idea please ?

2 Answers2

4

You're going down the right track. Basically, you can monitor for 'DIRECT_LINE/INCOMING_ACTIVITY' events and then check if the incoming activity has the appropriate name. For more details, take a look at the code snippet below and the Incoming Event Web Chat sample.

const store = createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
      }
    });
  }
  else if(action.type === 'DIRECT_LINE/INCOMING_ACTIVITY'){
    if (action.payload.activity.name === 'locationRequest') {
      this.setState(() => ({
        locationRequested: true
      }));
    }
  }
  return next(action);
});
tdurnford
  • 3,632
  • 2
  • 6
  • 15
  • Thanks a lot it worked ! In my use case, the context is in a ComponentDialog with WaterfallSteps: - the bot is sending a ChoicePrompt "Would you like the bot to get your location ? YES/NO" - If YES, the bot sends a "locationRequest" event - WebChat receives the event and show a GetLocation button - When user clicks on the button, it sends the geolocation to the bot and the dialog continue How can we pause the ComponentDialog to let the user click the button in the webchat ? – Allain Richter Jan 30 '20 at 22:38
  • Glad I could help. If you could mark my answer as accepted, I would appreciate it. In regards to your second question, I would recommend asking a new question on StackOverflow. It requires too much code to cover in the comments. – tdurnford Jan 30 '20 at 23:35
  • My second question is right here: https://stackoverflow.com/questions/59996351/botframework-v4-how-to-exchange-informations-with-webchat-within-componentdialo – Allain Richter Jan 31 '20 at 00:14
3

You are sending an activity from your bot, which means you can try to catch the activity and then you can check if the name of the activity is "locationRequest", and then you change the value of the locationRequested variable.

It would look something like this:

// We are adding a new middleware to customize the behavior of DIRECT_LINE/INCOMING_ACTIVITY.
    const store = window.WebChat.createStore(
      {},
      ({ dispatch }) => next => action => {
       if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
         if (action.payload.activity.name == "locationRequest") {
           this.setState(() => ({ locationRequested: true }));
         }
       }

        return next(action);
      }
    );

Hope this helps! Btw. I based my answer on this sample

Armend Ukehaxhaj
  • 533
  • 6
  • 17