Sending formatted IMs from a UCMA application

I can't honestly say that I've run into a lot of situations where I've said to myself, "If only I could send an HTML formatted instant message right now, my problems would go away." But adding formatting to IMs can definitely help with some specialized communication applications such as bots where you want to distinguish different types of messages, or emphasize certain parts of a conversation. So, to make those nice visual touches easier, and for anyone out there who is in a tight spot by virtue of not knowing how to send an IM with special formatting, I thought I'd write up a quick post on it. Normally, instant messages from a UCMA application are sent as plain text. This is the easy way to do it, and in code it looks something like this:

[csharp] call.Flow.BeginSendInstantMessage("Jackdaws love my big sphinx of quartz.", ar => { try { call.Flow.EndSendInstantMessage(ar); } catch (RealTimeException ex) { Console.WriteLine(ex); } }, null); [/csharp]

(If you're wondering about my sample message, it's one of those sentences that use all of the letters of the alphabet. I don't really have any tips on the decorative preferences of jackdaws.)

Run this code using an already-established IM call,and you'll get a perfectly respectable instant message like this:

Instant message without formatting

However,there is a second overload of the InstantMessagingFlow.BeginSendInstantMessage method. Here is an example of how you can use it to send an HTML-formatted instant message:

[csharp] string htmlMessage = "<html><body><span style="color: red; font-variant: small-caps;">" + "Jackdaws love my <span style="font-size: 26px;">big</span> sphinx of quartz." + "</span></body></html>"; byte[] htmlBytes = Encoding.UTF8.GetBytes(htmlMessage);

call.Flow.BeginSendInstantMessage( new System.Net.Mime.ContentType("text/html"), htmlBytes, ar => { try { call.Flow.EndSendInstantMessage(ar); } catch (RealTimeException ex) { Console.WriteLine(ex); } }, null); [/csharp]

The first parameter for this overload of BeginSendInstantMessage needs to be an instance of System.Net.Mime.ContentType; for an HTML message, you can provide the string "text/html" as the parameter for the ContentType constructor. (A normal plain-text IM would have a MIME type of "text/plain".)

The second parameter is the byte-encoded message body, which you can get by calling Encoding.UTF8.GetBytes with your HTML string.

With code like the above, you get a beautifully formatted instant message like this:

Instant message with formatting

I think this one really communicates the magnitude of the sphinx of quartz we're talking about here.

That's about all there is to it. It doesn''t seem to be necessary to wrap everything in <html> and <body> tags, but I usually do it just to feel good about myself. Also, note that you can send links, but they may get blocked depending on the policy settings in your environment.

It''s also possible to send other types of content, such as image/gif or rtf or application/x-ms-ink or multi-part/alternative. Let me know if you find an interesting way to use any of these!