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...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
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.