App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Plugin/API/Translate.pm  view on Meta::CPAN

                         example     => 'en de "Good morning!"',
                         result      => '"Guten Morgen!"', },

        'translate-party' => { method      => 'translate_party',
                               description => 'Repeatedly translates the given phrase back and forth between languages until equilibrium is found.',
                               usage       => '<from> <to> <text>',
                               example     => '',
                               result      => '', },
    }},
);

has 'token' => (
    is  => 'rw',
    isa => 'Str',
);

has 'last_authed' => (
    is      => 'rw',
    isa     => 'Num',
    default => 0,
);

has 'ua' => (
    is      => 'rw',
    isa     => 'LWP::UserAgent',
    default => sub {
        my $ua = LWP::UserAgent->new;
        $ua->agent('App::RoboBot');
        $ua->timeout(3);
        return $ua;
    },
);

has 'valid_config' => (
    is      => 'rw',
    isa     => 'Bool',
    traits  => [qw( SetOnce )],
);

has 'cache' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { {} },
);

sub init {
    my ($self, $bot) = @_;

    if (exists $self->bot->config->plugins->{'translate'}{'client'}
            && exists $self->bot->config->plugins->{'translate'}{'secret'}) {
        if ($self->update_token) {
            $self->valid_config(1);
        } else {
            $self->valid_config(0);
        }
    } else {
        $self->valid_config(0);
    }
}

sub update_token {
    my ($self) = @_;

    # last_authed only changes when it was successful, so if we have a current
    # one, skip all the work ahead
    return 1 if $self->last_authed > time() - 590;

    my $client = $self->bot->config->plugins->{'translate'}{'client'};
    my $secret = $self->bot->config->plugins->{'translate'}{'secret'};

    my $response = $self->ua->post('https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
        { grant_type    => 'client_credentials',
          client_id     => $client,
          client_secret => $secret,
          scope         => 'http://api.microsofttranslator.com' });

    return 0 unless $response->is_success;

    my $json;
    eval {
        $json = decode_json($response->decoded_content);
    };
    return 0 if $@;
    return 0 unless ref($json) eq 'HASH' && exists $json->{'access_token'} && $json->{'access_token'} =~ m{HMACSHA256};

    $self->token($json->{'access_token'});
    $self->last_authed(time() +0);

    return 1;
}

sub translate_text {
    my ($self, $message, $command, $rpl, $from, $to, @args) = @_;

    unless ($self->valid_config) {
        $message->response->raise('This bot instance does not have a valid Microsoft Translate API configuration. No translations will be possible.');
        return;
    }

    unless (defined $from && defined $to && length($from) > 1 && length($to) > 1) {
        $message->response->raise('Must provide a source and destination language for translation.');
        return;
    }

    if (lc($from) eq lc($to)) {
        $message->response->raise('Translate to the same language? What is the point in that?');
        return;
    }

    my $text = join(' ', @args);

    unless (defined $text && length($text) > 1) {
        $message->response->raise('Must provide text to translate.');
        return;
    }

    my $translation = $self->_do_translate($from, $to, $text);

    return $translation if defined $translation;

    $message->response->raise('Could not translate your phrase. Please check your source and destination languages.');



( run in 1.113 second using v1.01-cache-2.11-cpan-39bf76dae61 )