1: [WorkflowPlugIn(Area = PlugInArea.None, Url = "~/WorkflowUI/TwitterWorkflowUI.ascx")]
2: public sealed partial class SendTwitterNotification : SequentialWorkflowActivity
3: { 4: //TODO: Digg, Reddit och Delicious, stumpleupon
5:
6: public string TwitterUsername { get; set; } 7: public string TwitterPassword { get; set; } 8: public string TwitterMessage { get; set; } 9: public bool TweetAlways { get; set; } 10:
11: public WorkflowPageEventArgs PageArgs { get; set; } 12:
13: public static string tinyurl = "http://tinyurl.com/api-create.php?url={0}"; 14:
15: public SendTwitterNotification()
16: { 17: InitializeComponent();
18: }
19:
20: protected void CheckIfItIsFirstPublish(object sender, ConditionalEventArgs args)
21: { 22: if (TweetAlways)
23: { 24: args.Result = true;
25: }
26: else
27: { 28: args.Result = (bool)(DataFactory.Instance.ListVersions(PageArgs.PageLink).Where(pv => pv.Status == VersionStatus.PreviouslyPublished).Count() == 0);
29: }
30: }
31:
32:
33: /// <summary>
34: /// Post an update to a Twitter acount
35: /// </summary>
36: /// <param name="username">The username of the account</param>
37: /// <param name="password">The password of the account</param>
38: /// <param name="tweet">The status to post</param>
39: public static void PostTweet(string username, string password, string tweet)
40: { 41: try
42: { 43: // encode the username/password
44: string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
45: // determine what we want to upload as a status
46: byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet); 47: // connect with the update page
48: HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml"); 49: // set the method to POST
50: request.Method = "POST";
51: request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
52: // set the authorisation levels
53: request.Headers.Add("Authorization", "Basic " + user); 54: request.ContentType = "application/x-www-form-urlencoded";
55: // set the length of the content
56: request.ContentLength = bytes.Length;
57:
58: // set up the stream
59: Stream reqStream = request.GetRequestStream();
60: // write to the stream
61: reqStream.Write(bytes, 0, bytes.Length);
62: // close the stream
63: reqStream.Close();
64: }
65: catch (Exception ex) {/* DO NOTHING */} 66: }
67:
68: private string ShortenUrl(string url)
69: { 70: WebClient wc = new WebClient();
71: return wc.DownloadString(string.Format(tinyurl, url)).Trim();
72: }
73:
74: private void sendTwitterAnnounce_ExecuteCode(object sender, EventArgs e)
75: { 76: PageData pd=DataFactory.Instance.GetPage(PageArgs.PageLink);
77: string author = (pd["Author"] as string) ?? pd.CreatedBy;
78: string title = pd.PageName;
79: UrlBuilder url = new UrlBuilder(pd.LinkURL);
80: EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, pd.PageLink, System.Text.UTF8Encoding.UTF8);
81: string linkurl = UriSupport.AbsoluteUrlBySettings(url.ToString());
82: string shorturl = ShortenUrl(linkurl);
83:
84: string msg = string.Format(TwitterMessage,author,title,shorturl,linkurl);
85: //Max 140 chars
86: if (msg.Length > 140)
87: { 88: int toomuch = msg.Length - 140;
89: if (toomuch < title.Length)
90: { 91: msg=string.Format(TwitterMessage, author, title.Substring(0,title.Length-toomuch), shorturl, linkurl);
92: }
93: }
94:
95: PostTweet(TwitterUsername, TwitterPassword, HttpUtility.UrlEncode(msg));
96: }
97:
98: }