Before you start you need to obtain the Lync SDK package (https://download.microsoft.com/download/0/5/6/056013D5-98F8-4B79-88EF-D221E519C37F/lyncsdk.exe)
I had hard time to have it working with listening what ‘other’ people are writing in the conversation. Thanks to this post(‘John O’ reply) i was able to properly hook the event(to the right participant).
The ‘what*’, was aimed at message: “What is 2+4” or “what is 10*3″ for example.
In response you should get the reply with the result.
If you want to test it with ‘somebody’.
[sourcecode language=”powershell”]
cd "C:\Program Files (x86)\Microsoft Office\Office15\lyncsdk\Assemblies\Desktop"
Import-Module .\Microsoft.Lync.Model.dll
$Client = [Microsoft.Lync.Model.LyncClient]::GetClient()
$Conversation = $client.ConversationManager.AddConversation()
$person=$client.ContactManager.GetContactByUri(‘person@domain.com’)
$conversation.AddParticipant($person)
Get-EventSubscriber|Unregister-Event
# For each participant in the conversation
$conversation.Participants | Where { !$_.IsSelf } | foreach {
Register-ObjectEvent -InputObject $_.Modalities[1] -EventName "InstantMessageReceived" -SourceIdentifier "person $i" -action {
$global:conv = $event
$msg = $conv.SourceEventArgs.Text.trim()
write-host $msg
switch -Wildcard ($msg) {
"What*" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage((Invoke-Expression $msg.split()[-1]), {}, 0)}
"Hello" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage("Hello human", {}, 0)}
"stupid robot" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage("Humanity is overrated", {}, 0)}
}
}
$i++
}
[/sourcecode]
Or if you don’t have friends and want to test it against yourself (2nd client running).
[sourcecode language=”powershell”]
cd "C:\Program Files (x86)\Microsoft Office\Office15\lyncsdk\Assemblies\Desktop"
Import-Module .\Microsoft.Lync.Model.dll
$Client = [Microsoft.Lync.Model.LyncClient]::GetClient()
$Conversation = $client.ConversationManager.AddConversation()
$conversation.AddParticipant($client.Self.Contact)
Get-EventSubscriber|Unregister-Event
$conversation.Participants[0]| foreach {
Register-ObjectEvent -InputObject $_.Modalities[1] -EventName "InstantMessageReceived" -SourceIdentifier "person $i" -action {
$global:conv = $event
$msg = $conv.SourceEventArgs.Text.trim()
write-host $msg
#if ($msg -like ‘What*’) {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage((Invoke-Expression $msg.split()[-1]), {}, 0)}
switch -Wildcard ($msg) {
"What*" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage((Invoke-Expression $msg.split()[-1]), {}, 0)}
"Hello" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage("Hello human", {}, 0)}
"stupid robot" {$Conversation.Modalities[‘InstantMessage’].BeginSendMessage("Humanity is overrated", {}, 0)}
}
}
$i++
}
[/sourcecode]
You can send message using the PS session anytime then:
$Conversation.Modalities[‘InstantMessage’].BeginSendMessage(‘text’, {}, 0)}
Some useful links:
https://msdn.microsoft.com/en-us/library/office/jj937273.aspx
https://msdn.microsoft.com/en-us/library/office/hh243705(v=office.14).aspx
https://msdn.microsoft.com/en-us/library/office/jj933215.aspx
https://msdn.microsoft.com/en-us/library/office/jj937298.aspx
https://msdn.microsoft.com/en-us/library/office/jj933248.aspx
https://blogs.technet.microsoft.com/heyscriptingguy/2011/06/16/use-asynchronous-event-handling-in-powershell/