Spambusting with Umbraco and Akismet

When I started this blog, I really didn't think I would need any comment spam prevention. I have also never liked the idea of having to moderate/approve comments manually (smells like work).

However, after about three days on the interwebs, I decided otherwise. Bring on Akismet from Automattic. From their website, "Automattic Kismet (Akismet for short) is a collaborative effort to make comment and trackback spam a non-issue and restore innocence to blogging, so you never have to worry about spam again."

Sounds good to me, let's get started!

First, you will need a Wordpress account. You can then find your API key by clicking on Profile.

Next, you will need to download Akismet for .Net 2.0 from Codeplex and drop the included assembly into your bin directory. Now, time for some code.

I like the idea of saving every comment, but only publishing those that pass the spam check. So, my btnSubmit_Click event looks something like this (sorry for all the wraps):

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Akismet api = new Akismet(ConfigurationManager.AppSettings["wordpressKey"], "", "Test/1.0");
        if (!api.VerifyKey()) throw new Exception("Key could not be verified");

        AkismetComment comment = new AkismetComment();
        comment.Blog = "";
        comment.UserIp = Request.UserAgent.ToString();
        comment.UserAgent = Request.UserAgent.ToString();
        comment.CommentContent = Server.HtmlEncode(txtComment.Text);
        comment.CommentType = "comment";
        comment.CommentAuthor = Server.HtmlEncode(txtName.Text);
        comment.CommentAuthorEmail = Server.HtmlEncode(txtEmail.Text);
        comment.CommentAuthorUrl = Server.HtmlEncode(txtWebsite.Text);

        DocumentType commentDt = DocumentType.GetByAlias("BlogPostComment");
        Document newComment = Document.MakeNew(txtName.Text, commentDt, new umbraco.BusinessLogic.User(0), Node.GetCurrent().Id);

        newComment.getProperty("name").Value = Server.HtmlEncode(txtName.Text);
        newComment.getProperty("email").Value = Server.HtmlEncode(txtEmail.Text);
        newComment.getProperty("website").Value = Server.HtmlEncode(txtWebsite.Text);
        newComment.getProperty("comment").Value = Server.HtmlEncode(txtComment.Text);

        newComment.Save();

        if (!api.CommentCheck(comment))
        {
            newComment.Publish(new umbraco.BusinessLogic.User(0));
            umbraco.library.UpdateDocumentCache(newComment.Id);
            Response.Redirect(umbraco.library.NiceUrl(Node.GetCurrent().Id) + "#comment-" + newComment.Id.ToString());
        }
        else
        {
            customError.IsValid = false;
            customError.ErrorMessage = "Your comment has been detected as spam.";
        }           
    }

I have also thought about creating a new button datatype to enable me to submit unblocked spam and Ham (false positives) back to Akismet to help improve the algorithm, but have not gotten around to it and really haven't needed to. Akismet really does a great job for lazy folks like me. Their claim of 93% accuracy is looking pretty solid.

Now all I need is for Umbraco to implement a "Delete at" feature for documents so that I can set an expiration date for all of the unpublished spam nodes that I have in my content tree.

Leave a Comment

Name  [Preferably your real name]
Email  [Not shown, for gravatar only]
Website  [Optional. Include http://]
Comment  [No markup allowed. 2000 characters max.]
 

About Me

I am a Founder and Managing Partner of Trio Technologies and Trio Merchant Services in Abilene, Texas. I have a beautiful wife and two incredible sons.

Popular

Blogroll

Bookmarks