Monday 19 October 2015

A quick bit of twitter integration.

OK – so it looks like I’m not going to replace Facebook or Twitter with my social networking site I’m Never Going Back There.   After a full day in production there’s only been one comment.

So – if you can’t beat them, join them.  Time for a bit of integration.

[more]

So – if I can post entries to twitter with a link to the site and get people to follow the twitter account then this should give me lots of links which means lots of Google ranking.

I also want to be able to get people to authenticate with there twitter account and be able to post entries to twitter.

But, this is a spare time project!  So – first a bit of twitter integration.

So I had a quick dig around on the twitter developers pages.  Seems their API is in some flux between REST and streaming API’s.  I decided I didn’t want to get involved in any low level stuff so picked Twitterizer from the list of twitter integration libraries.

Posting a tweet

My first goal was to create a twitter entry for each comment.

The Code

I downloaded the DLL’s and put them in a new folder in our Framework classes – where we reference shared components such as the MS Enterprise blocks.

I added references to them from my shiny business layer created when I ditched Entity Framework.

I registered an new twitter user @imnevergoing and registered my application using this account.  I then downloaded my two application keys (Consumer Key and Consumer Secret) and the two OAuth access tokens. 

The first two tokens authenticate my application.  The second two allow me to authenticate @imnevergoing without directing a user to authenticate on the twitter site.  The posts will always posted as @imnevergoing.

The code is pretty simple.  I form my authentication key as instructed in the twitter documentation here and for a tiny URL back to my site using the code I borrowed from “Shortening URLs Using TinyUrl API in .net” .

I then simply form my post text and post the comment!

 

    public static bool TweetComment(Comment c)
    {
        bool ret = false;

        try
        {
            OAuthTokens tokens = new OAuthTokens();
            tokens.ConsumerKey = ConfigurationSettings.AppSettings["consumerKey"];
            tokens.ConsumerSecret = ConfigurationSettings.AppSettings["consumerSecret"];
            tokens.AccessToken = ConfigurationSettings.AppSettings["oauth_token"];
            tokens.AccessTokenSecret = ConfigurationSettings.AppSettings["oauth_token_secret"];

            String url = MakeTinyUrl("http://www.imnevergoingbackthere.com/");
            StringBuilder sb = new StringBuilder();
            sb.Append(c.TheVenue.Name);
            sb.Append(" ");
            sb.Append(c.TheVenue.Town);
            sb.Append(" ");
            sb.Append(c.CommentText);

            String tweet = sb.ToString(0, Math.Min(sb.Length, 140 - (url.Length + 1)));
            tweet = String.Format("{0}-{1}", tweet, url);

            TwitterResponse<TwitterStatus> resp = TwitterStatus.Update(tokens, tweet);
            if (resp.Result == RequestResult.Success)
            {
                // update teh comment
            }
        }
        catch
        {
            // log the error
        }

        return ret;
    }
    public static string MakeTinyUrl(string Url)
    {
        try
        {
            if (Url.Length <= 30)
            {
                return Url;
            }
            if (!Url.ToLower().StartsWith("http") && !Url.ToLower().StartsWith("ftp"))
            {
                Url = "
http://" + Url;
            }
            var request = WebRequest.Create("
http://tinyurl.com/api-create.php?url=" + Url);
            var res = request.GetResponse();
            string text;
            using (var reader = new StreamReader(res.GetResponseStream()))
            {
                text = reader.ReadToEnd();
            }
            return text;
        }
        catch (Exception)
        {
            return Url;
        }
    }
}

I then call this after saving the comment to the database, and, since it’s in my business process layer, this will happen whatever the client (web service, web page …).

[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
       public static int InsertComment(Comment comment)
       {
           int ret = 0;
           ret = DBComment.InsertComment(comment);
           TweetComment(comment);
           return ret;
       }

The Tao

Thus spake the master programmer:

``Though a program be but three lines long, someday it will have to be maintained.''

The Tao Of Programming

This may look fine but its not particularly great as

  1. It’s done synchronously as part of the web request.  This could take  a significant amount of time.
  2. If it fails it will never be tweeted.
  3. I would like a link the actual comment as more meaningful and probably better for SEO– but I don’t have the view yet!

Much better plan is to extend the object/database to indicate if the comment has been tweeted and then on each comment add and website start-up kick off a thread to retrieve all the un-tweeted comments and tweet them.

I’ll cover this in a separate blog.

Follow Me

This should be pretty easy -  a simple button from the twitter goodies site in the master page should do the trick.

Tweet this.

Ideally I’d like to be able to tweet each individual comment – but I haven’t got the view or time for this yet.

So I settle for two buttons on the master page – a tweet this button and an “add this” button – which covers a lot of other social networking sites.

The twitter button can be built from twitter’s goodies and the “Add this” from Add This"!

No comments:

Post a Comment