How to send SMS from TCL/TK using HTTP request

Download: tcl-tk-sms-api.tcl (1,9 Mb)

In this guide you will learn how to send SMS messages from TCL using HTTP request with the help of Ozeki NG SMS Gateway. You will also find a source code example and an application guide as well.

Introduction

The most simple but very effective method to forward data from one application to another is HTTP. TCL is an ideal mean to send SMS messages from the PC to mobile phones as it is able to calls to submit HTTP requests. You can introduce SMS technology into your IT system if you download and install a powerful SMS gateway such as Ozeki NG SMS Gateway. With this software product you can operate with SMS messages with the use of HTTP GET or HTTP POST method calls (Figure 1).

how to send sms message from tcl through http
Figure 1 - How to send SMS messages from TCL through HTTP.

After you have installed the SMS gateway, SMS sending will work as follows: to be able to operate with SMS messages, you need to forward your messages to the SMS Gateway. The built-in webserver of the gateway provides an HTTP SMS API that allows to submit messages. After the messages arrived at the SMS gateway they will be forwarded to the mobile network either with a GSM modem attached to the computer with a data cable, or the gateway directly connects to the SMS center of the mobile service provider over the Internet.

Prerequisites

To setup and operate this solution properly you need the follows:

Configuration

To send SMS messages from TCL using HTTP requests, first you need to download, install and configure Ozeki NG SMS Gateway software to your computer. Then import the source code provided below into a new project you write in TCL. After you imported it you can configure the code in the following way: In "host" variable replace 127.0.0.1. to the IP address on which Ozeki NG SMS Gateway has been installed. (In our example it is on the local computer that is why its IP address is 127.0.0.1. Replace "user_name" and "user_password" to the values with which you log into Ozeki NG SMS Gateway. Then replace the recipient to the intended recipient. In the message body you can type your message. Next, the example will piece together the HTTP request and then query it. Finally, it will verify whether the SMS sending is successful. In case of success you will read: "Message successfully sent", while if there was a problem the following message appears: "Message not sent! Please check your settings". Please note that you need to customize the values in the source code below.

Source code sample

Feel free to use and modify this source code sample!


#!/usr/bin/tclsh
package require http

###############################################
##   Ozeki NG - SMS Gateway TCL example     ##
###############################################

###############################################
##       Functions for url - encoding       ###
###############################################

proc init {} {
variable map
variable alphanumeric a-zA-Z0-9
for {set i 0} {$i <= 256} {incr i} {
set c [format %c $i]
if {![string match \[$alphanumeric\] $c]} {
set map($c) %[format %.2x $i]
}
}
array set map { " " + \n %0d%0a }
}

proc url-encode {string} {
variable map
variable alphanumeric
regsub -all \[^$alphanumeric\] $string {$map(&)} string
regsub -all {[][{})\\]\)} $string {\\&} string
return [subst -nocommand $string]
}

init

###############################################
###            Ozeki NG informations        ###
###############################################

set host "127.0.0.1"
set port "9501"
set username [url-encode "admin"]
set password [url-encode "abc123"]
set recipient [url-encode "+00123456"]
set message [url-encode "Test Message from Tcl"]

###############################################
### Putting together the final HTTP Request ###
###############################################

set url "http://$host:$port/api?action=sendmessage&username=$username
&password=$password&recipient=$recipient&messagetype=SMS:TEXT&messagedata=$message"

################################################
####            Sending the message          ###
################################################

set http [::http::geturl $url]

################################################
###        Verifying the response            ###
################################################

upvar #0 $http state

if {$state(http) == "HTTP/1.1 200 OK"} {
puts "Message successfully sent\n"
} else {
puts "Message not sent! Please check your settings!\n"
}

exit

More information