Erlang BulkSMS Module
I recently bought some BulkSMS credits for a project I am working on in Erlang so here is a simple driver for it.
It supports only what I needed, just the ability to send SMS messages, get quotes for sending and to get the credit balance. There is no support for batch sending or any of the other parts of the API. If I need them, I will add them.
So, here it is, warts and all.
It does work!
%%%------------------------------------------------------------------- %%% File : bulksms.erl %%% Author : Sean Charles <sean@objitsu.com> %%% Description : A simple driver to the BulkSMS message interface %%% : supports sending SMS messages, getting quotes and %%% : a credit balance. %%% : %%% : NOTE: send_batch is not implemented. %%% : %%% Created : 19 Apr 2010 by Sean Charles <sean@objitsu.com> %%% %%% Published under a 'do what you want with it but a credit would %%% be nice' licence. %%% %%%------------------------------------------------------------------- -module(bulksms). -compile([send_sms/2, quote_sms/2, get_credits/0]). -define(BULKSERVER,"http://www.bulksms.co.uk:5567/eapi/"). -define(MULTIPART,"application/x-www-form-urlencoded"). -define(GETCREDITS,"http://www.bulksms.co.uk:5567/eapi/user/get_credits/1/1.1"). -define(SENDSMS,"http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0"). -define(QUOTESMS,"http://www.bulksms.co.uk:5567/eapi/submission/quote_sms/2/2.0"). %% Define the routing_group setting -define(ROUTING_GROUP,2). %% These will be part of the session start request -define(USERNAME,"--your-bulksms-account-name-here--"). -define(PASSWORD,"--password-here--"). %%-------------------------------------------------------------------- %% %% Send an SMS to one or more devices. The MSISDN string is assumed to %% be a string() containing one or more comma separated numbers. %% %%-------------------------------------------------------------------- send_sms( MSISDN, Msg )-> execute( ?SENDSMS, [{message,Msg}, {msisdn,MSISDN}]). %%-------------------------------------------------------------------- %% %% @doc This takes the same parameters as the send function but %% returns a quote of the number of credits that would be required to %% send the supplied data for real. %% %%-------------------------------------------------------------------- quote_sms( MSISDN, Msg )-> execute(?QUOTESMS, [{message,Msg}, {msisdn,MSISDN}]). %%-------------------------------------------------------------------- %% %% @doc This answers the number of credits remaining on the account. %% %% @spec get_credits() -> {ok, float()} | {error, Reason} %% %%-------------------------------------------------------------------- get_credits()-> case execute(?GETCREDITS,[]) of {ok,Data}-> List = string:tokens(Data,"|"), Code = list_to_integer(lists:nth(1,List)), Desc = lists:nth(2,List), case Code of 0 -> {Credits,_} = string:to_float(lists:nth(2,List)), {ok,Credits}; _ -> {Code,Desc} end; {error,Reason}-> {error,Reason} end. %%-------------------------------------------------------------------- %% %% @doc Execute a specific API command to the BulkSMS server. %% %%-------------------------------------------------------------------- execute(URL,Params) -> POSTData = post_data(Params), io:fwrite("DATA: ~s~n",[POSTData]), Response = httpc:request( post, {URL,[],?MULTIPART,POSTData}, [], [{full_result,false}]), case Response of {ok, {200, Data}} -> {ok, Data}; {ok, {Error, Data}} -> {error, Error, Data} end. %%-------------------------------------------------------------------- %% %% @doc Converts a {k,v} list into a URL string and also adds in %% essential fields such as the username and password. %% %%-------------------------------------------------------------------- post_data(Params)-> AllParams = lists:flatten( [{username,?USERNAME}, {password,?PASSWORD}, {routing_group,?ROUTING_GROUP}, %%{test_always_succeed,1}, Params]), Data = lists:flatten( [io_lib:format("&~s=~s", [K,to_string(V)]) || {K,V} <- AllParams ]), string:sub_string(Data,2). to_string(X) when is_atom(X)-> atom_to_list(X); to_string(X) when is_integer(X)-> io_lib:format("~B",[X]); to_string(X) when is_list(X)-> X.