Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Microsoft Teams #514

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
328641e
Update integrations_controller.rb
immewnity Dec 30, 2021
11ae3f9
Create teams_integrations_controller.rb
immewnity Dec 30, 2021
8981742
Create teams_notification.rb
immewnity Dec 30, 2021
853f0bc
Create teams_integration.rb
immewnity Dec 30, 2021
c2383f6
Update index.html.erb
immewnity Dec 30, 2021
657332d
Create _form.html.erb
immewnity Dec 30, 2021
c409c02
Create edit.html.erb
immewnity Dec 30, 2021
a903f41
Create index.html.erb
immewnity Dec 30, 2021
7fc93c9
Create new.html.erb
immewnity Dec 30, 2021
72f908b
Create show.html.erb
immewnity Dec 30, 2021
1f73bee
Update routes.rb
immewnity Dec 30, 2021
bca24f4
Create 20211230105330_create_teams_integrations.rb
immewnity Dec 30, 2021
7fad967
Update schema.rb
immewnity Dec 30, 2021
9d8c2a0
Update teams_integration.rb
immewnity Dec 30, 2021
b2ffd3c
Update page.rb
immewnity Dec 30, 2021
32c528f
Update _form.html.erb
immewnity Dec 30, 2021
eb2db2a
Update factories.rb
immewnity Dec 30, 2021
8eaf2f4
Update subscription_spec.rb
immewnity Dec 30, 2021
3c5de04
Create edit.html.erb_spec.rb
immewnity Dec 30, 2021
70a56ba
Create index.html.erb_spec.rb
immewnity Dec 30, 2021
b53d069
Create new.html.erb_spec.rb
immewnity Dec 30, 2021
8674b8b
Create show.html.erb_spec.rb
immewnity Dec 30, 2021
643c657
Create teams_integrations_routing_spec.rb
immewnity Dec 30, 2021
09afc60
Create teams_integrations_spec.rb
immewnity Dec 30, 2021
a1f0606
Create teams_integrations_spec.rb
immewnity Dec 30, 2021
99fba0a
Create teams_integrations_helper.rb
immewnity Dec 30, 2021
8eaae5e
Create teams_integrations_controller_spec.rb
immewnity Dec 30, 2021
72c5234
Create teams_integrations.scss
immewnity Dec 30, 2021
23b7e16
Create teams_integrations_helper.rb
immewnity Dec 30, 2021
b0ece12
Create teams_integrations.js
immewnity Dec 30, 2021
c7dce94
Update pages_controller.rb
immewnity Dec 31, 2021
f7f443e
Update _form.html.erb
immewnity Dec 31, 2021
c00c9eb
Update _form.html.erb
immewnity Dec 31, 2021
00670ba
Update pages_controller.rb
immewnity Dec 31, 2021
89d597e
Attempting to change to a MessageCard for Teams
immewnity Dec 31, 2021
91de27c
Update teams_integration.rb
immewnity Dec 31, 2021
c757be9
Update teams_integrations_spec.rb
immewnity Dec 31, 2021
525489b
Update teams_integration.rb
immewnity Dec 31, 2021
e72b080
Update teams_integration.rb
immewnity Dec 31, 2021
ec7916a
Update teams_integration.rb
immewnity Dec 31, 2021
4a6befd
Add Teams and SQS
immewnity Dec 31, 2021
05e09cd
Add Teams and SQS
immewnity Dec 31, 2021
cb8a067
Add Teams and SQS
immewnity Dec 31, 2021
3672b4f
Add Teams
immewnity Dec 31, 2021
75c9c66
Add Teams
immewnity Dec 31, 2021
d0c3c64
Added Teams and SQS
immewnity Dec 31, 2021
ddb450b
Added Teams and SQS
immewnity Dec 31, 2021
bf1c396
Update help.md
immewnity Dec 31, 2021
f89f78d
Update README.md
immewnity Dec 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Klaxon is a free, quick to set up and easy to use *robot* that checks websites r

You list websites you want monitored and Klaxon will visit them and, if they change, email you what's different. It saves you having to reload dozens of links yourself every day.

It's perfect for monitoring website changes you might miss, like freedom of information disclosure logs, court records, and anything related to Donald Trump. And it can even send notifications to your Slack and Discord channels.
It's perfect for monitoring website changes you might miss, like freedom of information disclosure logs, court records, and anything related to Donald Trump. And it can even send notifications to Slack, Discord, Microsoft Teams, and Amazon SQS.

Read more below, or say hello to the humans behind the project at the [Google Group email list](https://groups.google.com/forum/#!forum/news-klaxon-users).

Expand All @@ -18,7 +18,7 @@ The public release of this free and open source software was supported by Knight

## How Does Klaxon Work?

Klaxon enables users to "bookmark" portions of a webpage and be notified (via email, [Slack, or Discord](#notify-a-slack-or-discord-channel)) of any changes that may occur to those sections. [Learn more about bookmarklets on the help.md page](data/help.md).
Klaxon enables users to "bookmark" portions of a webpage and be notified (via email, [Slack, Discord](#notify-a-slack-or-discord-channel), Microsoft Teams, or Amazon SQS) of any changes that may occur to those sections. [Learn more about bookmarklets on the help.md page](data/help.md).

[![Circle CI](https://circleci.com/gh/themarshallproject/klaxon.svg?style=svg)](https://circleci.com/gh/themarshallproject/klaxon)

Expand Down Expand Up @@ -54,7 +54,7 @@ If you have a Heroku account and you’re ready to go, it’s time to click on t

You must be logged into your Heroku account, and it will take you to a page to configure your new app in Heroku’s dashboard. First, give your app a name in the first box. While this is technically optional, this will also double as the URL for your Klaxon instance, so think carefully about it for a moment. Try maybe an abbreviation for your newsroom with a hyphen and the word klaxon, like “wp-klaxon” or “sl-klaxon”. This will become a URL as https://sl-klaxon.herokuapp.com/

Scroll down to the “* Admin_emails” field, add a comma-separated list of email addresses for your newsroom’s Klaxon administrators. These administrators will be able to create accounts for any user in your organization, as well as configure various Klaxons and integrations with services like Slack and Discord.
Scroll down to the “* Admin_emails” field, add a comma-separated list of email addresses for your newsroom’s Klaxon administrators. These administrators will be able to create accounts for any user in your organization, as well as configure various Klaxons and integrations with services like Slack and Microsoft Teams.

Click the big purple “Deploy for Free” button. If you haven’t given Heroku your credit card yet, it will ask you for your information now. As long as you’re on the free settings, it won’t charge you, but Heroku wants to be prepared in case you change tiers. After that, give Heroku a few minutes for the app to build.

Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/teams_integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/teams_integrations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the teams_integrations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
1 change: 1 addition & 0 deletions app/controllers/integrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class IntegrationsController < ApplicationController
def index
@slack_integrations = SlackIntegration.all
@sqs_integrations = SqsIntegration.all
@teams_integrations = TeamsIntegration.all
end
end
2 changes: 2 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def new
@page = Page.new
@users = User.all
@slack_integrations = SlackIntegration.all
@teams_integrations = TeamsIntegration.all
end

# GET /pages/1/edit
def edit
@users = User.all
@slack_integrations = SlackIntegration.all
@teams_integrations = TeamsIntegration.all
end

# POST /pages
Expand Down
59 changes: 59 additions & 0 deletions app/controllers/teams_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class TeamsIntegrationsController < ApplicationController
before_action :set_teams_integration, only: [:show, :edit, :update, :destroy]
before_action :authorize

# GET /teams_integrations
def index
@teams_integrations = TeamsIntegration.all
end

# GET /teams_integrations/1
def show
end

# GET /teams_integrations/new
def new
@teams_integration = TeamsIntegration.new
end

# GET /teams_integrations/1/edit
def edit
end

# POST /teams_integrations
def create
@teams_integration = TeamsIntegration.new(teams_integration_params)

if @teams_integration.save
redirect_to integrations_path, notice: 'Teams integration was successfully created.'
else
render :new
end
end

# PATCH/PUT /teams_integrations/1
def update
if @teams_integration.update(teams_integration_params)
redirect_to integrations_path, notice: 'Teams integration was successfully updated.'
else
render :edit
end
end

# DELETE /teams_integrations/1
def destroy
@teams_integration.destroy
redirect_to integrations_path, notice: 'Teams integration was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_teams_integration
@teams_integration = TeamsIntegration.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def teams_integration_params
params.require(:teams_integration).permit(:channel, :webhook_url)
end
end
2 changes: 2 additions & 0 deletions app/helpers/teams_integrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TeamsIntegrationsHelper
end
12 changes: 12 additions & 0 deletions app/lib/teams_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TeamsNotification
def self.perform(url, payload)
json = payload.to_json
request = HTTParty.post(url, body: json, headers: {'Content-Type' => 'application/json'})
if request.code == 200
request.body
else
puts "Error sending webhook to url=#{url} for payload=#{payload.to_json}"
return false
end
end
end
2 changes: 2 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def update_subscriptions
User.find(id).subscribe(self)
elsif model == 'slack'
SlackIntegration.find(id).subscribe(self)
elsif model == 'teams'
TeamsIntegration.find(id).subscribe(self)
else
raise "unknown subscription type"
end
Expand Down
95 changes: 95 additions & 0 deletions app/models/teams_integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class TeamsIntegration < ApplicationRecord

validates :channel, length: { minimum: 2 }
validates :webhook_url, length: { minimum: 10 }
after_destroy :remove_subscriptions

include Rails.application.routes.url_helpers

def subscriptions
Subscription.where(watcher: self)
end

def watching
subscriptions.map(&:watching)
end

def subscribe(watchable)
# TODO: extract this. also in User
Subscription.where(watcher: self, watching: watchable).first_or_create do |subscription|
puts "#{self} subscribed to #{watchable}"
end
end

def is_subscribed_to?(watchable)
Subscription.where(watcher: self, watching: watchable).exists?
end

def send_notification(change)
puts "teams_integration#send_notification #{self.channel}"

change_date = change&.created_at&.strftime("%A, %B %d, %Y at %H:%M")
page_name = change&.after&.page&.name
source_url = change&.after&.page&.url
summary = "#{page_name} changed"
text = "#{page_name} changed #{page_change_url(change)}"

icon_url = URI.join(root_url, '/images/klaxon-logo-100px.png').to_s

payload = {
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"themeColor": "FF0B3A",
"summary": summary,
"sections": [
{
"activityTitle": "Klaxon",
"activitySubtitle": change_date,
"activityImage": icon_url,
"text": text
}
],
"potentialAction": [
{
"@type": "OpenUri",
"name": "Go to Source",
"targets": [
{
"os": "default",
"uri": source_url
}
]
},
{
"@type": "OpenUri",
"name": "Compare Snapshots",
"targets": [
{
"os": "default",
"uri": "#{page_change_url(change)}"
}
]
},
{
"@type": "OpenUri",
"name": "Snapshot HTML",
"targets": [
{
"os": "default",
"uri": "#{show_page_snapshot_html_url(change)}"
}
]
}
]
}

TeamsNotification.perform(self.webhook_url, payload)
return payload
end

def remove_subscriptions
subscriptions.each do |s|
s.destroy
end
end
end
29 changes: 28 additions & 1 deletion app/views/integrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack, Microsoft Teams, and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
</div>
<div class="col-md-3">
</div>
Expand Down Expand Up @@ -75,6 +75,33 @@
</tbody>
</table>
<% end %>
<div class="row">
<div class="col-md-9">
<h4 style="margin-top: 0;" class="klax-spacer-md">Teams</h4>
</div>
<div class="col-md-3">
<%= link_to "Create Teams Integration", new_teams_integration_path, class: 'btn btn-default btn-lg btn-block' %>
</div>
</div>
<% if @teams_integrations.present? %>
<table class="table">
<thead>
<tr>
<th>Channel</th>
<th></th>
</tr>
</thead>
<tbody>
<% @teams_integrations.each do |integration| %>
<tr>
<td><%= integration.channel %></td>
<td><%= link_to "Edit", edit_teams_integration_path(integration), class: 'btn btn-default btn-block pull-right', style: "max-width: 136px !important;" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
</div>

Expand Down
24 changes: 24 additions & 0 deletions app/views/pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<div class="col-md-3 klax-spacer-md">
<h5>Slack</h5>
<p>It looks like you haven't configured any Slack notifications yet, would you like to? <%= link_to "Go to settings.", integrations_path %>
</div>
<% else %>
<div class="col-md-3 klax-spacer-md">
<h5>Slack</h5>
Expand All @@ -80,6 +81,29 @@
<% end %>
</div>
<% end %>

<% if @teams_integrations.empty? %>

<div class="col-md-3 klax-spacer-md">
<h5>Teams</h5>
<p>It looks like you haven't configured any Teams notifications yet, would you like to? <%= link_to "Go to settings.", integrations_path %>
</div>
<% else %>
<div class="col-md-3 klax-spacer-md">
<h5>Teams</h5>
<p>Which of the Teams channels that you’ve added should get notification when this item changes?</p>

<% @teams_integrations.each do |teams| %>
<div class="checkbox">
<label>
<%= check_box_tag "page[subscriptions][]", "teams:#{teams.id}", teams.is_subscribed_to?(@page) %>
<%= teams.channel %>
</label>
</div>
<% end %>
</div>
<% end %>

<div class="col-md-3 klax-spacer-md disabled">
<!-- <h5>Twitter</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tincidunt semper orci, eget molestie orci ullamcorper eu. Curabitur viverra ligula a dolor scelerisque, at tincidunt.</p> -->
Expand Down
2 changes: 1 addition & 1 deletion app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="col-md-3">
</div>
<div class="col-md-6">
<p class="lead">Klaxon enables reporters and editors to monitor scores of sites and files on the web for newsworthy changes. Get email and Slack notifications when something changes.</p>
<p class="lead">Klaxon enables reporters and editors to monitor scores of sites and files on the web for newsworthy changes. Get email, Slack, Microsoft Teams, and/or Amazon SQS notifications when something changes.</p>
</div>
<div class="col-md-3">
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/slack_integrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack, Microsoft Teams, and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
</div>
<div class="col-md-3">
<!-- negative space -->
Expand All @@ -19,4 +19,4 @@
</div><!-- /.container -->
</div>

<%= render 'form' %>
<%= render 'form' %>
4 changes: 2 additions & 2 deletions app/views/slack_integrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack, Microsoft Teams, and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
</div>
<div class="col-md-6">
<!-- negative space -->
</div>
</div>
</div><!-- /.container -->
</div>
<%= render 'form' %>
<%= render 'form' %>
2 changes: 1 addition & 1 deletion app/views/sqs_integrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack, Microsoft Teams, and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
</div>
<div class="col-md-3">
<!-- negative space -->
Expand Down
2 changes: 1 addition & 1 deletion app/views/sqs_integrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
<p class="lead">Integrations allow you to push alerts to various platforms. For now, Klaxon supports Slack, Microsoft Teams, and Amazon SQS. We hope to add more in the future. If you’re interested in helping, <a href="https://github.com/themarshallproject/klaxon">visit our repo.</a></p>
</div>
<div class="col-md-6">
<!-- negative space -->
Expand Down
2 changes: 1 addition & 1 deletion app/views/static/help.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ img {
</div>
<div class="row">
<div class="col-md-6">
<p class="lead">Klaxon enables reporters and editors to monitor scores of sites and files on the web for newsworthy changes. Get email and Slack notifications when something changes. It’s an open source project being built by <svg width="13px" height="13px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 223.2 223.2" enable-background="new 0 0 223.2 223.2" xml:space="preserve"><g><g><polygon fill="#ff0b3a" points="3.6,3.6 3.6,219.6 34.5,219.6 34.5,34.5 "></polygon><polygon fill="#ff0b3a" points="65.3,65.3 65.3,219.6 96.2,219.6 96.2,96.2 "></polygon><polygon fill="#ff0b3a" points="188.7,34.5 188.7,219.6 219.6,219.6 219.6,3.6 "></polygon><polygon fill="#ff0b3a" points="127,96.2 127,219.6 157.9,219.6 157.9,65.3 "></polygon></g></g></svg> <a href="https://themarshallproject.org">The Marshall Project.</a></p>
<p class="lead">Klaxon enables reporters and editors to monitor scores of sites and files on the web for newsworthy changes. Get email, Slack, Microsoft Teams, and/or Amazon SQS notifications when something changes. It’s an open source project being built by <svg width="13px" height="13px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 223.2 223.2" enable-background="new 0 0 223.2 223.2" xml:space="preserve"><g><g><polygon fill="#ff0b3a" points="3.6,3.6 3.6,219.6 34.5,219.6 34.5,34.5 "></polygon><polygon fill="#ff0b3a" points="65.3,65.3 65.3,219.6 96.2,219.6 96.2,96.2 "></polygon><polygon fill="#ff0b3a" points="188.7,34.5 188.7,219.6 219.6,219.6 219.6,3.6 "></polygon><polygon fill="#ff0b3a" points="127,96.2 127,219.6 157.9,219.6 157.9,65.3 "></polygon></g></g></svg> <a href="https://themarshallproject.org">The Marshall Project.</a></p>
</div>
<div class="col-md-3">

Expand Down
Loading