Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Blair 466 posts 731 karma points
    May 30, 2010 @ 04:17
    Paul Blair
    0

    Keeping tweets short

    In order to try and keep the generated tweets below the 140 char limit I have made some modifications to the prepareMessage function.

    The general gist of the code is:

    • if generated message is <140 then carry on as normal
    • if the message with a full URL is <120 characters then we will truncate the title and append "..."
    • If that is still too long then sue a short URL using the node id

    (this code assumes directory URL's - a small modification is required if not used.)

                if (template.Replace("#title#", title).Replace("#url#", url).Length < 140)
                {
                    template = template.Replace("#title#", title).Replace("#url#", url);
                }
                else if (template.Replace("#url#", url).Length < 120)
                {
                    //allow full URL & truncated title - less than 120 to leave enough space for meaningful title
                    template = template.Replace("#url#", url).Replace("#title#", title.Substring(0, 135 - template.Replace("#url#", url).Length) + "...");
                }
                else
                {
                    // otherwise url will be http:example.com/1234  (assumes directory URL's)
                    Uri u = HttpContext.Current.Request.Url;
                    String baseUrl = u.Scheme + "://" + u.Host + "/";
                    template = template.Replace("#url#", baseUrl + d.Id.ToString()).Replace("#title#", title.Substring(0, 135 - template.Replace("#url#", url).Length) + "...");
                }
  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    Jun 02, 2010 @ 13:36
    Darren Ferguson
    0

    Hi Paul,

    Thanks for this.

    I can give you write access to SVN if you'd like to commit these changes?

Please Sign in or register to post replies

Write your reply to:

Draft