Skip to main content

Company Instant Message RSS feed Notice Board LCS response Bot using communicator Web Access (Ajax)

Company announcement notice boards are something I’ve done a bit in the past usually these are web based the last one I did a while ago used a simple Exchange Event sink to create a simple web based notice board where information about company events could be posted to a mailbox and appear on the web page. They are also useful for posting things like network outages or as a change control list. With new technology comes new ways of doing things and seeing how I had the code for creating a LCS bot and a RSS feed it seemed natural to join them together and basically have a LCS bot that creates and RSS feed based on Instant message you send to it.

The bot part of this code work’s as per my pervious post the RSS feed part creates a simple RSS document using the MICROSOFT.XMLDOM object. Because you can’t specify a subject and body in IM there’s some code that looks for any carriage returns in the Instant message and if it finds a carriage return it takes the first section as the title for the new post. It then uses the rest of the message as the body of the post. There’s also a further bit of code that coverts any carriage returns in the post to html breaks. The RSS feed is also self managing so at the top of the script there is a variable where you can specify the maximum number of posts you want to have in the feed the script then handles removing any old posts as new ones are added.

To run this script you first need to configure the following four variables at the top of the script

SipURI = "user@domain.com"
Servername = "servername.domain.com"
userName = "domain\username"
Password = "password"

And then the following two variables need to be configured with the location you want the feed file save to and the maximum number of items you want in the feed

feedfilename = "E:\inetpub\wwwroot\LCSfeed.xml"
Maxitemnumber = 20

After you’ve configured the script all you need to do is start it from the command shell like

Cscript CBLcsrespbotv1.vbs

The script is very verbose and should return a lot of information back to the console while it’s running.

I’ve put a downloadable copy of the script here the script itself looks like

SipURI = "user@domain.com"
Servername = "servername.domain.com"
userName = "domain\username"
Password = "password"

feedfilename = "E:\inetpub\wwwroot\LCSfeed.xml"
Maxitemnumber = 20

set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
Com.ActiveConnection = Conn
strNameingContext = iAdRootDSE.Get("configurationNamingContext")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")

set objdom = CreateObject("MICROSOFT.XMLDOM")
Set fso = CreateObject("Scripting.FileSystemObject")


if fso.FileExists(feedfilename) then
objdom.async="false"
objdom.load(feedfilename)
Set xnItemNodes = objdom.getElementsByTagName("item")
Set xnChannelNodes = objdom.getElementsByTagName("channel")
wscript.echo xnItemNodes.length
if xnItemNodes.length > Maxitemnumber then
for i = Maxitemnumber to xnItemNodes.length
set parentnode = xnItemNodes(i-1).parentnode
parentnode.removechild(xnItemNodes(i-1))
next
end if
set xnChannelNode = xnChannelNodes(0)
fptrack = 0
else
' ************ Create Root XML Elemements ************************
fptrack = 1
Set objField = objDom.createElement("rss")
Set objattID = objDom.createAttribute("version")
objattID.Text = "2.0"
objField.setAttributeNode objattID
objDom.appendChild objField
Set xnChannelNode = objDom.createElement("channel")
objfield.appendChild xnChannelNode
Set objField4 = objDom.createElement("title")
objfield4.text = "Company Instant Message Notice Board"
xnChannelNode.appendChild objField4
Set objField5 = objDom.createElement("description")
objfield5.text = "Company Instant Message Notice Board"
xnChannelNode.appendChild objField5
Set objField6 = objDom.createElement("language")
objfield6.text = "en-us"
xnChannelNode.appendChild objField6
Set objField7 = objDom.createElement("lastBuildDate")
objfield7.text = WeekdayName(weekday(now),3) & ", " & day(now()) & " " &
Monthname(month(now()),3) & " " & year(now()) & " " & formatdatetime(now(),4) &
":00 GMT"
xnChannelNode.appendChild objField7
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
objDom.insertBefore objPI, objDom.childNodes(0)
' ************ Create Root XML Elemements ************************
end if

set req = createobject("microsoft.xmlhttp")
req.Open "GET", "https://" & Servername & "/iwa/logon.html?uri=" & SipURI & "&signinas=1&language=en&epid=",
False, Username, Password
req.send
reqhedrarry = split(req.GetAllResponseHeaders(), vbCrLf,-1,1)
for c = lbound(reqhedrarry) to ubound(reqhedrarry)
if instr(lcase(reqhedrarry(c)),"set-cookie:") then reqsessionID =
right(reqhedrarry(c),len(reqhedrarry(c))-12)
next
wscript.echo reqsessionID
chk = left(reqsessionID,36)
updatestr = "https://" & Servername & "/cwa/AsyncDataChannel.ashx?AckID=0&Ck=" &
chk
req.Open "GET", updatestr, False, Username, Password
req.setRequestHeader "Cookie:", reqsessionID
req.send
latupdate = mid(req.responsetext,instr(req.responsetext,"latestUpdate=")+14,instr(instr(req.responsetext,"latestUpdate=")+14,req.responsetext,chr(34))-(instr(req.responsetext,"latestUpdate=")+14))

while i <> 1
updatestr = "https://" & Servername & "/cwa/AsyncDataChannel.ashx?AckID=" &
latupdate & "&Ck=" & chk
req.Open "GET", updatestr, False, Username, Password
req.setRequestHeader "Cookie:", reqsessionID
req.send
wscript.echo req.status
if instr(req.responsetext,"div id=""exception""") then i = 1
oldlat = latupdate
latupdate = mid(req.responsetext,instr(req.responsetext,"latestUpdate=")+14,instr(instr(req.responsetext,"latestUpdate=")+14,req.responsetext,chr(34))-(instr(req.responsetext,"latestUpdate=")+14))
wscript.echo req.responsetext
wscript.echo latupdate
if latupdate = "yTimeout" then latupdate = oldlat
if instr(req.responsetext,"message=""") then
Imid = mid(req.responsetext,instr(req.responsetext,"imId=""")+6,instr(instr(req.responsetext,"imId=""")+6,req.responsetext,chr(34))-(instr(req.responsetext,"imId=""")+6))
message = mid(req.responsetext,instr(req.responsetext,"message=""")+9,instr(instr(req.responsetext,"message=""")+9,req.responsetext,chr(34))-(instr(req.responsetext,"message=""")+9))
wscript.echo "************************Message Recieved****************************"
wscript.echo message
wscript.echo "************************Message
Ends********************************"
wscript.echo Imid
exist = 0
if instr(req.responsetext,"inviters=""") then
Invite =
mid(req.responsetext,instr(req.responsetext,"inviters=""")+10,instr(instr(req.responsetext,"inviters=""")+10,req.responsetext,chr(34))-(instr(req.responsetext,"inviters=""")+10))
else
exist = 1
elen =
instr((instr(instr(req.responsetext,"action=""receive"""),req.responsetext,"uri=""")+5),req.responsetext,chr(34))
-
(instr(instr(req.responsetext,"action=""receive"""),req.responsetext,"uri=""")+5)
Invite =
mid(req.responsetext,instr(instr(req.responsetext,"action=""receive"""),req.responsetext,"uri=""")+5,elen)
end if
SIPQueryFilter = "(&(objectCategory=person)(msRTCSIP-PrimaryUserAddress=" &
Invite & "))"
strQuery1 = "<LDAP://" & strDefaultNamingContext & ">;" & SIPQueryFilter &
";DisplayName;subtree"
com.Properties("Page Size") = 100
Com.CommandText = strQuery1
Set Rs1 = Com.Execute
if rs1.recordcount <> 0 then
FromDisplay = rs1.fields("DisplayName")
else
FromDisplay = replace(Invite,"sip:","")
end if
call CreateItem(message,FromDisplay,fptrack)
Set xnItemNodes = objdom.getElementsByTagName("item")
Set xnChannelNodes = objdom.getElementsByTagName("channel")
wscript.echo xnItemNodes.length
if xnItemNodes.length > Maxitemnumber then
for i = Maxitemnumber to xnItemNodes.length
set parentnode = xnItemNodes(i-1).parentnode
parentnode.removechild(xnItemNodes(i-1))
next
end if
set xnChannelNode = xnChannelNodes(0)
fptrack = 0
objdom.save(feedfilename)
SendMessage("Thanks for your message it has been added to the board")
message = ""
Invite = ""
end if
wend


function SendMessage(message)

' ---Send Message---
Sendmsgcmd = "https://" & Servername & "/cwa/MainCommandHandler.ashx?Ck=" & chk
Messagestr = "cmdPkg=1,LcwAcceptImRequest," & Imid
req.open "POST", Sendmsgcmd, False, Username ,password
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.setRequestHeader "Cookie:", reqsessionID
req.send Messagestr
wscript.echo req.status
Sendmsgcmd = "https://" & Servername & "/cwa/MainCommandHandler.ashx?Ck=" & chk
Messagestr = "cmdPkg=2,LcwSendMessageRequest," & Imid & "," & Message &
",X-MMS-IM-Format: FN=Arial%253B EF=%253B CO=000000%253B CS=1%253B PF=00"
req.open "POST", Sendmsgcmd, False, Username ,password
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.setRequestHeader "Cookie:", reqsessionID
req.send Messagestr
wscript.echo req.status

end function

Sub CreateItem(PostSubject,PostFrom,Fp)
PostBody = ""
if instr(PostSubject,chr(10)) then
PostBody =
mid(PostSubject,instr(PostSubject,chr(10)),len(postsubject)-instr(PostSubject,chr(10)))
postbody = replace(postbody,chr(10),"<br>")
PostSubject = left(PostSubject,instr(PostSubject,chr(10)))
end if
rndval = Int((20000000000 * Rnd) + 1)
rval = day(now) & month(now) & year(now) & hour(now) & minute(now) & rndval
Set objField2 = objDom.createElement("item")
if fp = 1 then
xnChannelNode.appendChild objField2
else
xnChannelNode.insertbefore objField2, xnItemNodes(0)
end if
Set objField8 = objDom.createElement("guid")
Set objattID8 = objDom.createAttribute("isPermaLink")
objattID8.Text = "false"
objField8.setAttributeNode objattID8
objfield8.text = rval
objfield2.appendChild objField8
Set objField9 = objDom.createElement("title")
objfield9.text = PostSubject
objfield2.appendChild objField9
if PostBody <> "" then
Set objField11 = objDom.createElement("description")
objfield11.text = PostBody
objfield2.appendChild objField11
end if
Set xnChannelNode0 = objDom.createElement("author")
xnChannelNode0.text = PostFrom
objfield2.appendChild xnChannelNode0
Set xnChannelNode1 = objDom.createElement("pubDate")
xnChannelNode1.text = WeekdayName(weekday(now),3) & ", " & day(now()) & " " &
Monthname(month(now()),3) & " " & year(now()) & " " & formatdatetime(now(),4) &
":00 GMT"
objfield2.appendChild xnChannelNode1
set objfield2 = nothing
set objfield8 = nothing
set objfield9 = nothing
set xnChannelNode0 = nothing
set xnChannelNode1 = nothing

End Sub

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Exporting and Uploading Mailbox Items using Exchange Web Services using the new ExportItems and UploadItems operations in Exchange 2010 SP1

Two new EWS Operations ExportItems and UploadItems where introduced in Exchange 2010 SP1 that allowed you to do a number of useful things that where previously not possible using Exchange Web Services. Any object that Exchange stores is basically a collection of properties for example a message object is a collection of Message properties, Recipient properties and Attachment properties with a few meta properties that describe the underlying storage thrown in. Normally when using EWS you can access these properties in a number of a ways eg one example is using the strongly type objects such as emailmessage that presents the underlying properties in an intuitive way that's easy to use. Another way is using Extended Properties to access the underlying properties directly. However previously in EWS there was no method to access every property of a message hence there is no way to export or import an item and maintain full fidelity of every property on that item (you could export the...

EWS Create Mailbox folder Powershell module for Exchange and Office365 Mailboxes

This is a rollup post for a couple of scripts I've posted in the past for creating folders using EWS in an Exchange OnPremise or Exchange online Cloud mailbox. It can do the following Create a Folder in the Root of the Mailbox Create-Folder -Mailboxname mailbox@domain.com -NewFolderName test Create a Folder as a SubFolder of the Inbox Create-Folder -Mailboxname mailbox@domain.com -NewFolderName test -ParentFolder '\Inbox' Create a Folder as a SubFolder of the Inbox using EWS Impersonation Create-Folder -Mailboxname mailbox@domain.com -NewFolderName test -ParentFolder '\Inbox' -useImpersonation Create a new Contacts Folder as a SubFolder of the Mailboxes Contacts Folder Create-Folder -Mailboxname mailbox@domain.com -NewFolderName test -ParentFolder '\Contacts' -FolderClass IPF.Contact Create a new Calendar Folder as a SubFolder of the Mailboxes Calendar Folder Create-Folder -Mailboxname mailbox@domain.com -NewFolderName test -Parent...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.