Skip to content

Commit 4e17743

Browse files
committed
Add perl/hangover.pl
Dependencies: - DateTime - SMS::Send::Twilio - YAML
1 parent 3bcd684 commit 4e17743

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

perl/hangover.pl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use DateTime;
7+
use SMS::Send;
8+
use YAML;
9+
10+
# Config
11+
my $conf = Load( <<'...' );
12+
---
13+
phone_numbers:
14+
my_number: +15005550006
15+
boss_number: +xxx
16+
reasons:
17+
- Locked out
18+
- Pipes broke
19+
- Food poisoning
20+
- Not feeling well
21+
...
22+
23+
my $date = DateTime->now;
24+
25+
# Skip on weekends
26+
if ( $date->day_of_week >= 6 ) {
27+
exit;
28+
}
29+
30+
# Exit early if no sessions with my username are found
31+
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
32+
33+
my @sessions = grep {
34+
m/$ENV{'USER'}/
35+
} <$cmd_who>;
36+
37+
close $cmd_who;
38+
39+
exit if ( scalar( @sessions ) == 0 );
40+
41+
# Load Twilio API config
42+
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
43+
LINE: while ( my $line = <$env> ) {
44+
next LINE unless ( $line =~ m/^(TWILIO[^=]+)=(.*)(?:[\n\r]*)/ );
45+
$conf->{'env'}->{ $1 } = $2;
46+
}
47+
48+
close $env;
49+
50+
# Randomize excuse
51+
my $reason_number = int( rand( scalar( @{ $conf->{'reasons'} } ) ) );
52+
my $sms_text = "Gonna work from home. ". $conf->{'reasons'}[ $reason_number ];
53+
54+
# Create an object. There are three required values:
55+
my $sender = SMS::Send->new('Twilio',
56+
_accountsid => $conf->{'env'}->{'TWILIO_ACCOUNT_SID'},
57+
_authtoken => $conf->{'env'}->{'TWILIO_AUTH_TOKEN'},
58+
_from => $conf->{'phone_numbers'}->{'my_number'},
59+
);
60+
61+
# Send a message to me
62+
my $sent = $sender->send_sms(
63+
text => $sms_text,
64+
to => $conf->{'phone_numbers'}->{'boss_number'},
65+
);
66+
67+
# Did it send?
68+
if ( $sent ) {
69+
print "Sent message.\n";
70+
} else {
71+
print "Message failed.\n";
72+
}
73+

0 commit comments

Comments
 (0)