If you click on create new application, it asks you for some basic information, some of which you can edit later. Fill in all the details and click on create your Twitter application. I have named my application TwitterWorker and added it.
Now this is a fairly simple and known side, simply start Visual Studio and install Azure SDK, if you have it already installed then you will see cloud project templates. Select it and create Cloud Services project type.
Let’s name our project as Feed.Autotweet and add a worker role to it named Feed.Twitter.
Before going ahead, let me declare some nuget packages and assemblies we need to include in our worker role class library project.
- TweetSharp
- System.Configuration.dll
- System.ServiceModel.dll
Once you are done with initial setup, open up app.config file and add below entries in app settings.
- <appSettings>
- <add key="ConsumerKey" value="xxxx" />
- <add key="ConsumerSecret" value="xxxx" />
- <add key="AccessToken" value="xxx-xxxx" />
- <add key="TokenSecret" value="xxxx" />
- </appSettings>
Initialize keys with correct values which we copies from twitter portal while registering the application. And source code is as below, it’s quite self-explanatory and I have added comments wherever necessary.
- public class WorkerRole : RoleEntryPoint
- {
- private static List<string> existingArticles = new List<string>();
- private static string consumerKey = string.Empty;
- private static string consumerSecret = string.Empty;
- private static string accessToken = string.Empty;
- private static string tokenSecret = string.Empty;
-
- private static string tweetText = "{0}-{1}";
- public override void Run()
- {
- Trace.TraceInformation("Feed.Twitter is running");
-
- while (true)
- {
-
- XmlReader reader = new MyXmlReader("http://www.c-sharpcorner.com/rss/latestcontentall.aspx");
- SyndicationFeed feed = SyndicationFeed.Load(reader);
- Rss20FeedFormatter rssFormatter = feed.GetRss20Formatter();
- XmlTextWriter rssWriter = new XmlTextWriter("rss.xml", Encoding.UTF8);
- rssWriter.Formatting = Formatting.Indented;
- rssFormatter.WriteTo(rssWriter);
- rssWriter.Close();
-
- foreach (var feedItem in feed.Items)
- {
-
-
- if (!existingArticles.Contains(feedItem.Title.Text.ToLower().Trim()))
- {
-
- consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
- consumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
- accessToken = ConfigurationManager.AppSettings["AccessToken"];
- tokenSecret = ConfigurationManager.AppSettings["TokenSecret"];
-
-
- var service = new TwitterService(consumerKey, consumerSecret);
- service.AuthenticateWith(accessToken, tokenSecret);
-
- SendTweetOptions sendTweet = new SendTweetOptions();
- string tweetUrl = feedItem.Links[0].Uri.ToString();
- string tweet = string.Format(tweetText, feedItem.Title.Text, tweetUrl);
- string tweetTitle = feedItem.Title.Text;
-
-
- if (tweet.Length > 140)
- {
- tweetTitle = feedItem.Title.Text.Substring(0, 50) + "..";
- }
-
-
- tweet = string.Format(tweetText, tweetTitle, tweetUrl);
- sendTweet.Status = tweet;
-
-
- service.SendTweet(sendTweet);
-
-
- existingArticles.Add(feedItem.Title.Text.ToLower().Trim());
- }
- }
-
-
- TimeSpan ts = new TimeSpan(1, 0, 0);
- Thread.Sleep(ts);
- }
-
- }
Class MyXmlReader.cs has been referred from this link. For demo purposes, we are polling C# Corner RSS Feed every 1 hour, you can keep this setting configurable and implement it for any other feed. This article is not focusing on creating and deploying Azure cloud services but it’s quite straightforward. You can take a look at detailed documentation here.
As of writing this, this is deployed on Azure and you can see all tweets on my twitter account.
Such tools might come in handy for Marketing managers, content publishers who need to keep their twitter handles up-to date with all the latest published content on their web sites e.g. news or organization achievements etc.
There is a huge scope to betterment of the source above and you can extend it to post article links with article owner's Twitter handle, posting tweets with media in the article etc and who knows -- it might be useful for our C# Corner Twitter handle manager to seamlessly post about new articles on this site.
Thanks for reading this, feel free to post your views or comments.