EMG and custom outbound http requests using EBE

Implementing a custom connector using the DLL approach will give better performance than the approach described below. This variant might be easier, though.

HTTP connectors in EMG uses a default format where MGP options are used as key-value pairs in a HTTP POST or GET request. The format is well defined and we provide a sample HTTP API description.

However, sometimes it is necessary to integrate with service providers that require a different format for their HTTP apis. In those cases it is fairly simple to put together a perl script which can be configured as a EBE connector in EMG. Any type of http request (get or post) can be created this way with data formatted as key-value, xml, json etc.

Sample code

Sample script for formatting and sending a custom HTTP request in perl.

#!/opt/perl-5.12.2-emg/bin/perl
 
use Encode;
use LWP;
use POSIX qw(strftime);
use URI::Escape;
 
my $ua = LWP::UserAgent->new;
my $username = 'user';
my $password = 'secret';
my $logfile = "/tmp/ebe-http-custom.log";
my $options;
 
# Log with leading timestamp and process id
sub dolog {
  my ($str) = @_;
  my $ts = strftime( "%Y-%m-%d %H:%M:%S", localtime );
  open( OUT, ">>$logfile" );
  print OUT "$ts [$$] $str";
  close(OUT);
}
 
while(<STDIN>) {
  chomp;
  my($key, $value) = split(/\t/);
  $options->{$key} = $value;
  dolog("key=$key value=$value\n");
}
 
my $id = $options->{1} || -1;
my $destaddr = $options->{8} || '';
my $msg = $options->{16} || '';
my $charcode = $options->{28} || '';
 
# Check whether message is Unicode (UCS2)
if ( $charcode eq '4' ) {
  # Yes, try to convert it
  my $decodedHex = pack( 'H*', $msg );
  eval { $msg = decode( "UCS-2BE", $decodedHex, Encode::FB_QUIET ); };
  if($@) {
    dolog("Could not decode message, $@");
    $msg = "?";
  }
} else {
  # No, hex decode plain text message
  $msg =~ s/(..)/pack("C", hex($1))/eg;
}
 
# Escape data
my $msgdata = uri_escape("$msg");
my $url = "http://api.example.com/sms?user=$username&pass=$password&linesep=0&testmode=0&recipient=$destaddr&message=$msgdata";
dolog("url=$url\n");
 
# Submit HTTP GET request
my $resp = $ua->get( $url );
 
unless($resp->is_success) {
  dolog("Request failed: " . $resp->status_line . "\n");
  exit 1;
}
 
exit 0;

Make sure the script is executable:

chmod u+x ebe-http-custom.pl

You should now be able to execute the file from the command-line using

./ebe-http-custom.pl

Since the script reads from standard input it should simply hang until you break it with Ctrl-C. It should not give any error messages.

If you have created the file on a Windows machine you may get Windows line endings in the file (CR+LF). If so, open the file in binary mode with

vi -b ebe-http-custom.pl

and remove any garbage characters.

Sample server.cfg entry

Corresponding connector configuration in server.cfg:

CONNECTOR ebe-http-custom <
TYPE=OUTGOING
PROTOCOL=EBE
ADDRESS=/home/emg/etc/ebe-http-custom.pl
INSTANCES=1
>