Android-ElectricSheep-Automator

 view release on metacpan or  search on metacpan

script/electric-sheep-viber-send-message.pl  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

our $VERSION = '0.09';

use lib ('blib/lib');

use Getopt::Long qw(:config no_ignore_case);
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;

use Android::ElectricSheep::Automator::Plugins::Apps::Viber;

my $VERBOSITY = 0; # we need verbosity of 10 (max), so this is not used
my ($DEVICE, %SENDPARS, $CONFIGFILE);
my $CLOSE_APP_AFTER = 0;
if( ! Getopt::Long::GetOptions(
  'recipient=s' => sub { $SENDPARS{$_[0]} = $_[1] },
  # 1) no unicode, 2) each space must be converted to '%s'
  'message=s' => sub { $SENDPARS{$_[0]} = $_[1] },
  'outbase=s' => sub { $SENDPARS{$_[0]} = $_[1] },
  'mock' => sub { $SENDPARS{$_[0]} = 1 },

  'close-app-after' => sub { $CLOSE_APP_AFTER = 1 },
  'device|d=s' => \$DEVICE,
  'verbosity|v=i' => \$VERBOSITY,
  'configfile|c=s' => \$CONFIGFILE,
  'help|h' => sub { print STDOUT usage(); exit(0); }
) ){ die usage() }

for ('message', 'recipient'){
	if( ! exists($SENDPARS{$_}) || ! defined($SENDPARS{$_}) ){ print STDERR usage(); print STDERR "\n$0 : error, input parameter '$_' is missing.\n"; exit(1); }
}
if( ! defined $CONFIGFILE ){ print STDERR usage(); print STDERR "\n$0 : error, a configuration file must be specified with '--configfile'.\n"; exit(1); }
if( ! -f $CONFIGFILE ){ die "$0 : failed to find config file '$CONFIGFILE'." }

my $params = {
	'configfile' => $CONFIGFILE,
	'verbosity' => $VERBOSITY,
	'device-connected' => 1,
};
# we assume there is a device connected which the user
# must specify by serial, of if just one, we connect to
# it without the serial
if( defined $DEVICE ){ $params->{'device-serial'} = $DEVICE }
else { $params->{'device-is-connected'} = 1 }

my $client = Android::ElectricSheep::Automator::Plugins::Apps::Viber->new($params);
if( ! defined($client) ){ die "$0 : failed to instantiate the automator." }

# navigate to the home screen, get rid of previous tests rubbish
$client->mother->home_screen();

my ($res);
# open the app if it is not running or bring it to foreground
# IT IS IMPORTANT TO BE IN THE FOREGROUND
# open the app
$res = $client->open_app();
if( ! defined($res) ){ die "$0 : failed to open Viber app." }
sleep(4);
# is the app running?
$res = $client->is_app_running();
if( ! defined($res) ){ die "$0 : failed to check if Viber app is running." }
if( $res != 1 ){ die "$0 : Viber app is not running yet, perhaps increase the waiting time..." }

# ok app is running and on the foreground

my $ret = $client->send_message(\%SENDPARS);
if( ! defined($ret) ){ die "$0 : failed to send message." }
if( $VERBOSITY > 0 ){ print STDOUT "$0 : message sent OK.\n" }



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