App-SpreadRevolutionaryDate

 view release on metacpan or  search on metacpan

lib/App/SpreadRevolutionaryDate/MsgMaker/Gemini.pm  view on Meta::CPAN

use 5.014;
use utf8;
package App::SpreadRevolutionaryDate::MsgMaker::Gemini;
$App::SpreadRevolutionaryDate::MsgMaker::Gemini::VERSION = '0.54';
# ABSTRACT: MsgMaker class for L<App::SpreadRevolutionaryDate> to build message with Gemini prompt

use Moose;
with 'App::SpreadRevolutionaryDate::MsgMaker';

use DateTime;
use File::ShareDir ':ALL';
use LWP::UserAgent;
use JSON;

use Locale::TextDomain 'App-SpreadRevolutionaryDate';
use namespace::autoclean;

has 'api_key' => (
  is  => 'ro',
  isa => 'Str',
  required => 1,
);

has 'process' => (
  is  => 'ro',
  isa => 'Str',
  required => 1,
);

has 'prompt' => (
  is  => 'ro',
  isa => 'HashRef[Str]',
  required => 1,
);

has 'search' => (
  is  => 'ro',
  isa => 'HashRef[Bool]',
);

has 'intro' => (
  is  => 'ro',
  isa => 'HashRef[Str]',
);

has 'img_path' => (
  is  => 'ro',
  isa => 'HashRef[Str]',
);

has 'img_url' => (
  is  => 'ro',
  isa => 'HashRef[Str]',
);

has 'img_alt' => (
  is  => 'ro',
  isa => 'HashRef[Str]',
);

has '+locale' => (
  default => 'fr',
);

around BUILDARGS => sub {
  my ($orig, $class, %args) = @_;

  if ($args{process}) {
      die "Process $args{process} has no prompt\n" unless $args{prompt}->{$args{process}};
  }

  return $class->$orig(%args);
};


sub compute {
  my $self = shift;

  my $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key= ' . $self->api_key;

  my $today = DateTime->now(locale => $self->locale);
  my $prompt = $self->prompt->{$self->process};
  my @vars = $self->prompt->{$self->process} =~ /\$(\w+)/g;
  foreach my $var (@vars) {
    $prompt =~ s/\$$var/$today->$var/e;
  }

  my $payload = {
    contents => [
      {
        parts => [
          {
            text => $prompt,
          }
        ],
      }
    ],
  };

  if ($self->search && $self->search->{$self->process}) {
    $payload->{tools} = [
      {
        google_search => {},
      },
   ];
  }

  my $json = JSON->new->utf8;
  my $args_json = $json->encode($payload);

  my $ua = LWP::UserAgent->new(env_proxy => 1, timeout => 10, agent =>'App::SpreadRevolutionaryDate bot');
  $ua->default_header('Accept' => 'application/json');
  $ua->default_header('Content-Type' => 'application/json');
  my $req = HTTP::Request->new('POST', $url);
  $req->content($args_json);
  my $resp = $ua->request($req);

  my $msg;
  if ($resp && $resp->is_success) {
    my $content;
    eval { $content = $json->decode($resp->content) };
    unless ($@) {
      if ($content->{candidates} && scalar(@{$content->{candidates}}) == 1 && $content->{candidates}->[0]->{content} && $content->{candidates}->[0]->{content}->{parts} && scalar(@{$content->{candidates}->[0]->{content}->{parts}}) >= 1 && $content->{c...
        $msg = $content->{candidates}->[0]->{content}->{parts}->[0]->{text};
        $msg =~ s/\s+$//;
        $msg .= "\n#IAGenerated #" . $self->process;
      }
    }
  } else {
    my $error = $resp->content ? $json->decode($resp->content) : $resp->msg;
    if (ref($error) eq 'HASH' && $error->{error} && $error->{error}->{message}) {
      die $error->{error}->{message} . "\n";
    } else {
      die $error . "\n";
    }
  }

  if ($self->intro && $self->intro->{$self->process}) {
    my $intro = $self->intro->{$self->process};
    my @intro_vars = $self->intro->{$self->process} =~ /\$(\w+)/g;
    foreach my $intro_var (@intro_vars) {



( run in 0.645 second using v1.01-cache-2.11-cpan-ceb78f64989 )