1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| use strict;
use warnings;
use CGI ':standard';
use JSON::XS 'decode_json', 'encode_json';
use IO::All;
use YAML::XS qw/DumpFile LoadFile/;
use FindBin;
use Socialtext::Resting::Getopt qw/get_rester/;
use Weather::Underground;
use Acme::LOLCAT ();
use Finance::Quote;
use Locale::Currency::Format;
use URI::Escape;
use DateTime;
use DateTime::Duration;
use utf8;
# Use a local YAML file to store state. Make sure
# this file exists and is writable.
my $Conf_file = "$FindBin::Bin/sally.yaml";
my $Config = load_config();
# Use a config file to hold the Socialtext server
# and username/password details.
my $rester = get_rester( 'rester-config' => "$FindBin::Bin/.sally-rester");
eval {
print header('text/plain');
if (my $js = param('json_payload')) {
my $signals = decode_json($js);
my $signal = $signals->[0]->{object};
my $actor = $signals->[0]->{actor};
my $at = $signals->[0]->{at};
# Map the tags into a hash for ease
$signal->{tags} = { map {$_ => 1} @{$signal->{tags}}};
# The thread is where our Reply will be sent to.
# Because we always reply to the incoming message, our replies will
# be sent to the appropriate context - private or to the group or
# account they came from.
$signal->{thread} = $signal->{in_reply_to}{signal_id} || $signal->{id};
my $resp;
my @annotations;
my $begin = "^(?:{user: $Config->{our_user_id}}\\s+)?#?";
my $STOCK_SYMBOL = qr/\b([A-Z]{3,4})\b/;
# Now look at the signal body for opportunities to interact.
if ($signal->{body} =~ m/${begin}(tell|ask)\s+{user: (\d+)}\s+(.+)/i) {
# Tell or Ask means we will store a message to deliver to a user
# later.
my ($verb, $user_id, $msg) = ($1, $2, $3);
if ($user_id == $Config->{our_user_id}) {
$resp = "Uh, thanks {user: $actor->{id}} - I'll, uh, do that.";
}
else {
push @{ $Config->{tell}{$user_id} }, {
from => $actor->{id},
at => $at,
verb => $verb,
message => $msg,
};
save_config();
$resp = "I'll pass that on to {user: $user_id}";
}
}
elsif ($actor->{id} == $Config->{our_user_id}) {
# Ignore our own messages.
return;
}
elsif ($signal->{body} =~ m/${begin}weather (.+)/i) {
# Look up the weather and reply with what we found
my $query = $1;
($resp, my $places) = handle_weather($query, \@annotations);
if ($places) {
# Set up some interactions for our weather response.
push @annotations, {
interactor => { title => "More about \u$query..." }
};
for (@$places) {
my $place = $_->{place};
push @annotations, {
action => {
title => "Map for $place",
url => "http://maps.google.com/maps?f=q&z=15&output=embed&q=" . uri_escape($place),
target => "frame",
width => "100%",
height => "400"
},
};
push @annotations, {
action => {
title => "Search for $place",
url => "http://www.google.com/search?q=" . uri_escape($place),
target => "blank",
},
};
}
}
}
elsif ($signal->{body} =~ m/${begin}lol:? (.+)/i) {
# LOLbot repeats the signal using Acme::LOLCAT
$resp = Acme::LOLCAT::translate($1);
}
elsif ($signal->{body} =~ m/${begin}exchange\s+.*?([\d.]*)\s*(\w{3}) (?:to )?(\w{3})/i) {
# Currency Exchange
$resp = handle_exchange($1, $2, $3);
}
elsif ($signal->{body} =~ m/\b(quote|stock)\b.+$STOCK_SYMBOL/) {
$resp = '';
while ($signal->{body} =~ m/$STOCK_SYMBOL/g) {
# Look up a stock quote
$resp .= handle_stock($1);
}
}
elsif ($signal->{body} =~ m/${begin}math (.+)/i) {
# Do some math
$resp = handle_math($1);
}
elsif ($signal->{body} =~ m/${begin}seen {user: (\d+)}/i) {
# Handle requests for the last signal a user sent.
my $user_id = $1;
if (my $last = $Config->{last_seen}{$user_id}) {
(my $at = $last->{at}) =~ s/(.+)\..+/$1/;
$resp = qq(I last saw {user: $user_id} saying '$last->{body}' at "$at"<) . $rester->server . qq($last->{uri}>);
}
else {
$resp = "Sorry, I haven't seen {user: $user_id}";
}
}
elsif ($signal->{body} =~ m/${begin}poll(?:\s+(.+)|\s*$)/i) {
# Start a poll
my ($topic) = ($1);
$topic ||= '';
push @annotations, {
interactor => { title => "Start a poll..." }
};
# In this case, the poll itself is handled by the hal9000 script.
push @annotations, {
action => {
title => "Set Answers",
url => "http://borax.socialtext.net/~audreyt/hal9000/poll.cgi/$signal->{signal_id}/start?q="
. uri_escape($topic) . ';user=' . $actor->{id}
. ';account_ids=' . join(',', @{$signal->{account_ids}||[]})
. ';group_ids=' . join(',', @{$signal->{group_ids}||[]})
. ';request_uri=' . $ENV{SCRIPT_URI},
target => "frame",
width => "100%",
height => "300"
},
};
$resp = "{user: $actor->{id}} Please click 'Start a poll...' to the right to set the answers."
}
elsif ($signal->{body} =~ m/^{user: $Config->{our_user_id}} (?:commands|help)$/) {
# This is kind of a usage help.
$resp = help_text();
push @annotations, detailed_help_annotation();
}
elsif ($signal->{tags}{bug}) {
# If someone mentions the #bug tag, send them the link to our bug
# reporting form, but only once per thread.
unless ($Config->{bug_report}{$signal->{thread}}) {
$Config->{bug_report}{$signal->{thread}} = 1;
$resp = qq{If you've found a bug, please use the "bug reporting form"<https://www2.socialtext.net/bug-report/index.cgi?internal_bug_report>};
}
}
elsif ($signal->{body} =~ /roll ((\d+)d(\d+))/i) {
my ($arg, $n, $die) = ($1,$2,$3);
my @rolls;
push @rolls, int(rand($die))+1 for (1..$n);
my $sum = 0;
$sum += $_ for @rolls;
$resp = "rolled $arg got ".join(', ',@rolls)." for $sum\n";
}
elsif ($signal->{body} =~ m/selling days/i) {
my $days = selling_days_left_in_the_quarter();
$resp = "There are $days selling days left in the quarter.";
}
elsif ($signal->{body} =~ m/^{user: $Config->{our_user_id}} (.+)$/
or (($signal->{recipient_id}||0) == $Config->{our_user_id})) {
# For any other kind of mention
$resp = help_text();
push @annotations, detailed_help_annotation();
}
if ($resp) {
send_signal(
$resp,
in_reply_to => { signal_id => $signal->{id} },
(@annotations ? (annotations => \@annotations) : ()),
);
}
# Remember each user's last signal for the seen command.
$Config->{last_seen}{$actor->{id}} = {
body => $signal->{body},
at => $at,
uri => $signal->{uri},
};
save_config();
# Check if there is any messages for the user that just signalled.
if (my $msgs = $Config->{tell}{$actor->{id}}) {
for my $msg (@$msgs) {
(my $at = $msg->{at}) =~ s/(.+)\..+/$1/;
my $verb = $msg->{verb} || 'tell';
my $message = "{user: $actor->{id}}: {user: $msg->{from}} said to $verb you $msg->{message} (at $at)";
send_signal($message);
}
delete $Config->{tell}{$actor->{id}};
save_config();
}
}
elsif (my $jsp = param('json_post')) {
# This allows other scripts (like the hal9000 poll bot) to post http to
# sally, and have sally send a signal.
my $signal = decode_json($jsp);
send_signal(delete $signal->{body}, %$signal);
}
elsif (param('detailed_help')) {
print <<'EOT';
You can interact with me in several ways:
I can look up the weather in a given place. Examples:
weather New York, NY
weather 90210
weather SFO
I can exchange currencies:
exchange USD to GBP
exchange CAD to USD
exchange $50 CAD to USD
I will look up any thing that looks like a stock symbol:
Price of GOOG?
What is the price of GOOG, AAPL and RIM?
I'm good at math:
math 9 * 42
math ((50000 * 20) * 0.87) - ((50000 * 20) * 0.75)
I can help you poll your peers about some question:
poll
poll How far do you live from Florida?
I can translate into LOLcat:
lol Can I have a cheeseberger?
I can tell you when I last saw a particular user send a signal:
seen @SomeUser
I can remind someone when they next return to signals:
tell @SomeUser to read the memo and fill out their TPS reports.
And for you keeners out there, my source code is available here:
<a href="http://github.com/socialtext/st-clients/blob/master/rest/perl/sally/sally.cgi" target="_blank">http://github.com/socialtext/st-clie...ally/sally.cgi</a>
EOT
exit;
}
};
if ($@) {
# Dump any errors to a temp file.
DumpFile('/tmp/sally-error', { error => $@ } );
}
print "KTHXBYE\n";
exit;
sub send_signal {
my $text = shift;
my %args = @_;
$rester->post_signal($text, %args);
}
sub load_config {
return LoadFile($Conf_file) if -e $Conf_file and -s _;
# Determine our user_id
my $me = $rester->get_user($rester->username);
$Config = {
our_user_id => $me->{user_id},
};
save_config();
return $Config;
}
sub save_config {
DumpFile($Conf_file, $Config);
}
sub handle_weather {
my $place = shift;
my $annos = shift;
my $weather = Weather::Underground->new(place => $place) or
die "Can't load weather for $place: $@\n";
my $ref = $weather->get_weather();
unless ($ref) {
push @$annos, detailed_help_annotation();
return "Sorry, I cannot look up the weather in $place. "
. "Try: _city, state_ or _zip code_ or _airport_";
}
my $w = $ref->[0];
return("In $w->{place} it is $w->{fahrenheit}°F ($w->{celsius}°C) and $w->{conditions}", $ref);
} |
Partager