Skip to content

Commit

Permalink
Add Calendar notify
Browse files Browse the repository at this point in the history
  • Loading branch information
jhang-jhe-wei committed Aug 3, 2021
1 parent 570c4e8 commit b7c96c6
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 8 deletions.
1 change: 1 addition & 0 deletions Capfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "capistrano/deploy"
require 'capistrano/rails'
require 'capistrano/passenger'
require "capistrano/rvm"
require "whenever/capistrano"
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ gem 'devise'
gem 'omniauth-line', git: 'https://github.com/etrex/omniauth-line.git'

gem 'lotify'
gem 'whenever', require: false
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (3.0.0)
chronic (0.10.2)
concurrent-ruby (1.1.9)
crass (1.0.6)
devise (4.8.0)
Expand Down Expand Up @@ -302,6 +303,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.4.2)
Expand Down Expand Up @@ -338,6 +341,7 @@ DEPENDENCIES
web-console (>= 4.1.0)
webdrivers
webpacker (~> 5.0)
whenever

RUBY VERSION
ruby 2.7.2p137
Expand Down
34 changes: 29 additions & 5 deletions app/controllers/notifies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class NotifiesController < ApplicationController
http_basic_authenticate_with :name => ENV["HTTP_AUTH_USERNAME"], :password => ENV["HTTP_AUTH_PASSWORD"], only: [:version, :version_notify]
http_basic_authenticate_with :name => ENV["HTTP_AUTH_USERNAME"], :password => ENV["HTTP_AUTH_PASSWORD"], only: [:version, :version_notify, :calendar, :calendar_notify]

def index
@auth_link = LineNotify.get_auth_link(current_user.line_id)
end
Expand All @@ -14,9 +15,18 @@ def version_notify
data = release_data
subscriptions = Subscription.where("notify_type = ?", "版本更新通知")
subscriptions.each do |subscription|
LineNotify.send(subscription.user.line_notify_token, message: "\n\n版本更新通知\n\n版本號#{data[0]["tag_name"]}\n\n#{data[0]["body"]}")
LineNotify.send(subscription.user.line_notify_token, message: "\n\n版本更新通知\n\n💎 最新版本號#{data[0]['tag_name']}\n\n本次更新內容為以下:\n\n#{data[0]['body'].gsub!('#','📋').gsub!('-','📌')}")
end
redirect_to version_notify_path, notice: '訊息已傳送!'
redirect_to version_notify_path, notice: "訊息已傳送!"
end

def calendar
@event_list = calendar_events_list["items"]
end

def calendar_notify
CalendarEventsNotify.new.perform
redirect_to calendar_notify_path, notice: "訊息已傳送!"
end

def callback
Expand All @@ -31,8 +41,22 @@ def callback
end

private

def release_data
response = Faraday.get('https://api.github.com/repos/jhang-jhe-wei/NTUST-Senior/releases')
JSON.parse response.body
response = Faraday.get("https://api.github.com/repos/jhang-jhe-wei/NTUST-Senior/releases")
JSON.parse response.body
end

def calendar_events_list(duration = 7.days)
calendar_id = "b10730224@gapps.ntust.edu.tw"
url = "https://www.googleapis.com/calendar/v3/calendars/#{calendar_id}/events"
response = Faraday.get(url, {
key: ENV["GOOGLE_API_KEY"],
singleEvents: true,
order_by: "startTime",
time_min: DateTime.now,
time_max: DateTime.now + duration,
})
JSON.parse response.body
end
end
9 changes: 7 additions & 2 deletions app/controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ def subscribe
subscription = Subscription.new(subscription_params)
if subscription.save
current_user.subscriptions << subscription
LineNotify.send(current_user.line_notify_token, message: "#{subscription.notify_type} 訂閱成功!")
case subscription.notify_type
when "台科大行事曆"
LineNotify.send(current_user.line_notify_token, message: "#{subscription.notify_type} 訂閱成功!\n如果你比較習慣使用 Google Calendar 也可以點擊此連結 https://calendar.google.com/calendar/u/1?cid=YjEwNzMwMjI0QGdhcHBzLm50dXN0LmVkdS50dw 加入到你的 Google Calendar 。")
else
LineNotify.send(current_user.line_notify_token, message: "#{subscription.notify_type} 訂閱成功!")
end
render json: { type: "text", text: "恭喜你完成 #{subscription.notify_type} 的訂閱!" }
else
render json: { type: "text", text: subscription.errors.to_s }
Expand All @@ -35,7 +40,7 @@ def has_token?
end

def notify_present?
notifies = ["版本更新通知", "學校行事曆通知"]
notifies = ["版本更新通知", "台科大行事曆"]
render json: { type: "text", text: "沒有該通知,可能已被刪除或不存在!" } if notifies.exclude? params[:notify_type]
end

Expand Down
34 changes: 34 additions & 0 deletions app/services/calendar_events_notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class CalendarEventsNotify
def initialize
@message = ""
calendar_events_list['items'].try(:each) do |event|
@message += "🗓 #{event.dig('summary')} (#{event.dig('start', 'date')})\n\n"
end
end

def perform
subscriptions = Subscription.where("notify_type = ?", "台科大行事曆")
subscriptions.each do |subscription|
if @message
LineNotify.send(subscription.user.line_notify_token, message: "\n\n台科大行事曆\n\n近七日台科大活動如下:\n\n#{@message}")
else
LineNotify.send(subscription.user.line_notify_token, message: "\n\n台科大行事曆\n\n近七日沒有任何活動哦!祝你一切順心!")
end
end
end

private

def calendar_events_list(duration = 7.days)
calendar_id = "b10730224@gapps.ntust.edu.tw"
url = "https://www.googleapis.com/calendar/v3/calendars/#{calendar_id}/events"
response = Faraday.get(url, {
key: ENV["GOOGLE_API_KEY"],
singleEvents: true,
order_by: "startTime",
time_min: DateTime.now,
time_max: DateTime.now + duration,
})
JSON.parse response.body
end
end
22 changes: 22 additions & 0 deletions app/views/notifies/calendar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="container">
<h1>最近 7 日活動</h1>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Summary</th>
<th scope="col">Start Date</th>
</tr>
</thead>
<tbody>
<% @event_list.try(:each) do |event| %>
<tr>
<td><%= "#{event.dig('id')[0..10]}..." %></td>
<td><%= event.dig('summary') %></td>
<td><%= event.dig('start', 'date') %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "發送活動資訊", calendar_notify_path, method: :post, class: "btn btn-primary text-white"%>
</div>
1 change: 1 addition & 0 deletions app/views/notifies/index.line.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
carousel do
cover
page("版本更新通知", "訂閱後有關於版本的更新都會通知你哦!")
page("台科大行事曆", "訂閱後每天都會通知你近 7 日有關於台科大的活動哦!")
end
end.to_json)%>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
root to: "notifies#version"
root to: "notifies#calendar"
#devise
devise_for :users, controllers: {
omniauth_callbacks: 'omniauth_callbacks'
Expand Down Expand Up @@ -75,6 +75,8 @@
get 'notifies/callback',to: 'notifies#callback'
get 'notifies/version', to: 'notifies#version'
post 'notifies/version', to: 'notifies#version_notify', as: 'version_notify'
get 'notifies/calendar', to: 'notifies#calendar'
post 'notifies/calendar', to: 'notifies#calendar_notify', as: 'calendar_notify'
#other
get '*other',to: 'guides#other'
end
25 changes: 25 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end

# Learn more: http://github.com/javan/whenever
set :output, "log/cron_log.log"
env :PATH, ENV['PATH']
every 1.day, at: '8:00 am' do
rake "line:calendar_notify"
end
7 changes: 7 additions & 0 deletions lib/tasks/line.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace :line do
desc "Send Calendar events which occur in 7 days to Subscriber."
task calendar_notify: :environment do
CalendarEventsNotify.new.perform
end

end

0 comments on commit b7c96c6

Please sign in to comment.