Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontline SMS Integration with Shujaa SMS Gateway #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Fconnection {
IntelliSmsFconnection,
NexmoFconnection,
SmssyncFconnection,
SmppFconnection]
SmppFconnection,
ShujaaSmsFconnection]

static getNonnullableConfigFields = { clazz ->
def fields = clazz.configFields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package frontlinesms2

import frontlinesms2.camel.shujaasms.*

import org.apache.camel.Exchange
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.RouteDefinition
import frontlinesms2.camel.exception.*

class ShujaaSmsFconnection extends Fconnection {
private static final String SHUJAASMS_URL = 'http://sms.shujaa.mobi/sendsms?'
static final configFields = [name:null, username:null, password:null, account:null, source:null, network:null]
static defaultValues = []
static String getShortName() { 'shujaasms' }

String username
String password
String account
String source
String network

static constraints = {
username blank:false
password blank:false
account blank:false
source blank:false
network blank:false
}

static passwords = ['password']


static mapping = {
password column: 'shujaa_password'
}

List<RouteDefinition> getRouteDefinitions() {
return new RouteBuilder() {
@Override void configure() {}
List getRouteDefinitions() {
return [from("seda:out-${ShujaaSmsFconnection.this.id}")
.onException(AuthenticationException, InvalidApiIdException, InsufficientCreditException)
.handled(true)
.beanRef('fconnectionService', 'handleDisconnection')
.end()
.setHeader(Fconnection.HEADER_FCONNECTION_ID, simple(ShujaaSmsFconnection.this.id.toString()))
.process(new ShujaaSmsPreProcessor())
.setHeader(Exchange.HTTP_QUERY,
simple( 'username=${header.shujaasms.username}&' +
'password=${header.shujaasms.password}&' +
'account=${header.shujaasms.account}&' +
'source=${header.shujaasms.source}&' +
'network=${header.shujaasms.network}&' +
'destination=${header.shujaasms.dst}&' +
'message=${body}'
))
.to(SHUJAASMS_URL)
.process(new ShujaaSmsPostProcessor())
.routeId("out-internet-${ShujaaSmsFconnection.this.id}")]
}
}.routeDefinitions
}
}
11 changes: 11 additions & 0 deletions plugins/frontlinesms-core/grails-app/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1152,3 +1152,14 @@ routing.notification.no-available-route=Outgoing message(s) not sent due to your

recipientSelector.keepTyping=Keep typing...
recipientSelector.searching=Searching...

# ShujaaSMS Fconnection
shujaasms.label=Shujaa SMS Account
shujaasms.type.label=Type
shujaasms.name.label=Name
shujaasms.username.label=Username
shujaasms.password.label=Password
shujaasms.account.label=Account
shujaasms.source.label=Source
shujaasms.network.label=Network
shujaasms.description=Send and receive messages through a Shujaa SMS Gateway account
11 changes: 11 additions & 0 deletions plugins/frontlinesms-core/grails-app/i18n/messages_sw.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1130,3 +1130,14 @@ smppfconnection.fromNumber.label=From Number
smppfconnection.description=Tuma na pata risala kwenye SMSC.
smppfconnection.global.info=Utahitaji akaunti na mtandao yako.
smppfconnection.send.validator.invalid = Huwezi tengeneza muunganisho isiyo tuma wala pata risala.

# ShujaaSMS Fconnection
shujaasms.label=Akaunti ya Shujaa SMS
shujaasms.type.label=Aina
shujaasms.name.label=Jina
shujaasms.username.label=Jina la mtumuzi
shujaasms.password.label=Nywila
shujaasms.account.label=Akaunti
shujaasms.source.label=Jina au Nambari Litokapo
shujaasms.network.label=Mtandao wa kuhamisha data
shujaasms.description=Tuma na upokee risala kwa kutumia akaunti ya Shujaa SMS
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,25 @@ databaseChangeLog = {
}
}
}

changeSet(author: "geoffrey (generated)", id: "11355230052153-40") {
createTable(tableName: "shujaa_sms_fconnection") {
column(autoIncrement: "true", name: "id", type: "bigint") {
constraints(nullable: "false", primaryKey: "true", primaryKeyName: "shujaa_sms_fconnectionPK")
}

column(name: "username", type: "varchar(255)")

column(name: "shujaa_password", type: "varchar(255)")

column(name: "account", type: "varchar(255)")

column(name: "source", type: "varchar(255)")

column(name: "network", type: "varchar(255)")


}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package frontlinesms2.camel.shujaasms

import frontlinesms2.*
import frontlinesms2.camel.exception.*
import org.apache.camel.*

class ShujaaSmsPostProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
def log = { println "ShujaaSmsPostProcessor.process() : $it" }
log 'ENTRY'
log "in.body:" + exchange.in.body
byte[] bytes = exchange.in.getBody(byte[].class);
log "in.body as byte[]:" + bytes
String text = new String(bytes, "UTF-8").trim();
log "in.body as byte[] as String:" + text
log "in.body got as a string" + exchange.in.getBody(String.class)
if(text ==~ "ID:.*"){
log "message sent successfully"
}else {
if(text.endsWith(":0")){
log "The message has been accepted by the gateway for delivery."
}else if(text.endsWith(":1")){
log "The delivery to the destination address is successful."
}else if(text.endsWith(":2")){
log "The delivery to the destination address has not been successful."
throw new AuthenticationException("Shujaa SMS Gateway : The delivery to the destination address has not been successful.")
}else if(text.endsWith(":4")){
log "The message has been queued for delivery."
}else if(text.endsWith(":8")){
log "The message has been submitted to the operator SMS gateway."
}else if(text.endsWith(":16")){
log "The message has been rejected by the operator SMS gateway."
throw new AuthenticationException("Shujaa SMS Gateway : The message has been rejected by the operator SMS gateway.")
}else if(text.endsWith(":32")){
log "Intermediate notifications by the operator SMS gateway."
}else if(text.endsWith(":200")){
log "The username or email used to authenticate is not registered with the gateway."
throw new AuthenticationException("Shujaa SMS Gateway : The username or email used to authenticate is not registered with the gateway.")
}else if(text.endsWith(":201")){
log "The password supplied does not match the username."
throw new AuthenticationException("Shujaa SMS Gateway : The password supplied does not match the username.")
}else if(text.endsWith(":202")){
log "The account from which the send attempt has been made is not active, it may be in a suspended or deleted state."
throw new AuthenticationException("Shujaa SMS Gateway : The account from which the send attempt has been made is not active, it may be in a suspended or deleted state.")
}else if(text.endsWith(":203")){
log "The destination address is not a valid MSISDN or is a destination that is supported by the gateway."
throw new AuthenticationException("Shujaa SMS Gateway : The destination address is not a valid MSISDN or is a destination that is supported by the gateway.")
}else if(text.endsWith(":204")){
log "The source address is not allowed to be used by the particular account."
throw new AuthenticationException("Shujaa SMS Gateway : The source address is not allowed to be used by the particular account.")
}else if(text.endsWith(":205")){
log "Not enough or invalid parameters have been provided when submitting a request to send a message."
throw new AuthenticationException("Shujaa SMS Gateway : Not enough or invalid parameters have been provided when submitting a request to send a message.")
}else if(text.endsWith(":206")){
log "An internal problem with the gateway is preventing the delivery of the message."
throw new AuthenticationException("Shujaa SMS Gateway : An internal problem with the gateway is preventing the delivery of the message.")
}else if(text.endsWith(":207")){
log "The user has exhausted the credits allowed for bulk SMS."
throw new AuthenticationException("Shujaa SMS Gateway : The user has exhausted the credits allowed for bulk SMS.")
}else if(text.endsWith(":208")){
log "The message format is invalid, for example there is no content in the message."
throw new AuthenticationException("Shujaa SMS Gateway : The message format is invalid, for example there is no content in the message.")
}else if(text.endsWith(":209")){
log "The phone number prefix provided is not in the range handled by this gateway."
throw new AuthenticationException("Shujaa SMS Gateway : The phone number prefix provided is not in the range handled by this gateway.")
}
}
log 'EXIT'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package frontlinesms2.camel.shujaasms

import frontlinesms2.*
import org.apache.camel.*

class ShujaaSmsPreProcessor implements Processor {
public void process(Exchange x) throws Exception {
def log = { println "ShujaaSmsPreProcessor.process() : $it" }
log 'ENTRY'

// URL-encode body
def d = x.in.body
x.out.headers['frontlinesms.dispatch.id'] = d.id
x.out.body = urlEncode(d.text)

def destination = d.dst
if(destination && destination.charAt(0)=='+') destination = destination.substring(1)
set x, 'dst', destination

// Add auth details to header
log "Calculating connection ID..."
def connectionId = x.fconnectionId
log "connectionId=$connectionId"
def connection = ShujaaSmsFconnection.get(connectionId)
log "connection=$connection"
['username', 'password', 'account', 'source' , 'network'].each { set x, it, connection."$it"}

log 'EXIT'
}

private def set(Exchange x, String header, String value) {
println "PreProcessor.set() : header=$header; value=$value"
x.out.headers["shujaasms.$header"] = urlEncode(value)
}

private String urlEncode(String s) throws UnsupportedEncodingException {
println "PreProcessor.urlEncode : s=$s -> ${URLEncoder.encode(s, "UTF-8")}"
return URLEncoder.encode(s, "UTF-8");
}
}