Last active
August 29, 2015 14:07
-
-
Save sinergy/bd885cd301c3e5b7bf59 to your computer and use it in GitHub Desktop.
A script which read the duty table CSV file, and send a notification message to the specific room of HipChat. 讀取 CSV 中的值日生名單,並且透過 hipchat.pl 這隻 perl script 傳遞提醒訊息至指定的 room 中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
日期 | 值日生 | |
---|---|---|
2014/07/04 | Arc | |
2014/07/11 | Clark | |
2014/07/18 | Jack | |
2014/07/25 | William | |
2014/08/01 | kiki | |
2014/08/08 | Cloud | |
2014/08/15 | Isaac | |
2014/08/22 | Haru | |
2014/08/29 | Jimmy | |
2014/09/05 | Eddie | |
2014/09/12 | John | |
2014/09/19 | Arc | |
2014/09/26 | Clark | |
2014/10/03 | Jack | |
2014/10/10 | William | |
2014/10/17 | kiki | |
2014/10/24 | Cloud | |
2014/10/31 | Isaac | |
2014/11/07 | Haru | |
2014/11/14 | Jimmy | |
2014/11/21 | Eddie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Perl script to send a on duty notification to hipchat using either the REST API v2. | |
# | |
# Modified by Cloud Chen. | |
# | |
use warnings; | |
use strict; | |
use Text::CSV; | |
use Getopt::Long; | |
use Time::Piece; | |
use FindBin qw($RealBin); | |
my $usage = "This script will read the source CSV and send a notification to hipchat when there is one on duty today.\n | |
Usage: | |
\t-source CSV duty table. Example: '-source \"duty_table.csv\"' | |
"; | |
my $pathHipchatScript = $RealBin."/hipchat.pl"; | |
#print "\tabsolute path of hipchat script: $pathHipchatScript\n"; | |
# Basic config | |
my $token = "YOUR TOKEN HERE"; | |
my $roomId = "YOUR ROOM ID HERE"; | |
my $message = "提醒您,本週的值日生是 @"; | |
my $srcFile = ""; | |
# Get input options | |
GetOptions("source=s" => \$srcFile); | |
if ($srcFile eq "") { | |
print "You must specify a source CSV file!\n"; | |
die ("$usage\n"); | |
} | |
my $absPath = $RealBin."/".$srcFile; | |
print "absolute path of source file: $absPath\n"; | |
# Replace the '^M' carriage return with general linux line break. | |
my $replace_carriage_return = "perl -p -i -e 's/\\r/\\n/g' ".$absPath; | |
system($replace_carriage_return); | |
# Get current time | |
my $date = localtime->strftime('%Y/%m/%d'); | |
print "date: $date\n"; | |
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag(); | |
open (my $fileHandle, "<:encoding(utf8)", $absPath) or die "$absPath : $!"; | |
while (my $row = $csv->getline($fileHandle)) { | |
# date format example: 2014/09/11 | |
$row->[0] =~ m/^[\d]{4}\/[\d]{2}\/[\d]{2}$/ or next; | |
if ($date eq $row->[0]) { | |
$message.=$row->[1]; | |
my @args = ('perl', $pathHipchatScript, "-room", "$roomId", "-token", "$token", "-message", "$message"); | |
system(@args); | |
# Remove the one who has been notified. | |
my $remove_notified = "perl -i -ne 'print \$_ if \$. != $.' $absPath"; | |
print "remove_notified command: $remove_notified\n"; | |
system($remove_notified); | |
last; | |
} | |
} | |
$csv->eof; | |
close $fileHandle or die "$fileHandle: $!"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Perl script to send a on duty notification to hipchat using either the REST API v2. | |
# | |
# Modified by Cloud Chen. | |
# | |
use warnings; | |
use strict; | |
use Text::CSV; | |
use Getopt::Long; | |
use DateTime; | |
use DateTime::Duration; | |
use FindBin qw($RealBin); | |
my $usage = "This script will read the source CSV and send a notification to the room of hipchat before one week from the talk.\n | |
Usage: | |
\t-source CSV present rotation table. Example: '-source \"presents.csv\"' | |
"; | |
# Basic config | |
my $token = "YOUR TOKEN HERE"; | |
my $roomId = "HIPCHAT ROOM ID HERE"; | |
my $pathHipchatScript = $RealBin."/hipchat.pl"; | |
my $remind_message = "下週技術研討的主講者是 @%s, 提醒您別忘囉!"; | |
my $on_time_message = "\@All 又到了大家最期待的「技術研討」下午茶時間囉!本週的主講者是 @%s,趕緊到會議室集合並聆聽這場精采的簡報吧!Move your ass now please :)"; | |
my $srcFile = ""; | |
# Get input options | |
GetOptions("source=s" => \$srcFile); | |
if ($srcFile eq "") { | |
print "You must specify a source CSV file!\n"; | |
die ("$usage\n"); | |
} | |
my $absPath = $RealBin."/".$srcFile; | |
print "absolute path of source file: $absPath\n"; | |
# Replace the '^M' carriage return with general linux line break. | |
my $replace_carriage_return = "perl -p -i -e 's/\\r/\\n/g' ".$absPath; | |
system($replace_carriage_return); | |
# Get current time | |
my $dt = DateTime->today(); | |
my $str_today = $dt->ymd("/"); | |
print "today: $str_today\n"; | |
my $csv = Text::CSV->new({binary => 1}) or die "Cannot use CSV: ".Text::CSV->error_diag(); | |
open (my $fileHandle, "<:encoding(utf8)", $absPath) or die "$absPath : $!"; | |
while (my $row = $csv->getline($fileHandle)) { | |
# date format example: 2014/09/11 | |
$row->[0] =~ m/^([\d]{4})\/([\d]{2})\/([\d]{2})$/ or next; | |
my $present_date = DateTime->new( | |
year => $1, | |
month => $2, | |
day => $3, | |
hour => $dt->hour(), | |
minute => $dt->minute(), | |
second => $dt->second(), | |
time_zone => $dt->time_zone() | |
); | |
my $log = sprintf("Presentation day: %s\n", $present_date->ymd("/"));; | |
print $log; | |
my $a_week_before_present_date = $present_date->subtract( | |
DateTime::Duration->new(weeks => 1) | |
); | |
$log = sprintf("\t - A week before P-Day: %s\n", $a_week_before_present_date->ymd("/")); | |
print $log; | |
my $str_week_before = $a_week_before_present_date->ymd("/"); | |
if ($str_today eq $row->[0]) { | |
my $message = sprintf($on_time_message, $row->[1]); | |
my @args = ('perl', $pathHipchatScript, "-room", "$roomId", "-token", "$token", "-message", "$message"); | |
system(@args); | |
# Remove the one who has been notified. | |
my $remove_notified = "perl -i -ne 'print \$_ if \$. != $.' $absPath"; | |
print "remove_notified command: $remove_notified\n"; | |
system($remove_notified); | |
last; | |
} | |
# Check if it is the day to send notification reminder for the speaker | |
if ($str_today eq $str_week_before) { | |
my $message = sprintf($remind_message, $row->[1]); | |
my @args = ('perl', $pathHipchatScript, "-room", "$roomId", "-token", "$token", "-message", "$message"); | |
system(@args); | |
} | |
} | |
$csv->eof; | |
close $fileHandle or die "$fileHandle: $!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment