From a2cfdc838fb9217518f5ca059baa3356035f81d4 Mon Sep 17 00:00:00 2001 From: Joshua Arts Date: Tue, 9 May 2017 10:26:01 -0400 Subject: [PATCH] Slack Integration (#170) * slack integration * recomment server variables * recomment server variables --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/controllers/bbb_controller.rb | 2 +- app/jobs/end_meeting_job.rb | 3 +++ app/jobs/join_meeting_job.rb | 9 ++++++-- app/jobs/recording_updates_job.rb | 5 +++++ config/initializers/slack.rb | 36 +++++++++++++++++++++++++++++++ config/locales/en-us.yml | 6 ++++++ env | 11 +++++++++- 9 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 config/initializers/slack.rb diff --git a/Gemfile b/Gemfile index ef04d15c..ec1aff77 100644 --- a/Gemfile +++ b/Gemfile @@ -76,3 +76,5 @@ gem 'jquery-datatables-rails', '~> 3.4.0' gem 'rails-timeago', '~> 2.0' gem 'http_accept_language' + +gem 'slack-notifier' diff --git a/Gemfile.lock b/Gemfile.lock index 9b37b4ff..4b84566c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -186,6 +186,7 @@ GEM json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) + slack-notifier (2.1.0) spring (2.0.0) activesupport (>= 4.2) spring-watcher-listen (2.0.1) @@ -247,6 +248,7 @@ DEPENDENCIES rails-timeago (~> 2.0) sass-rails (~> 5.0) simplecov + slack-notifier spring spring-watcher-listen (~> 2.0.0) sqlite3 diff --git a/app/controllers/bbb_controller.rb b/app/controllers/bbb_controller.rb index b99d5ef7..6b47975b 100644 --- a/app/controllers/bbb_controller.rb +++ b/app/controllers/bbb_controller.rb @@ -91,7 +91,7 @@ class BbbController < ApplicationController # the user can join the meeting if user if bbb_res[:returncode] && current_user == user - JoinMeetingJob.perform_later(user.encrypted_id, params[:id]) + JoinMeetingJob.perform_later(user, params[:id], base_url) # user will be waiting for a moderator else diff --git a/app/jobs/end_meeting_job.rb b/app/jobs/end_meeting_job.rb index 6cf7c251..baf87f0a 100644 --- a/app/jobs/end_meeting_job.rb +++ b/app/jobs/end_meeting_job.rb @@ -18,6 +18,9 @@ class EndMeetingJob < ApplicationJob queue_as :default def perform(room, meeting) + + Rails.configuration.slack_notifier.ping I18n.t('slack.meeting_end', meeting: meeting) if !Rails.configuration.slack_notifier.nil? + ActionCable.server.broadcast "#{room}-#{meeting}_meeting_updates_channel", action: 'meeting_ended' end diff --git a/app/jobs/join_meeting_job.rb b/app/jobs/join_meeting_job.rb index 02dc2db4..51539936 100644 --- a/app/jobs/join_meeting_job.rb +++ b/app/jobs/join_meeting_job.rb @@ -17,8 +17,13 @@ class JoinMeetingJob < ApplicationJob queue_as :default - def perform(room, meeting) - ActionCable.server.broadcast "#{room}-#{meeting}_meeting_updates_channel", +def perform(user, meeting, base_url) + + join_message = I18n.t('slack.meeting_join', user: user.name, meeting: meeting) + "(#{base_url})" + formatted = Slack::Notifier::Util::LinkFormatter.format(join_message) + Rails.configuration.slack_notifier.ping formatted if !Rails.configuration.slack_notifier.nil? + + ActionCable.server.broadcast "#{user.encrypted_id}-#{meeting}_meeting_updates_channel", action: 'moderator_joined', moderator: 'joined' end diff --git a/app/jobs/recording_updates_job.rb b/app/jobs/recording_updates_job.rb index e059a2bb..cc913e08 100644 --- a/app/jobs/recording_updates_job.rb +++ b/app/jobs/recording_updates_job.rb @@ -22,6 +22,11 @@ class RecordingUpdatesJob < ApplicationJob def perform(room, record_id) recording = bbb_get_recordings({recordID: record_id})[:recordings].first full_id = "#{room}-#{recording[:metadata][:"meeting-name"]}" + + change = (recording[:metadata][:"gl-listed"] == "true") ? I18n.t('slack.published') : I18n.t('slack.unpublished') + slack_message = I18n.t('slack.recording_visibility', meeting: recording[:metadata][:"meeting-name"], change: change) + Rails.configuration.slack_notifier.ping slack_message if !Rails.configuration.slack_notifier.nil? + ActionCable.server.broadcast "#{room}_recording_updates_channel", action: 'update', id: record_id, diff --git a/config/initializers/slack.rb b/config/initializers/slack.rb new file mode 100644 index 00000000..886933f6 --- /dev/null +++ b/config/initializers/slack.rb @@ -0,0 +1,36 @@ +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2016 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +def uri?(string) + uri = URI.parse(string) + %w( http https ).include?(uri.scheme) +rescue URI::BadURIError + false +rescue URI::InvalidURIError + false +end + +if !ENV['SLACK_WEBHOOK'].nil? && !ENV['SLACK_WEBHOOK'].empty? && uri?(ENV['SLACK_WEBHOOK']) then + # Initialize the slack notifier. + Rails.application.config.slack_notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK'] do + defaults channel: ENV['SLACK_CHANNEL'], + username: "BigBlueButton", + icon_url: 'https://avatars3.githubusercontent.com/u/230228?v=3&s=200' + end +else + # Initialize it to nil (slack not configured) + Rails.application.config.slack_notifier = nil +end diff --git a/config/locales/en-us.yml b/config/locales/en-us.yml index 53212fbe..9ddfb41a 100644 --- a/config/locales/en-us.yml +++ b/config/locales/en-us.yml @@ -134,6 +134,12 @@ en-US: return_to_room: Return to main page session_url_explanation: The meeting will be taking place using the following URL signin_text: Log in with %{provider} + slack: + meeting_end: "%{meeting} has ended." + meeting_join: "%{user} joined %{meeting}.\n[Click here join!]" + recording_visibility: "A recording of %{meeting} was %{change}" + published: published + unpublished: unpublished start: Start start_join: Start start_meeting: Start meeting diff --git a/env b/env index a0f2a8a6..83b14a96 100644 --- a/env +++ b/env @@ -6,7 +6,6 @@ # docker run --rm bigbluebutton/greenlight rake secret # SECRET_KEY_BASE= - # Step 2 - Enter credentials for your BigBlueButton Server # # The endpoint and secret from your bigbluebutton server. To get these values, run @@ -47,6 +46,16 @@ GOOGLE_OAUTH2_SECRET= # the application when recordings are done). GREENLIGHT_USE_WEBHOOKS=false +# Slack Integration (optional) +# +# You will need to register an incoming-webhook for your slack channel +# in order for GreenLight to post to it. You can do this by going +# to https://slack.com/apps/A0F7XDUAZ-incoming-webhooks, selecting your +# team and then selecting "Add Incoming WebHooks integration" on the +# desired channel. You will then need to paste the webhook below. +# +SLACK_WEBHOOK= +SLACK_CHANNEL= # SMTP Mailer #