| 1 | package Plack::App::Openqq::SendMessage; | ||
| 2 | use parent qw(Plack::Component); | ||
| 3 | use URI::Escape qw(uri_unescape); | ||
| 4 | use JSON; | ||
| 5 | use Encode; | ||
| 6 | sub call{ | ||
| 7 | my $self = shift; | ||
| 8 | my $client = $self->{client}; | ||
| 9 | my $env = shift; | ||
| 10 | my %query_string; | ||
| 11 | for my $query_string (split(/&/,$env->{QUERY_STRING} )){ | ||
| 12 | my($key,$value) = split /=/,$query_string; | ||
| 13 | $query_string{$key} = $value; | ||
| 14 | } | ||
| 15 | my $uin; | ||
| 16 | if(defined $query_string{qq}){ | ||
| 17 | $uin = $client->get_uin_from_qq($query_string{qq}); | ||
| 18 | } | ||
| 19 | else{ | ||
| 20 | $uin = $query_string{uin}; | ||
| 21 | } | ||
| 22 | my $content = uri_unescape($query_string{content}); | ||
| 23 | |||
| 24 | return sub { | ||
| 25 | my $responder = shift; | ||
| 26 | my $msg = $client->create_msg(to_uin=>$uin,content=>$content); | ||
| 27 | $msg->{cb} = sub{ | ||
| 28 | my($msg,$is_success,$status) = @_; | ||
| 29 | my $res = { | ||
| 30 | msg_id => $msg->{msg_id}, | ||
| 31 | code => $is_success, | ||
| 32 | status => decode("utf8",$status), | ||
| 33 | }; | ||
| 34 | my $json = JSON->new->utf8->encode($res); | ||
| 35 | $responder->([ | ||
| 36 | 200, | ||
| 37 | ['Content-Type' => 'text/plain'], | ||
| 38 | [$json], | ||
| 39 | ]); | ||
| 40 | }; | ||
| 41 | $client->send_message($msg); | ||
| 42 | }; | ||
| 43 | |||
| 44 | } | ||
| 45 | 1; |