From 5b312d7dca1d21a5be592e5633f6d854ebca86d9 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 30 Aug 2024 20:47:23 +0100 Subject: [PATCH] FIX: web search with serp API --- lib/discourse_chatbot/functions/web_search_function.rb | 9 +++++---- plugin.rb | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/discourse_chatbot/functions/web_search_function.rb b/lib/discourse_chatbot/functions/web_search_function.rb index 2c638aa..fd5c690 100644 --- a/lib/discourse_chatbot/functions/web_search_function.rb +++ b/lib/discourse_chatbot/functions/web_search_function.rb @@ -41,12 +41,13 @@ def process(args) hash_results = ::GoogleSearch.new(q: args[parameters[0][:name]], serp_api_key: SiteSetting.chatbot_serp_api_key) .get_hash - hash_results.dig(:answer_box, :answer) || - hash_results.dig(:answer_box, :snippet) || - result = hash_results.dig(:organic_results, 0, :snippet) + result = hash_results.dig(:answer_box, :answer).presence || + hash_results.dig(:answer_box, :snippet).presence || + hash_results.dig(:organic_results) end result[0..SiteSetting.chatbot_function_response_char_limit] - rescue + rescue => e + Rails.logger.error("Chatbot: Error in web_search function: #{e}") I18n.t("chatbot.prompt.function.web_search.error", query: args[parameters[0][:name]]) end end diff --git a/plugin.rb b/plugin.rb index d14e69e..094d9b6 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-chatbot # about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages -# version: 1.0.1 +# version: 1.0.2 # authors: merefield # url: https://github.com/merefield/discourse-chatbot