File structure for controlling SQL information query from database

This document explain the file structure of the script file that is used to control which SQL query is executed when an incoming SMS message arrives to the system.

Introduction

The script file contains sections. Sections are separated by an empty line. There is now limit for the number of sections. When the script is consulted sections are read sequentially. The first section that matches an incoming SMS is used to create the response SMS message.

A section contains a condition and a set of SQL commands called action (Figure 1). The condition is the first line in the section. Any number of SQL commands can follow this line. If the condition matches all the SQL commands following the condition will be executed until an empty line is found. The SQL commands can return one or more response messages.

sections in the script file
Figure 1 - Sections in the script file

Condition

The condition is a single line statement. It has two parts the match target selector, and the match pattern (Figure 2). The match selector is a single letter, that determines which part of the incoming SMS message is checked with the matching pattern.

information query condition
Figure 2 - Information query condition

The match selector letter can be one of the following:

k - keyword //The first word of the SMS message
converted to uppercase
n - sender telephone number 
m - the message text 

The match pattern is a standard regular expression. Regular expressions are explain in the Regular expressions tutorial.

Some examples:

.* - matches any letter
^abc.* - matches a text starting with abc
.*abc.* - matches a text that contains abc

Action

If the condition matches the incoming SMS message, the SQL commands in the section will be executed. You can use one ore more SQL SELECT statements to query the information for the response messages and you can use SQL INSERT, SQL UPDATE or any other SQL statement to modify the database. Before the SQL commands are executed on the database, the following words are replaced in them:

$originator - is replaced to the sender telephone number of the messages
$recipient - is replaced to the telephone number that received the
message
$messagedata - is replaced to the message text
$keyword - is replaced to the keyword in the message
$senttime - is replaced to the timestamp, that represents when the
message was sent
$receivedtime - is replaced to the timestamp, that represents when the
message was received
$messagetype - is replaced to the message type (in most cases this will
be SMS:TEXT)
$id - is replaced to the unique string identifier of the message
$operator - is replaced to the name of the service provider connection
that received the message

The SQL statements will be executed in the order they follow each other. Every SQL statement must be written on a single line.

(Hint: In most cases you will only use one SQL SELECT to get the content for the response SMS message.)

Response

When SELECT SQL statements are executed, they can return one or more response messages. A response message is a row in the result set of the SQL select. A row in the result set should contain the following columns:

recipient - The 1st column in the result set (Mandatory)
messageData - The 2nd column in the result set (Mandatory)
messageType - The 3rd column in the result set (Optional)
sender - The 4th column in the result set (Optional)
operatorNames - The 5th column in the result set (Optional)

The SMS gateway will process the returned results and will send them as SMS messages.

Example

The following is an example script that matches two keywords: RED and BLUE and handles unmatched incoming messages with a default response.

C:\Program Files\Ozeki\OzekiNG - SMS Gateway\config\TestApp\sqlscript.txt
k^RED
INSERT INTO  log (sender,message) VALUES ('$sender','$messagedata');
SELECT '$sender',msg FROM autoreplymessage WHERE keyword='red'

k^BLUE
SELECT '$sender',msg FROM autoreplymessage WHERE keyword='blue'

k.*
SELECT '$sender',msg FROM autoreplymessage WHERE keyword='default'

More information