Some fun with the BizTalk Services ISB
I wrote a slightly Twitter-inspired, fun app over the weekend that's using the BizTalk Services Connectivity service and relay. In the spirit of Software+Services I'm going to give you half of it [for now] ;-) You must have the BizTalk Services SDK installed to run the sample.
The server app, which I'm keeping to myself for the next few days as part of the experiment, is an extension (add-in) to Windows Live Messenger. The Messenger add-in monitors all chats with tweetiebot@hotmail.com and keeps circular buffer with the last 40 incoming messages. Using the client (which is in the attached archive), you can get a list of "Tweets" and add a new one (same as chatting)
[ServiceContract(Name = "TweetieBot", Namespace = http://samples.vasters.com/2007/05/tweetiebot)]
public interface ITweetieBot
{
[OperationContract]
IList<Tweet> GetTweets(DateTime? since);
[OperationContract]
void Tweet(string nickname, string text);
}
or you can subscribe to new tweets and get them as they arrive
[ServiceContract(Name = "TweetieEvents", Namespace = http://samples.vasters.com/2007/05/tweetiebot)]
public interface ITweetieEvents
{
[OperationContract(IsOneWay=true)]
void OnTweet(Tweet tweet);
}
The client application hooks up to the client (that lives right on my desktop machine) through the BizTalk Services ISB and the server fires events back through the ISB relay into the client as new tweets arrive. So when you run the attached client app, you'll find that it starts with a dump of the current log of the bot and then keeps spitting out events as they arrive.
The client is actually pretty simple. The EventsClient is the subscriber for the pub/sub service (ConnectionMode.RelayMulticast) that writes out the received events to the console. The rest all happens in Main (parsing an validating the command line argument) and in Run.
class Program
{
class EventsClient : ITweetieEvents
{
public void OnTweet(Tweet tweet)
{
Console.WriteLine("[{0}] {1}:{2}", tweet.Time, tweet.User, tweet.Text);
}
}
static void Main(string[] args)
{
string usageMessage = "Usage: IMBotClient <messenger-email-address>";
if (args.Length == 0)
{
Console.WriteLine(usageMessage);
}
else
{
if (!Regex.IsMatch(args[0], @"^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$"))
{
Console.WriteLine(usageMessage);
Console.WriteLine("'{0}' is not a valid email address");
}
else
{
Run(args[0]);
}
}
}
private static void Run(string emailAddress)
{
EndpointAddress serviceAddress =
new EndpointAddress(String.Format(String.Format("sb://{0}/services/tweetiebot/{1}/service",
RelayBinding.DefaultRelayHostName, Uri.EscapeDataString(emailAddress))));
EndpointAddress eventsAddress =
new EndpointAddress(String.Format(String.Format("sb://{0}/services/tweetiebot/{1}/events",
RelayBinding.DefaultRelayHostName, Uri.EscapeDataString(emailAddress))));
The URI scheme for services that hook into the ISB is "sb:" and the default address of the relay is encoded in the SDK assemblies. We set up two endpoints here. One for the client channel to fetch the initial list and one for the event subscriber.
RelayBinding relayBinding = new RelayBinding();
ServiceHost eventsHost = new ServiceHost(typeof(EventsClient));
RelayBinding eventBinding = new RelayBinding(RelayConnectionMode.RelayedMulticast);
eventsHost.AddServiceEndpoint(typeof(ITweetieEvents), eventBinding, eventsAddress.ToString());
eventsHost.Open();
ChannelFactory<TweetieBotChannel> channelFactory = new ChannelFactory<TweetieBotChannel>(relayBinding, serviceAddress);
TweetieBotChannel channel = channelFactory.CreateChannel();
channel.Open();
The two *.Open() calls will each prompt for a CardSpace authentication, so you will have to be registered to run the sample. Once you have opened the channels (and my service is running), you'll be able to pull the list of current tweets. Meanwhile, whenever a new event pops up, the EventsClient above will write out a new line.
IList<Tweet> tweets = channel.GetTweets(lastTime);
foreach (Tweet tweet in tweets)
{
Console.WriteLine("[{0}] {1}:{2}", tweet.Time, tweet.User, tweet.Text);
}
Console.WriteLine("Press ENTER to quit at any time");
Console.ReadLine();
eventsHost.Close();
channel.Close();
channelFactory.Close();
}
So when you run the app, you can chat (anyone can, you don't need to be a buddy) tweetiebot@hotmail.com through Live Messenger and you'll see your chat lines (and potentially others') popping out as events from the service bus.
To run the sample with my bot, you need to call the client with "IMBotClient tweetiebot@hotmail.com" and select your BizTalk Services Information Card twice as you are prompted.
Privacy notice: I'm anonymizing the name of the contact only insofar as I'm clipping anything including and following the "at" sign of the user that chats the bot. So whatever you say is published as "emailname: text line"
IMBotClient.zip (3.61 KB)