Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Last active August 29, 2015 14:10
Show Gist options
  • Save bjartwolf/85973ce24bc884487b3d to your computer and use it in GitHub Desktop.
Save bjartwolf/85973ce24bc884487b3d to your computer and use it in GitHub Desktop.
namespace FDK.SmsSender
open PSWinCom.Gateway.Client;
open Model;
open log4net.Ext.EventID;
type ISmsService =
abstract member SendSms: recieverNumber: string -> body: string -> unit
type UnknownError = { Error: string}
type SmsLogMessage = { ReceiverNumber : string;
Body: string;
MessageStatus : string;
MessageStatusText : string;
UserReference : string}
override m.ToString() = Printf.sprintf "%A" m
exception SmsException of string * MessageResult
type SmsService(url: string, username: string, password: string, sender:string, logger: IEventIDLog) =
// https://wiki.pswin.com/Gateway%20XML%20API.ashx#Delivery_report_states_12
// Only DELIVRD should be considered positive delivery according to docs
// But is seems OK with returning OK too
// Otherwise we should throw exception and let NServiceBus deal with it
let (|Success|Fail|) (result: MessageResult) =
match result.Status with
| "DELIVRD" -> Success
| "OK" -> Success
| _ -> Fail
let _url = url
let _logger = logger
let _username = username
let _password = password
let _sender = sender
let logresponse (receiverNumber:string) (body: string) (result:MessageResult) (eventid: int) (logger: int*string -> unit) =
let logmsg = { ReceiverNumber = receiverNumber;
Body = body;
MessageStatus = result.Status;
MessageStatusText = result.StatusText;
UserReference = result.UserReference}
logger(eventid, logmsg.ToString())
interface ISmsService with
member this.SendSms receiverNumber body =
let sms = new Sms(ReceiverNumber = receiverNumber, SenderNumber = _sender, Text = body );
let messages:list<Message> = [sms]
try
let response = Gateway.Client(_url, _username, _password).Send(Seq.toList messages)
// The API let us send multiple SMSes and thus return a list of results
// but we will only send one SMS at a time and NServiceBus will retry
let result = List.head (List.ofSeq response.Results)
let log = logresponse receiverNumber body result
match result with
| Success ->
log ((int)EventIDConstants.SMSService.SMSSent) _logger.Info
| Fail ->
log ((int)EventIDConstants.SMSService.SMSError) _logger.Error
raise (SmsException ("Error sending SMS", result))
with
| SmsException _ as ex -> raise ex
| ex ->
_logger.Error((int)EventIDConstants.SMSService.SMSError, ex.Message.ToString())
raise ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment