Validating currency with symbols and letters

0

Hello I have a basic bot that asks a user how much money they currently have, the bot then takes the response and gives it back. Currently I am using Amazon's built in slot type AMAZON.Number. As an example:

  • bot - How much money do you have?
  • user - $250
  • (bot doesn't understand and re-asks the same question)
  • bot - How much money do you have?
  • user - 2k
  • (bot doesn't understand and re-asks the same question)
  • bot - How much money do you have?
  • user - 2000
  • bot - great, you have $2000.00

is there anyway to validate the number so that I can pass symbols such as the US dollar sign "$" and the number K, so that 3k is "$3000"?

Basically the bot is not registering the "$" and "k" as valid input.

asked a year ago296 views
2 Answers
2
Accepted Answer

you can use a lambda function as a custom slot this function can modify the user input according to your requirements

for example you can complete the following

exports.handler = (event, context, callback) => {
    var userMoney = event.currentIntent.slots.Currency; // get the user's response
    userMoney = userMoney.replace('$', ''); // remove dollar sign
    userMoney = userMoney.replace('k', '000'); // replace 'k' with '000'
    userMoney = parseFloat(userMoney); // convert the result to a number

    if (isNaN(userMoney)) {
profile picture
EXPERT
answered a year ago
  • Good shout

  • this is most likely what I will do which is validate the number being inserted into the slot. Like the first comment I thought there would be a way to do regex in the slot options in the lex dashboard rather than a codehook.

  • Can we use Amazon v1 slots, inside of v1 they had a slot value named AMAZON.US_DOLLAR but now is no longer there, is there a way to use that?

  • as i know AMAZON.US_DOLLAR from Amazon Lex v1 is not directly available in Amazon Lex v2 that us why i tried to give a workaround

    https://docs.aws.amazon.com/lexv2/latest/dg/grammar-industry.html

  • I ended up manually validating the currency using regex in lambda, and re-inserting the value without symbols, and if the value had a lowercase and uppercase k, I multiplied it by 1000. So I will mark this your @sedat_salman as the correct answer! :)

0

Im not a developer but how about using a regex expression to only return numbers and ignore anything else

/\d+.\d+

Or use AMAZON.AlphaNumeric using a regular expression which allows you to define a validation

profile picture
EXPERT
answered a year ago
  • I tried it using a custom slot, and Slot value resolution > Restrict to slot values > adding that regex and it does not work. Amazon Lex throws an error "The slot value '/\d+' in slot type 'budget' isn't valid. The slot value cant contain "*" and """

  • I tried doing again with your updated regex, unfortunately, the slot doesn't allow it.

  • Could try without the first forward slash, so \d+.\d+ but I also like sdtslmn answer

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions