Android-ElectricSheep-Automator
view release on metacpan or search on metacpan
lib/Android/ElectricSheep/Automator/Plugins/Apps/Viber.pm view on Meta::CPAN
. ' and @package="com.viber.voip"'
. ']'
;
@nodes = $xc->findnodes($asel);
$N = scalar @nodes;
if( $N == 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the specified recipient (".$params->{'recipient'}."): ${asel}"); return undef }
elsif( $N > 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the specified reci...
$node = $nodes[0];
$boundstr = $node->getAttribute('bounds');
if( ! defined $boundstr ){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); return undef }
if( $boundstr !~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); return undef }
$bounds = [[$1,$2],[$3,$4]];
# add the text in the node
# the message will be sanitised in input_text(), spaces will be replaced with %s
# unicode is not supported
# TODO: perhaps warn about unicode in message?
if( $self->mother->input_text({'bounds' => $bounds, 'text' => $message}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to input text on ${boundstr}. Note that there may be a maximum length for the message. Your message w...
sleep(1);
# and send!
# on the same XML we search for the send button
$asel = '//node'
. '['
. ' matches(@class,\'FrameLayout$\',"i")'
. ' and @resource-id="com.viber.voip:id/btn_send"'
. ' and @package="com.viber.voip"'
. ']'
;
@nodes = $xc->findnodes($asel);
$N = scalar @nodes;
if( $N == 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to find any nodes matching this XPath selector (for getting the recipient (".$params->{'recipient'}.") from the contacts on the central pane: ${asel}"); return un...
elsif( $N > 1 ){ $log->error("--begin matched nodes:\n".join("\n", @nodes)."\n--end nodes matched.\n\n${whoami} (via $parent), line ".__LINE__." : error, matched more than one node (see above) with this XPath selector (for getting the recipient (".$...
$node = $nodes[0];
# click the Send button
$boundstr = $node->getAttribute('bounds');
if( ! defined $boundstr ){ $log->error("${node}\n\n${whoami} (via $parent), line ".__LINE__." : error, above node does not have attribute 'bounds'."); return undef }
if( $boundstr !~ /\[\s*(\d+)\s*,\s*(\d+)\]\s*\[\s*(\d+)\s*,\s*(\d+)\]/ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to parse bounds string: '$boundstr'."); return undef }
$bounds = [[$1,$2],[$3,$4]];
# I know it does not support sending unicode to a textbox but ...
# TODO: check with a unicode recipient handle to see if this works
if( $verbosity > 0 ){ $log->info("To: '".Encode::decode_utf8($recipient)."'\nMessage:\n".Encode::decode_utf8($message)."\n--end message.\n${whoami} (via $parent), line ".__LINE__." : about to send the above message ...") }
if( $mock == 0 ){
# click it if not mocking
if( $self->mother->tap({'bounds' => $bounds}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to tap on $bounds."); return undef }
} else {
$log->warn("${whoami} (via $parent), line ".__LINE__." : The 'Send' button was not clicked because mock is ON.")
}
usleep(0.8);
return {};
}
# only pod below
=pod
=encoding utf8
=head1 NAME
Android::ElectricSheep::Automator::Plugins::Apps::Viber - Control the Viber app from your desktop via the ElectricSheep Automator
=head1 VERSION
Version 0.09
=head1 WARNING
Current distribution is extremely alpha. API may change.
=head1 SYNOPSIS
An L<Android::ElectricSheep::Automator> plugin which
interacts with the Viber app from the desktop.
use Android::ElectricSheep::Automator::Plugins::Apps::Viber;
my $viber = Android::ElectricSheep::Automator::Plugins::Apps::Viber->new({
'configfile' => $configfile,
'verbosity' => 1,
# we already have a device connected and ready to control
'device-is-connected' => 1,
});
# go to home screen to start fresh
$plugobj->mother->home_screen();
# open the viber app
$plugobj->open_app() or die
# is the app running now?
$plugobj->is_app_running() or die
$plugobj->send_message({
'recipient' => 'My Notes', # some of your contacts
# 1) no unicode, 2) each space must be converted to '%s'
'message' => 'thank%syou'
}) or die;
=head1 MOTIVATION
I wanted to send viber messages from my beloved Linux Desktop
using the command line. I did not want to install Viber app on
my smartphone. I do not use a smartphone except for
reading the news.
=head1 INSTALLING VIBER
Firstly, I installed an Android emulator
in my desktop computer, as described here
L<https://metacpan.org/pod/Android::ElectricSheep::Automator#Android-Emulators>
Note that some Android emulators do not allow access to
Google App Store (whatever is called). You can only install apps
via third party which is not very safe (even for your emulator).
See this for how to enable the App Store:
L<https://stackoverflow.com/questions/71815181/how-can-i-get-google-play-to-work-on-android-emulator-in-android-studio-bumblebe>
Fortunately, Viber offers a version of its app from its website,
calling it an "update". And you will not need to give any personal
details to Google to download it.
It is located here
L<https://download.cdn.viber.com/android/android_32_64/viber-lst.apk>
(via L<https://www.viber.com/en/download-android-update>).
=head1 CONSTRUCTOR
=head2 new($params)
Creates a new C<Android::ElectricSheep::Automator::Plugins::Apps::Viber> object. C<$params>
is a hash reference used to pass initialization options. These options are
passed onto the main constructor.
Refer to the documentation of
L<Android::ElectricSheep::Automator#new($params)> for
A configuration file or hash is required.
Here is an example configuration file to get you started:
</* $VERSION = '0.01'; */>
</* comments are allowed */>
</* and <% vars %> and <% verbatim sections %> */>
{
"Android::ElectricSheep::Automator" : {
"adb" : {
"path-to-executable" : "/usr/local/android-sdk/platform-tools/adb"
},
"debug" : {
"verbosity" : 0,
</* cleanup temp files on exit */>
"cleanup" : 1
},
"logger" : {
</* log to file if you uncomment this */>
</* "filename" : "..." */>
}
</* config for our plugins (each can go to separate file also) */>
},
"Android::ElectricSheep::Automator::Plugins::Apps::Viber" : {
}
}
All sections are mandatory. Setting C<"adb"> to the wrong path will
yield in problems.
=head1 METHODS
( run in 0.602 second using v1.01-cache-2.11-cpan-39bf76dae61 )