FEATURE: Google Chat integration (#57)
This commit is contained in:
parent
00ec1f486a
commit
13fea5b803
|
@ -227,3 +227,12 @@ en:
|
||||||
help: "The URL provided when you create a new incomming webhook"
|
help: "The URL provided when you create a new incomming webhook"
|
||||||
errors:
|
errors:
|
||||||
invalid_channel: "That channel does not exist on Webex"
|
invalid_channel: "That channel does not exist on Webex"
|
||||||
|
google:
|
||||||
|
title: "Google Chat"
|
||||||
|
param:
|
||||||
|
name:
|
||||||
|
title: "Name"
|
||||||
|
help: "A name for the channel (only shown in the Discourse admin interface)"
|
||||||
|
webhook_url:
|
||||||
|
title: "Webhook URL"
|
||||||
|
help: "The URL provided when you create a new webhook"
|
||||||
|
|
|
@ -98,6 +98,12 @@ en:
|
||||||
chat_integration_webex_enabled: "Enable the Webex Teams chat integration provider"
|
chat_integration_webex_enabled: "Enable the Webex Teams chat integration provider"
|
||||||
chat_integration_webex_excerpt_length: "Webex Team post excerpt length"
|
chat_integration_webex_excerpt_length: "Webex Team post excerpt length"
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
######## GOOGLE SETTINGS #########
|
||||||
|
###########################################
|
||||||
|
chat_integration_google_enabled: "Enable the 'Google Chat' chat integration provider"
|
||||||
|
chat_integration_google_excerpt_length: "Google Chat post excerpt length"
|
||||||
|
|
||||||
chat_integration:
|
chat_integration:
|
||||||
|
|
||||||
all_categories: "(all categories)"
|
all_categories: "(all categories)"
|
||||||
|
@ -280,3 +286,12 @@ en:
|
||||||
#######################################
|
#######################################
|
||||||
groupme:
|
groupme:
|
||||||
message_title: "posted"
|
message_title: "posted"
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
############ GOOGLE STRINGS ###########
|
||||||
|
#######################################
|
||||||
|
google:
|
||||||
|
new_post: "New post on %{site_title}"
|
||||||
|
new_topic: "New topic on %{site_title}"
|
||||||
|
author: "by @%{username}"
|
||||||
|
link: "View on %{site_title}"
|
||||||
|
|
|
@ -144,3 +144,10 @@ chat_integration:
|
||||||
default: false
|
default: false
|
||||||
chat_integration_webex_excerpt_length:
|
chat_integration_webex_excerpt_length:
|
||||||
default: 400
|
default: 400
|
||||||
|
###########################################
|
||||||
|
########## GOOGLE CHAT SETTINGS ###########
|
||||||
|
###########################################
|
||||||
|
chat_integration_google_enabled:
|
||||||
|
default: false
|
||||||
|
chat_integration_google_excerpt_length:
|
||||||
|
default: 100
|
||||||
|
|
|
@ -103,3 +103,4 @@ require_relative "provider/flowdock/flowdock_provider"
|
||||||
require_relative "provider/groupme/groupme_provider"
|
require_relative "provider/groupme/groupme_provider"
|
||||||
require_relative "provider/teams/teams_provider"
|
require_relative "provider/teams/teams_provider"
|
||||||
require_relative "provider/webex/webex_provider"
|
require_relative "provider/webex/webex_provider"
|
||||||
|
require_relative "provider/google/google_provider"
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module DiscourseChat
|
||||||
|
module Provider
|
||||||
|
module GoogleProvider
|
||||||
|
PROVIDER_NAME = 'google'.freeze
|
||||||
|
PROVIDER_ENABLED_SETTING = :chat_integration_google_enabled
|
||||||
|
CHANNEL_PARAMETERS = [
|
||||||
|
{ key: "name", regex: '^\S+$', unique: true },
|
||||||
|
{ key: "webhook_url", regex: '^https:\/\/chat.googleapis.com\/v1\/\S+$', unique: true, hidden: true }
|
||||||
|
]
|
||||||
|
|
||||||
|
def self.trigger_notification(post, channel, rule)
|
||||||
|
message = get_message(post)
|
||||||
|
uri = URI(channel.data['webhook_url'])
|
||||||
|
|
||||||
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
http.use_ssl = (uri.scheme == 'https')
|
||||||
|
|
||||||
|
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
|
||||||
|
req.body = message.to_json
|
||||||
|
response = http.request(req)
|
||||||
|
|
||||||
|
unless response.kind_of? Net::HTTPSuccess
|
||||||
|
raise ::DiscourseChat::ProviderError.new info: { request: req.body, response_code: response.code, response_body: response.body }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get_message(post)
|
||||||
|
{
|
||||||
|
cards: [
|
||||||
|
{
|
||||||
|
sections: [
|
||||||
|
{
|
||||||
|
widgets: [
|
||||||
|
{
|
||||||
|
keyValue: {
|
||||||
|
"topLabel": I18n.t("chat_integration.provider.google.new_#{post.is_first_post? ? "topic" : "post"}", site_title: SiteSetting.title),
|
||||||
|
"content": post.topic.title,
|
||||||
|
"contentMultiline": "false",
|
||||||
|
"bottomLabel": I18n.t("chat_integration.provider.google.author", username: post.user.username),
|
||||||
|
"onClick": {
|
||||||
|
"openLink": {
|
||||||
|
"url": post.full_url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
widgets: [
|
||||||
|
{
|
||||||
|
textParagraph: {
|
||||||
|
text: post.excerpt(SiteSetting.chat_integration_google_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
widgets: [
|
||||||
|
{
|
||||||
|
buttons: [
|
||||||
|
{
|
||||||
|
"textButton": {
|
||||||
|
"text": I18n.t("chat_integration.provider.google.link", site_title: SiteSetting.title),
|
||||||
|
"onClick": {
|
||||||
|
"openLink": {
|
||||||
|
"url": post.full_url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,30 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe DiscourseChat::Provider::GoogleProvider do
|
||||||
|
let(:post) { Fabricate(:post) }
|
||||||
|
|
||||||
|
describe '.trigger_notifications' do
|
||||||
|
before do
|
||||||
|
SiteSetting.chat_integration_google_enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:chan1) { DiscourseChat::Channel.create!(provider: 'google', data: { name: 'discourse', webhook_url: 'https://chat.googleapis.com/v1/abcdefg' }) }
|
||||||
|
|
||||||
|
it 'sends a webhook request' do
|
||||||
|
stub1 = stub_request(:post, chan1.data['webhook_url']).to_return(body: "1")
|
||||||
|
described_class.trigger_notification(post, chan1, nil)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles errors correctly' do
|
||||||
|
stub1 = stub_request(:post, chan1.data['webhook_url']).to_return(status: 400, body: "{}")
|
||||||
|
expect(stub1).to have_been_requested.times(0)
|
||||||
|
expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(::DiscourseChat::ProviderError)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue