I don't know if any messengers support reading messages from stdin, but luckily on UNIX (and particularly Linux) there's a library or script module for everything. Examples are the library used by Pidgin (libpurple) or
Net::XMPP for Jabber/GTalk communication in Perl.