Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Please add unicode support #1

Merged
merged 1 commit into from over 2 years ago

3 participants

Jui-Nan Lin Michiel Beijen Renee
Jui-Nan Lin

Nexmo support unicode text SMS, and I just add this type to Nexmo::SMS.

Renee reneeb merged commit c80f770 into from November 25, 2011
Renee reneeb closed this November 25, 2011
Michiel Beijen

Hi Renée, Jui-Nan Lin,

I don't really understand why Unicode needs to be supported by Nexmo::SMS::UnicodeMessage.
All it does, when you compare it to TextMessage, is send a type => 'unicode', to the server.
I'd say this can be just as easily supported from the existing TextMessage. And maybe it should set it by default?

Because UnicodeMessage will send Unicode, but what will TextMessage send? Why not use this Unicode flag by default when sending to Nexmo?

Would love to hear your feedback.

Renee
Owner

Hi Michiel,

according to the Nexmo documentation there is a difference between text messages and unicode messages. Even though the text has to be utf-8 encoded anyway. But if you use chars outside the latin-range, you have to use unicode message.

I will ask Nexmo staff if there is any penalty if unicode flag is used by default.

  • Renée
Jui-Nan Lin

Hi,

Some destination numbers does not support type=unicode messages (for example, US numbers).

And it's fine to make type an optional parameter in Nexmo::SMS::TextMessage to support unicode messages. I will make a patch for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 25, 2011
Jui-Nan Lin add unicode text support c80f770
This page is out of date. Refresh to see the latest.
2  lib/Nexmo/SMS.pm
@@ -6,6 +6,7 @@ use strict;
6 6
 use Nexmo::SMS::BinaryMessage;
7 7
 use Nexmo::SMS::TextMessage;
8 8
 use Nexmo::SMS::WAPPushMessage;
  9
+use Nexmo::SMS::UnicodeMessage;
9 10
 
10 11
 use Nexmo::SMS::GetBalance;
11 12
 
@@ -116,6 +117,7 @@ sub sms {
116 117
     
117 118
     my %types = (
118 119
         text    => 'Nexmo::SMS::TextMessage',
  120
+        unicode => 'Nexmo::SMS::UnicodeMessage',
119 121
         binary  => 'Nexmo::SMS::BinaryMessage',
120 122
         wappush => 'Nexmo::SMS::WAPPushMessage',
121 123
     );
261  lib/Nexmo/SMS/UnicodeMessage.pm
... ...
@@ -0,0 +1,261 @@
  1
+package Nexmo::SMS::UnicodeMessage;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+
  6
+use Nexmo::SMS::Response;
  7
+
  8
+use LWP::UserAgent;
  9
+use JSON::PP;
  10
+
  11
+=head1 NAME
  12
+
  13
+Nexmo::SMS::UnicodeMessage - Module that respresents a text message for the Nexmo SMS API!
  14
+
  15
+=head1 VERSION
  16
+
  17
+Version 0.01
  18
+
  19
+=cut
  20
+
  21
+our $VERSION = '0.01';
  22
+
  23
+my %attrs = (
  24
+    text              => 'required',
  25
+    type              => 'required',
  26
+    from              => 'required',
  27
+    to                => 'required',
  28
+    server            => 'required',
  29
+    username          => 'required',
  30
+    password          => 'required',
  31
+    status_report_req => 'optional',
  32
+    client_ref        => 'optional',
  33
+    network_code      => 'optional',
  34
+);
  35
+
  36
+for my $attr ( keys %attrs ) {
  37
+    no strict 'refs';
  38
+    *{ __PACKAGE__ . '::' . $attr } = sub {
  39
+        my ($self,$value) = @_;
  40
+        
  41
+        my $key = '__' . $attr . '__';
  42
+        $self->{$key} = $value if @_ == 2;
  43
+        return $self->{$key};
  44
+    };
  45
+}
  46
+
  47
+=head1 SYNOPSIS
  48
+
  49
+This module simplifies sending SMS through the Nexmo API.
  50
+
  51
+
  52
+    use Nexmo::SMS::UnicodeMessage;
  53
+
  54
+    my $nexmo = Nexmo::SMS::UnicodeMessage->new(
  55
+        server   => 'http://test.nexmo.com/sms/json',
  56
+        username => 'testuser1',
  57
+        password => 'testpasswd2',
  58
+        text     => 'This is a test',
  59
+        type     => 'unicode',
  60
+        from     => 'Test02',
  61
+        to       => '452312432',
  62
+    );
  63
+        
  64
+    my $response = $sms->send || die $sms->errstr;
  65
+    
  66
+    if ( $response->is_success ) {
  67
+        print "SMS was sent...\n";
  68
+    }
  69
+
  70
+=head1 METHODS
  71
+
  72
+=head2 new
  73
+
  74
+create a new object
  75
+
  76
+    my $message = Nexmo::SMS::UnicodeMessage->new(
  77
+        server   => 'http://test.nexmo.com/sms/json',
  78
+        username => 'testuser1',
  79
+        password => 'testpasswd2',
  80
+    );
  81
+
  82
+This method recognises these parameters:
  83
+
  84
+    text              => 'required',
  85
+    from              => 'required',
  86
+    to                => 'required',
  87
+    server            => 'required',
  88
+    username          => 'required',
  89
+    password          => 'required',
  90
+    status_report_req => 'optional',
  91
+    client_ref        => 'optional',
  92
+    network_code      => 'optional',
  93
+
  94
+=cut
  95
+
  96
+sub new {
  97
+    my ($class,%param) = @_;
  98
+    
  99
+    my $self = bless {}, $class;
  100
+    
  101
+    for my $attr ( keys %attrs ) {
  102
+        if ( exists $param{$attr} ) {
  103
+            $self->$attr( $param{$attr} );
  104
+        }
  105
+    }
  106
+    
  107
+    $self->user_agent(
  108
+        LWP::UserAgent->new(
  109
+            agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION,
  110
+        ),
  111
+    );
  112
+    
  113
+    return $self;
  114
+}
  115
+
  116
+=head2 user_agent
  117
+
  118
+Getter/setter for the user_agent attribute of the object. By default a new
  119
+object of LWP::UserAgent is used, but you can use your own class as long as it
  120
+is compatible to LWP::UserAgent.
  121
+
  122
+  $sms->user_agent( MyUserAgent->new );
  123
+  my $ua = $sms->user_agent;
  124
+
  125
+=cut
  126
+
  127
+sub user_agent {
  128
+    my ($self,$ua) = @_;
  129
+    
  130
+    $self->{__ua__} = $ua if @_ == 2;
  131
+    return $self->{__ua__};
  132
+}
  133
+
  134
+=head2 errstr
  135
+
  136
+return the "last" error as string.
  137
+
  138
+    print $sms->errstr;
  139
+
  140
+=cut
  141
+
  142
+sub errstr {
  143
+    my ($self,$message) = @_;
  144
+    
  145
+    $self->{__errstr__} = $message if @_ == 2;
  146
+    return $self->{__errstr__};
  147
+}
  148
+
  149
+=head2 send
  150
+
  151
+This actually calls the Nexmo SMS API. It returns a L<Nexmo::SMS::Response> object or
  152
+C<undef> (on failure).
  153
+
  154
+   my $sms = Nexmo::SMS::UnicodeMessage->new( ... );
  155
+   $sms->send or die $sms->errstr;
  156
+
  157
+=cut
  158
+
  159
+sub send {
  160
+    my ($self) = shift;
  161
+    
  162
+    my %optional;
  163
+    $optional{'client-ref'}        = $self->client_ref        if $self->client_ref;
  164
+    $optional{'status-report-req'} = $self->status_report_req if $self->status_report_req;
  165
+    $optional{'network-code'}      = $self->network_code      if $self->network_code;
  166
+    
  167
+    my $response = $self->user_agent->post(
  168
+        $self->server,
  169
+        {
  170
+            %optional,
  171
+            username => $self->username,
  172
+            password => $self->password,
  173
+            from     => $self->from,
  174
+            to       => $self->to,
  175
+            text     => $self->text,
  176
+            type     => $self->type,
  177
+        },
  178
+    );
  179
+    
  180
+    if ( !$response || !$response->is_success ) {
  181
+        $self->errstr("Request was not successful: " . $response->status_line);
  182
+        warn $response->content if $response;
  183
+        return;
  184
+    }
  185
+    
  186
+    my $json            = $response->content;
  187
+    my $response_object = Nexmo::SMS::Response->new( json => $json );
  188
+    
  189
+    if ( $response_object->is_error ) {
  190
+        $self->errstr( $response_object->errstr );
  191
+    }
  192
+    
  193
+    return $response_object;
  194
+}
  195
+
  196
+=head2 check_needed_params
  197
+
  198
+This method checks if all needed parameters are passed.
  199
+
  200
+  my $params_not_ok = Nexmo::SMS::UnicodeMessage->check_needed_params( ... );
  201
+  if ( $params_not_ok ) {
  202
+      print "Please check $params_not_ok";
  203
+  }
  204
+
  205
+=cut
  206
+
  207
+sub check_needed_params {
  208
+    my ($class,%params) = @_;
  209
+    
  210
+    my @params_not_ok;
  211
+    
  212
+    for my $attr ( keys %attrs ) {
  213
+        if ( $attrs{$attr} eq 'required' and !$params{$attr} ) {
  214
+            push @params_not_ok, $attr;
  215
+        }
  216
+    }
  217
+    
  218
+    return join ", ", @params_not_ok;
  219
+}
  220
+
  221
+=head1 Attributes
  222
+
  223
+These attributes are available for C<Nexmo::SMS::UnicodeMessage> objects:
  224
+
  225
+=over 4
  226
+
  227
+=item * client_ref
  228
+
  229
+=item * from
  230
+
  231
+=item * network_code
  232
+
  233
+=item * password
  234
+
  235
+=item * server
  236
+
  237
+=item * status_report_req
  238
+
  239
+=item * text
  240
+
  241
+=item * to
  242
+
  243
+=item * username
  244
+
  245
+=back
  246
+
  247
+=cut
  248
+
  249
+1;
  250
+
  251
+=head1 ACKNOWLEDGEMENTS
  252
+
  253
+
  254
+=head1 COPYRIGHT & LICENSE
  255
+
  256
+Copyright 2011 Renee Baecker.
  257
+
  258
+This program is released under the following license: artistic_2
  259
+
  260
+
  261
+=cut
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.