Mojolicious::Plugin::Nexmo - Asynchronous (and synchronous) SMS and TTS (Text To Speech) sending with Nexmo provider.
0.90
plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd', from => 'YourCompanyName', # This options are global, you don't need lg => 'de-de' # to declare them in every request };
use Mojolicious::Lite plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd' }; # /block ? mode=SMS & phone_number=447525856424 & message=Hello! get '/block' => sub { my $self = shift; my $mod = $self->param('mode'); my $tel = $self->param('phone_number'); my $mes = $self->param('message'); $self->render(text => "$tel : $mes"); my ($err, $err_mes, $info) = $self->nexmo( mode => $mod, to => $tel, text => $mes ); }; app->start;
use Mojolicious::Lite; plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd' }; # /nonblock ? mode=SMS & phone_number=447525856424 & message=Hello! get '/nonblock' => sub { my $self = shift; my $mod = $self->param('mode'); my $tel = $self->param('phone_number'); my $mes = $self->param('message'); $self->render(text => "$tel : $mes"); $self->nexmo( mode => $mod, to => $tel, text => $mes, sub { my ($self, $err, $err_mes, $info) = @_; # ... } ); }; app->start;
use Mojolicious::Lite; use Mojo::IOLoo; plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd' }; # /delay ? mode=SMS & phone_number=447525856424 & message=Hello! get '/delay' => sub { my $self = shift; my $mod = $self->param('mode'); my $tel = $self->param('phone_number'); my $mes = $self->param('message'); Mojo::IOLoop->delay( sub { my $delay = shift; $self->nexmo( mode => $mod, to => $tel, text => $mes, $delay->begin # CallBack ); return $self->render(text => "$tel : $mes"); }, sub { my ($delay, $err, $err_mes, $info) = @_; # ... } ); }; app->start;
This plugin provides an easy way to send SMS and TTS with Nexmo API.
You can redefine global options:
plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd', from => 'YourCompanyName' # Global option }; # ... $self->nexmo( mode => $mod, to => $tel, text => $mes, from => 'NewCompanyName' # 'NewCompanyName' will be used in this response ); # ...
Or you can disable global options by setting them in undef:
undef
plugin 'Nexmo' => { api_key => 'n3xm0rocks', api_secret => '12ab34cd', lg => 'de-de' # Global option }; # ... $self->nexmo( mode => $mod, to => $tel, text => $mes, lg => undef # lg will be missed in this response ); # ...
Mojolicious::Plugin::Nexmo supports the following options:
Your Nexmo API key & API secret. This two options are required, you should always declare it globally:
plugin 'Nexmo' => { api_key => '...', api_secret => '...' # ... }
Can be 'SMS' or 'TTS'. Depending on mode there are different options:
See detailed description of SMS options at https://docs.nexmo.com/index.php/sms-api/send-message.
$self->nexmo( mode => 'SMS', # options );
See detailed description of TTS options at https://docs.nexmo.com/index.php/voice-api/text-to-speech.
$self->nexmo( mode => 'TTS', # options );
For asynchronous mode you should pass a callback as last parameter:
$self->nexmo( mode => 'SMS', # options sub { my ($self, $err, $err_mes, $info) = @_; # ... } );
If a callback is missed, plugin works in synchronous mode:
my ($err, $err_mes, $info) = $self->nexmo( mode => 'SMS', # options );
$err
Values:
Success.
Network error.
Nexmo error response codes.
See detailed description of SMS response codes at https://docs.nexmo.com/index.php/sms-api/send-message#response_code.
See detailed description of TTS response codes at https://docs.nexmo.com/index.php/voice-api/text-to-speech#tts_response_code.
IT'S IMPORTANT: If you use 'SMS' mode, message can be divided into several parts. If all parts were sent succesfully, then 0 is returned. Otherwise $err will contain error code of first failed part. If you need error codes of all parts, use $info hash.
$info
$err_mes
Hash that corresponds to the Nexmo JSON response.
Set MOJOLICIOUS_NEXMO_DEBUG environment variable to turn Nexmo debug on.
MOJOLICIOUS_NEXMO_DEBUG
$ MOJOLICIOUS_NEXMO_DEBUG=1 morbo test
Mojolicious::Plugin::SMS
Nexmo::SMS
SMS::Send::Nexmo
Andrey Khozov, <avkhozov@googlemail.com>
Georgiy Alexeew, <alexeew.georgiy@gmail.com>
Alexey Stavrov, <logioniz@yandex.ru>
Copyright (C) 2014, Andrey Khozov
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
To install Mojolicious::Plugin::Nexmo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Nexmo
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Nexmo
For more information on module installation, please visit the detailed CPAN module installation guide.