Sounds funny, but since IBM i is the most advanced system in the world, it seems that we should be able to send updates to our Twitter account from within a CL program. For example, why not create a Twitter account for a specific i system box and then forward all the alerts from that box to the Twitter account? Well, you can.
Several years ago I wrote a service program that helped make SOCKETS programming easier for RPG IV programmers. It contains a lot of helper functions that take the complexity out of SOCKETS programming for RPG IV. iSockets allows you to do just that; rather than follow the strict SOCKETS protocol and logic, it allows you to write cool new applications, such as this week's SNDTWEET command. A lot of people have downloaded this service program, which I named iSockets (isockets.net), for free since it was first posted on the web. What do I get by giving it away? The same as you; I get to use it in my own code, and in today's article, in example code that does other cool stuff that won't cost your company a cent to implement.
The cool thing I did a few months ago was implement a SNDTWEET (Update my Twitter Status) CL command, and GETWITTER (Get My Twitter Status) APIs. These APIs do just what you think: post to Twitter and retrieve the latest status. I've been using them for a month on my own RPGWorld.com website (my Twitter status is posted along with another RPGWORLD Twitter status) and they work pretty well--better than the TWITTER.com servers I should say, but that's not saying too much (okay, that's a bit inside baseball, so I'll just go on).
Twitter.com is a microblogging website that asks one simple question: "What are you doing?" You can post up to 140 characters per entry. This limit was based on the SMS Text Messaging limit, which is another way to post entries to Twitter. Below is a copy of my own Twitter profile and its most recent entry (at the time of this writing).

Once you have posted an entry, the world can see your post in any of several ways. The most common is to "follow" someone on Twitter. This requires that you have a Twitter account of your own, and you can follow whomever you want. Note that I have about 54 followers, which is pathetic by any measure; but folks like Barack Obama and Oprah have several hundred thousand followers.
When you follow someone, you basically subscribe to their entries (called "feed"), and whenever you sign onto Twitter, you'll see not only your own entries but those of your subscriptions. In addition, you can selectively have someone's feed sent to your cell phone as a text message. Which is where my evil plan unfolds.
Anyone can sign up for Twitter--which means anything can signed up for a twitter account. Believe it or not, there are several pets (yes, cats and dogs) that have Twitter accounts, and even the Mars Exploration Program rovers had Twitter accounts. So why not a i system? Why not your system?
To illustrate, I went through the signup steps on Twitter.com and created a user account named Systemi (yes, there are no spaces in user names). You can follow it on Twitter by going to:
You can also search for @systemi to find posts on or about this user.
The at sign (yes, I know...) is how Twitter identifies user profiles in its own messages. So if you see something like @bobcozzi on a business card or email, they're referring to their Twitter user ID in hopes that you'll follow them.
So I've set up @systemi on Twitter and entered its first post, illustrated in the screen shot below:

Since this is a brand new account, no one is following it and it is following no one. We don't much care about following others, since this is a machine.
I've tweeted Systemi's first post ("Tweet" is the term for posting entries to Twitter), and now I want to set up a CL command that will allow me to post entries to the account from the system itself. To do this, you'll need to install the free iSOCKETS service program (iSockets.net). Once you've done that, you need to write a Twitter client that follows the Twitter protocol. Fortunately, I've done that for you and here it is:
It sends an Update to the Twitter.com Service for the given user.
USER--The Twitter User ID. Specify the user ID you created when you signed up for Twitter. This name is normally specified in all uppercase, however it is case insensitive.
PWD--The Twitter User Password. Specify the password (case sensitive) for the Twitter User specified on the USER parameter. Note that it is case sensitive so the CASE(*MIXED) keyword is specified which avoids auto-uppercase conversion of unquoted values for this parameter; in other words, it leaves it as you type it.
MSG--Status Update. Specify up to 140 characters (the Twitter limit) for this update.
The source code for the SNDTWEET CL Command follows:
SNDTWEET: CMD PROMPT('Update Your Twitter Status')
PARM KWD(USER) TYPE(*CHAR) LEN(32) MIN(1) +
PROMPT('Twitter User ID')
PARM KWD(PWD) TYPE(*CHAR) LEN(32) MIN(1) +
CASE(*MIXED) DSPINPUT(*NO) +
PROMPT('Twitter Password')
PARM KWD(MSG) TYPE(*CHAR) LEN(140) MIN(1) +
CASE(*MIXED) PROMPT('What are you doing?')
The command processing program for SNDTWEET doesn't have too much to do. Thanks to it using the free iSOCKETS service program it can focus on following the Twitter authentication and posting logic rather than screwing around with socket programming details.
The program accepts the three input parameters (User ID, Password, and Status) and then creates what Twitter calls an Authentication header. This is created using iSockets' MAKEBASE64 and SETURLAUTH subprocedures. Note that the Authentication must be in ASCII, so we first convert the plain text to ASCII and then convert the ASCII to Base64 before passing it to SETURLAUTH.
Twitter has a fairly complete API set that it seems to change on occasion--mostly to tighten up some loosely provided interfaces, but to update the current status of a user, the following command must be issued:
http://www.twitter.com/statuses/update.format
Where format is the format of the Twitter response. You have a number of choices, but I normally use XML since RPG IV has XML-INTO, which makes it easy to parse the response. So for our purposes http://www.twitter.com/statuses/update.xml would be specified.
The Statuses Update command requires one parameter: status=text where status= is required and text is the update text itself. We pass that as follows:
status=This is my twitter update. Hope you like it.
To send this command to Twitter, I use the POSTURLDATA subprocedure from iSockets. There is also a GETURLDATA subprocedure that some Twitter APIs, such as Get Current Status requires.
Here is the source code for the SNDTWEET RPG IV program:
H BNDDIR('ISOCKETS/ISOCKETS')
H DFTACTGRP(*NO) OPTION(*NODEBUGIO:*SRCSTMT)
********************************************
** © 2009 by Robert Cozzi, Jr.
** All rights reserved. Permission to use
** with i5/OS or IBM "i" operating systems
** is granted provided no fee is paid.
** When referring to this program always
** credit the program author.
********************************************
D updTwitter PR EXTPGM('UPDTWITTER')
D user 32A Const
D pwd 32A Const
D msg 140A Const
D updTwitter PI
D user 32A Const
D pwd 32A Const
D msg 140A Const
/include isockets/qcpysrc,isockets
/include isockets/qcpysrc,cprotos
D rtnLen S 10I 0
D rtnBuffer S 1024A
D msgUTF S 1024A
D status S 1024A Varying
D twitter DS Qualified
D url 255A Varying Inz('http://twitter.com')
D file 255A Varying Inz('/statuses/update.xml')
D auth S 64A
D auth4 S 4A
D auth64 S 128A Varying
D len S 10I 0
D cLen S 10I 0
D i S 10I 0
C MOVE *ON *INLR
/free
// Please remember!
// Twitter passwords are CaSe sENsitivE!
auth = %TrimR(user) + ':' + %TrimR(pwd);
len = %len(%TrimR(auth));
cvtToAscii(auth: len);
for i = 1 to len by 3;
if (i+3 > len);
cLen = (3 - ((i+3) - len)) + 1;
else;
cLen = 3;
endif;
makeBase64(%subst(auth:i:3):auth4: cLen);
auth64 += auth4;
endfor;
setUrlAuth('Authorization': 'Basic' :auth64 );
status = 'status=' + %trimR(msg);
rtnLen = postUrlData(twitter.url: twitter.file : status :
%addr(rtnBuffer) : %size(rtnBuffer): 0 : 819);
return;
/end-free
With the SNDTWEET CL command, you can now use Twitter to post not only your own status, but that of your i system, itself. When a job has an issue and you're away and want to know about it, just use SNDTWEET to post the message (maybe from QSYSOPR?) to Twitter and either "follow" that user ID or better yet, have its updates sent to your cell phone as SMS text messages.
I've created a Twitter account for my system that I named "Systemi." To follow its progress go to twitter.com/systemi. It should be posting updates periodically once I decide what I want to share about it. In the meantime, you can follow me, Bob Cozzi, on Twitter by going to Twitter.com/BobCozzi and clicking on "Follow."
If you visit my website RPGWorld.com and click on the free Discussion forum link, you'll see my Twitter status posted. I use the complement of this program that I wrote to retrieve the current Twitter status. I store the user profile and passwords in a database file so that the password is not plainly visible on a web page. I'll cover that program in the next issue of RPG Coder.
Bob Cozzi is the author of Subprocedures and Service Programs on DVD: a three-disc training series that gives RPG IV programmers an easy way to learn how to get started with subprocedures and of course service programs. It is available now at RPGWorld.com/DVD. His website, RPGWorld.com, welcomes thousands of System i RPG IV developers from all over the world to share ideas and help each other with technical issues. RPGWorld.com is also the place to watch iWeekly, Bob's live show for the System i community, every Friday at noon eastern time in the U.S. You also can watch past episodes anytime on the RPG World channel on USTREAM at: ustream.tv/channel/rpgworld.