-3

I researched a bit and found a way to leverage Lync 2013 SDK to send an IM to a contact in my contact list. But it would be much more useful if there were a way to get messages from the conversation window.

  • Can you add what you research, what is wrong and the desired output is? Have a look at [how to ask](https://stackoverflow.com/help/how-to-ask). – nuric May 28 '18 at 10:50
  • [link](https://blogs.technet.microsoft.com/csps/2011/05/05/send-an-instant-message-from-a-script/) I used this as a guide to write a small script to send messages through lync. – Sarabpreet Singh May 28 '18 at 12:00

1 Answers1

0

See Sample send receive Lync / Skype for business messages using powershell (Author: Grzegorz Kulikowski)

to test with another user

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++
   }

to test with yourself

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++
   }

Also, these may help:

Send and receive text in a conversation

Learn Skype Web SDK Day 15 : Receiving Instant Messages

VA systems engineer
  • 2,856
  • 2
  • 14
  • 38
  • I am getting this error when tried this script: `format-default : Exception has been thrown by the target of an invocation. + CategoryInfo : NotSpecified: (:) [format-default], TargetInvocationException + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Microsoft.PowerShell.Co mmands.FormatDefaultCommand` – Karthic Srinivasan Jun 26 '19 at 10:32
  • 1
    @KarthicSrinivasan Did you install the Lync SDK package as described in the link at the top of my answer? [Sample send receive Lync / Skype for business messages using powershell](https://psvmware.wordpress.com/2017/08/08/sample-send-receive-lync-skype-for-business-messages-using-powershell/)? It says: "Before you start you need to obtain the Lync SDK package: [lyncsdk.exe](https://download.microsoft.com/download/0/5/6/056013D5-98F8-4B79-88EF-D221E519C37F/lyncsdk.exe) – VA systems engineer Jun 26 '19 at 12:54
  • yes i have installed the lynk sdk. still getting the error. This line `$conversation.AddParticipant($person)` is trowing this exception – Karthic Srinivasan Jun 29 '19 at 11:24
  • 1
    Hard to say without knowing the details of your setup, i.e., OS, PowerShell version, what account you're using, etc., etc. A search on the exception text revealed this: [Exception has been thrown while reading SSA via PowerShell](https://social.technet.microsoft.com/Forums/lync/en-US/dee26f6a-bded-4fd9-ab63-543fb72095a7/exception-has-been-thrown-while-reading-ssa-via-powershell?forum=sharepointadminprevious). Good luck! – VA systems engineer Jun 29 '19 at 12:30