Help needed with this configuration

Hello,

I am trying to configure my GoIP4 GSM Gateway with FreePBX for receiving and sending SMS through Zoiper on my mobile phone but am not very knowledgeable about FreePBX and Asterisk. I already have it configured for making and receiving calls and it works perfectly.

I found this online and I was wondering if someone can point me on how to configure FreePBX/Asterisk with the SMS settings. Please see below:


One of our more recent goals with Tangaza was to find if there was a way to deploy it cheaply and quickly, instead of having a server hosted at an ISP with a hardline E1 going to it (we call the cheap way “Tangaza in a Suitcase” in our paper). We bought a GoIP GSM-VoIP gateway ($150 for a single SIM) and connected it to a laptop. That plus a SIM card is really all you need to roll your own version of Tangaza or any other IVR/SMS of your choosing – no more ~$1000 monthly ISP+E1 charge. This is not as reliable as being hosted, but it is cheaper and should be sufficient for a research project or demo. The gateway is exactly what the name says: it speaks VoIP (SIP or H.323) to Asterisk out one side, and GSM out the other. Asterisk then runs on the laptop. An Internet connection is not required, unless you need it for your particular application.

We needed to be able to support four operations: receive a voice call, initiate a voice call, receive an SMS, and send an SMS. This shows how to do each of these steps. I use Perl as the AGI language, but many other languages choices are available.

The Asterisk version on the laptop is 10.0.0 beta. I upgraded the firmware on the GoIP gateway. The laptop and the gateway are connected via ethernet. I logged into the laptop through its wireless connection, but the configuration could be done directly at the laptop as well. The gateway supplied the laptop with a DHCP address of 192.168.8.100.

Configuring the GSM-VoIP Gateway
We register a SIP user named ‘gsm1’ with Asterisk. We forward calls (called “PSTN to VOIP” below) to Asterisk’s extension 1. We forward SMS to its extension 2. It is the SIP configuration within Asterisk that says which context to go to, but this specifies the extension. To configure the gateway, start a browser on the laptop, and point it to 192.168.8.1

Key changes within the gateway’s configuration:

Changed “network tones to US” (depends on your location)
Call Settings:

SIP Phone;
Single Server Mode
Phone number = gsm1
Display Name = gsm1
SIP Proxy = 192.168.8.100
SIP Registrar Server = 192.168.8.100
Expiry = 3600
Auth ID = gsm1
Password = gsmdemo
Call Forward Type = Not Forward
No backup server
Call Divert Settings:

CID Forward Mode = Use Remote Party ID
Forward to VoIP = Enable
Forward Number (PSTN to VoIP) = 1
SMS Mode = Relay
SMS Forward Number = 2
DTMF Mode: Outband/RFC2833
Note that you can enter a SIM unlock PIN so one could use prepaid cards which use this.

Configuring the GSM SIP account within Asterisk
The gsm1 user in sip.conf allows the gateway to register with Asterisk. The context=goip is what directs messages from the GSM-VoIP gateway to the goip context within the dialplan below.

[gsm1]
canreinvite=no
context=goip
dtmfmode=rfc2833
fromuser=gsm1
host=dynamic
disallow=all
allow=ulaw
allow=alaw
allow=g729
insecure=port,invite
secret=gsm1
type=friend
defaultname=gsm1
Obviously this is pretty insecure, so change it once you get it working.

Asterisk’s Dialplan - Receiving and Sending Calls
This is the first set of changes in the dialplan (extensions.conf). A voice call coming to the GSM line will now land here. You can put an AGI here or use Asterisk’s built in functions. We are just playing hello-world and hanging up.

[goip]
exten => 1,1,Wait(1)
exten => 1,n,Answer
exten => 1,n,Playback(hello-world)
exten => 1,n,Wait(2)
exten => 1,n,Hangup
Initiating a Call
This .call file would land us in extension goip,1 in the dialplan. Again, we could put in a different extension to act differently for incoming and outgoing calls.

Channel: SIP/gsm1/18576548539
CallerID: “Asterisk” <16177104840>
Context: goip
Extension: 1
It makes a GSM call to 18576548539, and then starts playing hello-world once the person picks up.

Receiving and Sending SMS: Dialplan Component
Receiving SMS was pretty simple. Based on the gateway’s SMS relay settings, we just land here, in context goip, extension 2. The SMS message is given to us in the MESSAGE(body) variable. It’s format is [sender]\n[message text]. Unfortunately many of the Asterisk functions do not seem to handle newlines well. I was able to assign it to other variables and use CUT, but could not figure out how to CUT on the newline. To send it to an AGI, I url encoded it first. I’ve included two other variables just to help with the example: FOO and baz (well, baz is a parameter to the AGI). The AGI (below) sets the uri encoded variable SMSOUT. We then decode it, set the MESSAGE(body) to this, and send it back out to the gateway, which sends out the new SMS. This example takes an SMS as input, modifies the content slightly, and replies to the original sender.

exten => 2,1,Set(FOO=foo)
exten => 2,n,Set(SMSINRAW=${MESSAGE(body)})
exten => 2,n,Set(SMSIN=${URIENCODE(${SMSINRAW})})
exten => 2,n,AGI(agi://127.0.0.1:4674/entry?baz=1&sms=${SMSIN})
exten => 2,n,Verbose(smsout ${SMSOUT})
exten => 2,n,Set(SMSOUTRAW=${URIDECODE(${SMSOUT})})
exten => 2,n,Verbose(smsoutraw ${SMSOUTRAW})
exten => 2,n,Set(MESSAGE(body)=${SMSOUTRAW})
exten => 2,n,MessageSend(sip:gsm1)
exten => 2,n,Hangup
Receiving and Sending SMS: Perl AGI Component
This is the perl fastagi that is sitting listening on port 4674. When Asterisk reaches the AGI line above, we jump to sub entry within GoIP.pm below.

goip.agi

#!/usr/bin/perl

use strict;
use warnings;

use GoIP;

my $server = GoIP->new ({port => 4674});
$server->run;
Here is where we land from the AGI(agi://127.0.0.1:4674/entry?baz=1&sms=${SMSIN}) call in the dialplan.

GoIP.pm

package GoIP;

use strict;
use Data::Dumper;
use URI::Escape;
use base ‘Asterisk::FastAGI’;

sub entry {
my ($self) = @_;

# Access a simple variable
my $foo = $self->agi->get_variable("FOO");
# Access one of the parameters passed to the AGI
my $baz = $self->param('baz');

# Grab the url encoded SIP message,
# which is of the form [senders number]\n[message contents]
my $smsParam = $self->param('sms');

# Alternatively we can access the same info via a parameter. 
# Note that, because it contains a newline, Asterisk does not pass
# this properly without it being url encoded: getting the variable
# without this returned null.
my $smsEncodedIn = $self->agi->get_variable("SMSIN");

# Decode it
my $smsIn = uri_unescape($smsEncodedIn);

# Divide it into the sender's number and the message contents
my ($sender, $msg) = split (/\n/, $smsIn);

# Manipulate the response
my $response = '';
if ($msg =~ /^(\d+)$/) {
  $response = $1 + 1;
} else {
  $response = $msg.'+1';
}

# Create the contents of the response message.
# Because we are just echoing back to the sender in this example,
# we use the same number, but you could put any number in as the
# destination.
my $smsOut = "$sender\n$response";

# Make it possible to send back to Asterisk
my $smsEncodedOut = uri_escape($smsOut);

# Assign it to a variable so it is available when we return from
# the AGI.
$self->agi->set_variable("SMSOUT", $smsEncodedOut);

open LOG, ">/tmp/goip-agi.log" or die ("Cannot open log");
print LOG "hello\n";
print LOG "foo $foo\n";
print LOG "smsEncodedIn $smsEncodedIn\n";
print LOG "smsIn $smsIn\n";
print LOG "smsOut $smsOut\n";
print LOG "smsEncodedOut $smsEncodedOut\n";
print LOG "baz $baz\n";
print LOG "smsParam $smsParam";
print LOG "sender $sender\n";
print LOG "msg $msg\n";
print LOG "response $response\n";

#print LOG Dumper($self);
close LOG;

}

1;
Resulting goip-agi.log

hello
foo foo
smsEncodedIn %2B18576548539%0A1234%0A
smsIn +18576548539
1234

smsOut +18576548539
1235
smsEncodedOut %2B18576548538%0A1235
baz 1
smsParam %2B18576548538%0A1234%0Asender +18576548539
msg 1234
response 1235
Note that this could probably be achieved with the CUT command within the dialplan. I couldn’t quite figure that out; cutting on the newline character did not work. And you can only cut on a single character, so you’d need to cut a few times to cut on the url encoded string.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.