diff --git a/app/lib/smooch_nlu.rb b/app/lib/smooch_nlu.rb index 619551c879..8ac5878de2 100644 --- a/app/lib/smooch_nlu.rb +++ b/app/lib/smooch_nlu.rb @@ -9,7 +9,10 @@ class SmoochBotNotInstalledError < ::ArgumentError Bot::Alegre::MEAN_TOKENS_MODEL => 0.6 } - ALEGRE_CONTEXT_KEY = 'smooch_nlu_menu' + ALEGRE_CONTEXT_KEY = { + menu: 'smooch_nlu_menu', + resource: 'smooch_nlu_resource' + } def initialize(team_slug) @team_slug = team_slug @@ -26,18 +29,18 @@ def disable! end def enabled? - !!@smooch_bot_installation.get_nlu_menus_enabled + !!@smooch_bot_installation.get_nlu_enabled end - def add_keyword(language, menu, menu_option_index, keyword) - update_keywords(language, menu, menu_option_index, keyword, 'add') + def add_keyword_to_menu_option(language, menu, menu_option_index, keyword) + update_menu_option_keywords(language, menu, menu_option_index, keyword, 'add') end - def remove_keyword(language, menu, menu_option_index, keyword) - update_keywords(language, menu, menu_option_index, keyword, 'remove') + def remove_keyword_from_menu_option(language, menu, menu_option_index, keyword) + update_menu_option_keywords(language, menu, menu_option_index, keyword, 'remove') end - def list_keywords(languages = nil, menus = nil) + def list_menu_keywords(languages = nil, menus = nil) if languages.nil? languages = @smooch_bot_installation.get_smooch_workflows.map { |w| w['smooch_workflow_language'] } elsif languages.is_a? String @@ -76,7 +79,7 @@ def self.menu_option_from_message(message, language, options) team_slug = Team.find(Bot::Smooch.config['team_id']).slug params = nil response = nil - if Bot::Smooch.config.to_h['nlu_menus_enabled'] && !options.nil? + if Bot::Smooch.config.to_h['nlu_enabled'] && !options.nil? # FIXME: In the future we could consider menus across all languages when options is nil # FIXME: No need to call Alegre if it's an exact match to one of the keywords # FIXME: No need to call Alegre if message has no word characters @@ -86,7 +89,7 @@ def self.menu_option_from_message(message, language, options) models: ALEGRE_MODELS_AND_THRESHOLDS.keys, per_model_threshold: ALEGRE_MODELS_AND_THRESHOLDS, context: { - context: ALEGRE_CONTEXT_KEY, + context: ALEGRE_CONTEXT_KEY[:menu], team: team_slug, language: language, } @@ -130,32 +133,15 @@ def self.menu_option_from_message(message, language, options) private def toggle!(enabled) - @smooch_bot_installation.set_nlu_menus_enabled = enabled + @smooch_bot_installation.set_nlu_enabled = enabled @smooch_bot_installation.save! @smooch_bot_installation.reload end - def alegre_doc_id(menu, menu_option_id, keyword) - Digest::MD5.hexdigest([ALEGRE_CONTEXT_KEY, @team_slug, menu, menu_option_id, keyword].join(':')) - end - - def common_params_for_alegre(menu, language, menu_option_id, keyword) - { - doc_id: alegre_doc_id(menu, menu_option_id, keyword), - context: { - context: ALEGRE_CONTEXT_KEY, - team: @team_slug, - language: language, - menu: menu, - menu_option_id: menu_option_id - } - } - end - # "menu" is "main" or "secondary" # "operation" is "add" or "remove" # FIXME: Validate the two things above - def update_keywords(language, menu, menu_option_index, keyword, operation) + def update_menu_option_keywords(language, menu, menu_option_index, keyword, operation) alegre_operation = nil alegre_params = nil workflow = @smooch_bot_installation.get_smooch_workflows.find { |w| w['smooch_workflow_language'] == language } @@ -163,14 +149,25 @@ def update_keywords(language, menu, menu_option_index, keyword, operation) # Make sure there is a unique identifier for this menu option # FIXME: This whole thing should be a model :( menu_option_id = (workflow["smooch_state_#{menu}"]['smooch_menu_options'][menu_option_index]['smooch_menu_option_id'] ||= SecureRandom.uuid) + doc_id = Digest::MD5.hexdigest([ALEGRE_CONTEXT_KEY[:menu], @team_slug, menu, menu_option_id, keyword].join(':')) + common_alegre_params = { + doc_id: doc_id, + context: { + context: ALEGRE_CONTEXT_KEY[:menu], + team: @team_slug, + language: language, + menu: menu, + menu_option_id: menu_option_id + } + } if operation == 'add' && !keywords.include?(keyword) keywords << keyword alegre_operation = 'post' - alegre_params = common_params_for_alegre(menu, language, menu_option_id, keyword).merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys }) + alegre_params = common_alegre_params.merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys }) elsif operation == 'remove' keywords -= [keyword] alegre_operation = 'delete' - alegre_params = common_params_for_alegre(menu, language, menu_option_id, keyword).merge({ quiet: true }) + alegre_params = common_alegre_params.merge({ quiet: true }) end workflow["smooch_state_#{menu}"]['smooch_menu_options'][menu_option_index]['smooch_menu_option_nlu_keywords'] = keywords @smooch_bot_installation.save! diff --git a/test/lib/smooch_nlu_test.rb b/test/lib/smooch_nlu_test.rb index 81072c143e..3c1a1eac48 100644 --- a/test/lib/smooch_nlu_test.rb +++ b/test/lib/smooch_nlu_test.rb @@ -65,7 +65,7 @@ def create_team_with_smooch_bot_installed nlu = SmoochNlu.new(team.slug) nlu.enable! Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once - nlu.add_keyword('en', 'main', 0, 'subscribe') + nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe') expected_output = { 'en' => { 'main' => [ @@ -74,35 +74,35 @@ def create_team_with_smooch_bot_installed } } # Since the demo team has only one language and menu all of the following are nearly the same - assert_equal nlu.list_keywords('en', 'main'), expected_output - assert_equal nlu.list_keywords('en', ['main']), expected_output + assert_equal nlu.list_menu_keywords('en', 'main'), expected_output + assert_equal nlu.list_menu_keywords('en', ['main']), expected_output # These calls should include an empty secondary menu expected_output['en']['secondary'] = [] - assert_equal nlu.list_keywords(), expected_output - assert_equal nlu.list_keywords('en'), expected_output - assert_equal nlu.list_keywords(['en']), expected_output + assert_equal nlu.list_menu_keywords(), expected_output + assert_equal nlu.list_menu_keywords('en'), expected_output + assert_equal nlu.list_menu_keywords(['en']), expected_output end test 'should add keyword if it does not exist' do Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once team = create_team_with_smooch_bot_installed - SmoochNlu.new(team.slug).add_keyword('en', 'main', 0, 'subscribe to the newsletter') + SmoochNlu.new(team.slug).add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter') end test 'should not add keyword if it exists' do team = create_team_with_smooch_bot_installed nlu = SmoochNlu.new(team.slug) Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once - nlu.add_keyword('en', 'main', 0, 'subscribe to the newsletter') + nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter') Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.never - nlu.add_keyword('en', 'main', 0, 'subscribe to the newsletter') + nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter') end test 'should delete keyword' do Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'delete' && y == '/text/similarity/' }.once team = create_team_with_smooch_bot_installed - SmoochNlu.new(team.slug).remove_keyword('en', 'main', 0, 'subscribe to the newsletter') + SmoochNlu.new(team.slug).remove_keyword_from_menu_option('en', 'main', 0, 'subscribe to the newsletter') end test 'should not return a menu option if NLU is not enabled' do diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index 9a4a1e93f0..9a73961b9f 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -760,8 +760,8 @@ def send_message_outside_24_hours_window(template, pm = nil) # Enable NLU and add a couple of keywords for the newsletter menu option nlu = SmoochNlu.new(@team.slug) nlu.enable! - nlu.add_keyword('en', 'main', 2, 'I want to subscribe to the newsletter') - nlu.add_keyword('en', 'main', 2, 'I want to unsubscribe from the newsletter') + nlu.add_keyword_to_menu_option('en', 'main', 2, 'I want to subscribe to the newsletter') + nlu.add_keyword_to_menu_option('en', 'main', 2, 'I want to unsubscribe from the newsletter') reload_tipline_settings query_option_id = @installation.get_smooch_workflows[0]['smooch_state_main']['smooch_menu_options'][1]['smooch_menu_option_id'] subscription_option_id = @installation.get_smooch_workflows[0]['smooch_state_main']['smooch_menu_options'][2]['smooch_menu_option_id'] @@ -788,8 +788,8 @@ def send_message_outside_24_hours_window(template, pm = nil) # Delete two keywords, so expect two calls to Alegre Bot::Alegre.expects(:request_api).with{ |x, y, _z| x == 'delete' && y == '/text/similarity/' }.twice - nlu.remove_keyword('en', 'main', 2, 'I want to subscribe to the newsletter') - nlu.remove_keyword('en', 'main', 2, 'I want to unsubscribe from the newsletter') + nlu.remove_keyword_from_menu_option('en', 'main', 2, 'I want to subscribe to the newsletter') + nlu.remove_keyword_from_menu_option('en', 'main', 2, 'I want to unsubscribe from the newsletter') end test "should get multimedia resource on tipline bot v2" do