BotFramework DateTimePrompt locale not working

81 views Asked by At

I am using bitframework 4 on nodejs. I cant get the DateTimePrompt to use locale 'en-GB' which has date format dd/mm (not mm/dd) I set locale in 3 places none worked:

    adapter.processActivity(req, res, (context) => {
      context.activity.locale = 'en-GB';
      return myChatBot.run(context);
    };
    this.addDialog(new DateTimePrompt('startDatePrompt', undefined, 'en-GB'));

and in the webchat frame:

    window.WebChat.renderWebChat(
      {
        directLine: window.WebChat.createDirectLine({ token: token }),
        locale: 'en-GB',
        webSpeechPonyfillFactory
      },
      document.getElementById('webchat')
    );

I just want the date parser to parse as dd/mm not mm/dd any thoughts?

1

There are 1 answers

0
Hessel On

Create a customer prompt with regex expression. This will give you the flexibility you need.

In your constructor use a TextPrompt and pass the promptvalidator

this.addDialog(new TextPrompt(DATE_PROMPT, this.datePromptValidator));

In the dialogstep with the prompt.

    async dateStep(stepContext) {
        await helper.say(stepContext, 'Please enter a date');
        const promptOptions = { retryPrompt: 'This is not a valid date. Please use mm/dd' };
        return await stepContext.prompt(WHEN_PROMPT, promptOptions);
    }

Since the promptvalidator will use turnState (works for me) to share the result with the dialogstep, we check for this in the next dialogStep

    async stepafterDateStep(stepContext) {
        stepContext.values.date = stepContext.context.turnState.match;
    }

And the promptvalidator uses a regular expression to check for the right format. These regex expression tend to become complex. This one checks for dd/mm/yyyy. There are libraries available like https://regexlib.com/

    async datePromptValidator(promptContext) {
               const regex = new RegExp(/(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))/);

        var matches = promptContext.recognized.value.match(regex);
        if (matches) {
            promptContext.context.turnState.match = matches[0];
            return true;
        } else { return false; };
    }