Skip to content

Instantly share code, notes, and snippets.

@bsatrom
Created June 10, 2011 19:00
Show Gist options
  • Save bsatrom/1019523 to your computer and use it in GitHub Desktop.
Save bsatrom/1019523 to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
namespace MyCompany.Workflow.Messaging.Activities
{
public class SendSMSActivity : Activity
{
public static DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(System.String),
typeof(SendSMSActivity));
public static DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(System.String),
typeof(SendSMSActivity));
[DesignerSerializationVisibilityAttribute
(DesignerSerializationVisibility.Visible)]
[BrowsableAttribute(true)]
[DescriptionAttribute("Destination number of SMS message")]
[CategoryAttribute("SendSMSActivity Number Property")]
public string Number
{
get
{
return ((string)(base.GetValue(SendSMSActivity.NumberProperty)));
}
set
{
base.SetValue(SendSMSActivity.NumberProperty, value);
}
}
[DesignerSerializationVisibilityAttribute
(DesignerSerializationVisibility.Visible)]
[BrowsableAttribute(true)]
[DescriptionAttribute("Text of SMS message")]
[CategoryAttribute("SendSMSActivity Message Property")]
public string Message
{
get
{
return ((string)(base.GetValue(SendSMSActivity.MessageProperty)));
}
set
{
base.SetValue(SendSMSActivity.MessageProperty, value);
}
}
protected override ActivityExecutionStatus
Execute(ActivityExecutionContext context)
{
string response;
try
{
// Send the SMS Message
response = SendSMS(Number, Message);
}
catch (Exception e)
{
response = e.Message;
}
// Raise the PageFinished event back to the host
messageSentEvent(null, new MessageSentEventArgs(response));
// Notify the runtime that the activity has finished
return ActivityExecutionStatus.Closed;
}
public delegate void MessageSentEventHandler(object sender,
MessageSentEventArgs e);
private event MessageSentEventHandler messageSentEvent;
public event MessageSentEventHandler MessageSent
{
add
{
messageSentEvent += value;
}
remove
{
messageSentEvent -= value;
}
}
public string SendSMS(string number, string text)
{
WebClient apiRequest = new WebClient();
apiRequest.Credentials = CredentialCache.DefaultCredentials;
//Add a user agent header in case the requested URI contains a query
apiRequest.Headers.Add("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727;)");
//Add the SMS details to the apiRequest object
apiRequest.QueryString.Add("user", "xxxxx");
apiRequest.QueryString.Add("password", "xxxxx");
apiRequest.QueryString.Add("api_id", "xxxxx");
apiRequest.QueryString.Add("to", number);
apiRequest.QueryString.Add("text", text);
string baseURI = "http://api.clickatell.com/http/sendmsg";
Stream responseStream = apiRequest.OpenRead(baseURI);
StreamReader reader = new StreamReader(responseStream);
string responseCode = reader.ReadToEnd();
//Clean up in-memory objects
reader.Close();
responseStream.Close();
return (responseCode);
}
}
public class MessageSentEventArgs
{
private string response;
public string Response
{
get { return response; }
}
public MessageSentEventArgs(string response)
{
this.response = response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment