Skip to content

Commit 1bf0165

Browse files
committed
remove news api gem and use faraday to resolve clash with news plugin
1 parent ed6e90d commit 1bf0165

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/discourse_chatbot/functions/news_function.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require_relative '../function'
4-
require 'news-api'
54

65
module DiscourseChatbot
76
class NewsFunction < Function
@@ -40,14 +39,27 @@ def process(args)
4039
EOS
4140
super(args)
4241

43-
newsapi = News.new(SiteSetting.chatbot_news_api_token)
44-
all_articles = newsapi.get_everything(q: args[parameters[0][:name]],
45-
from: args[parameters[1][:name]], #'2023-08-01'
46-
language: 'en',
47-
sortBy: 'relevancy')
42+
conn_params = {}
43+
44+
conn_params = args[parameters[1][:name]].blank? ?
45+
{ q: "#{args[parameters[0][:name]]}", language: 'en', sortBy: 'relevancy' } :
46+
{ q: "#{args[parameters[0][:name]]}", language: 'en', sortBy: 'relevancy', start_date: "#{args[parameters[1][:name]]}" }
47+
48+
conn = Faraday.new(
49+
url: 'https://newsapi.org',
50+
params: conn_params,
51+
headers: { 'X-Api-Key' => "#{SiteSetting.chatbot_news_api_token}" }
52+
)
53+
54+
response = conn.get('/v2/everything')
55+
56+
response_body = JSON.parse(response.body)
57+
58+
all_articles = response_body["articles"]
59+
4860
news = "The latest news about this is: "
4961
all_articles.each do |a|
50-
news += "#{a.title}. "
62+
news += "#{a["title"]}. "
5163
end
5264
news
5365
rescue

plugin.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
# name: discourse-chatbot
33
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
4-
# version: 0.27
4+
# version: 0.28
55
# authors: merefield
66
# url: https://github.com/merefield/discourse-chatbot
77

@@ -12,8 +12,6 @@
1212
gem "google_search_results", '2.2.0'
1313
# wikipedia
1414
gem "wikipedia-client", '1.17.0'
15-
# news api
16-
gem 'news-api', '0.2.0'
1715
# safe ruby for calculations and date functions
1816
gem "childprocess", "4.1.0"
1917
gem "safe_ruby", "1.0.4"

0 commit comments

Comments
 (0)