我可以在Microsoft Lync API中为传入的IM设置事件处理程序吗?

时间:2013-03-06 22:45:52

标签: events powershell lync growl lync-client-sdk

我正在为InstantMessageReceived事件设置一个处理程序,但它似乎只在传出的文本消息上触发,而不是传入。这是我正在运行的代码:

# Register the app with Growl
$icon = "https://docs.google.com/uc?export=download&id=0B1Weg9ZlwneOZmY2b1NSVXJ0Q2s"
$types = '"new-im","new-call","invitation","share"'
& 'C:\Program Files (x86)\Growl for Windows\growlnotify.exe' /a:Lync /ai:$icon /r:$types "Registration."

#We just need the Model API for this example
import-module "C:\Program Files (x86)\Microsoft Lync\SDK\Assemblies\Desktop\Microsoft.Lync.Model.Dll"

#Get a reference to the Client object
$client = [Microsoft.Lync.Model.LyncClient]::GetClient()

#Set the client to reference to the local client
$self = $client.Self

# What do we do here?
$conversationMgr = $client.ConversationManager

# Register events for existing conversations.

$i = 0

for ($i=0; $i -lt $conversationMgr.Conversations.Count; $i++) {

Register-ObjectEvent -InputObject $conversationMgr.Conversations[$i].Modalities[1] -EventName "InstantMessageReceived" `
                    -SourceIdentifier "new im $i" `
                    -action {
                               $message = $EventArgs.Text
                               Write-Host "DEBUG: New incoming IM - $message"

                               # Try to get the name of the person...
                               $contactInfo = $Event.Sender.Conversation.Participants[1].Contact.GetContactInformation([Microsoft.Lync.Model.ContactInformationType[]] @("FirstName", "LastName", "DisplayName", "PrimaryEmailAddress", "Photo", "IconUrl", "IconStream"))
                               $name = " "
                               if ($contactInfo.Get_Item("FirstName")) { $name = $contactInfo.Get_Item("FirstName") + " " + $contactInfo.Get_Item("LastName") + ":" }
                               elseif ($contactInfo.Get_Item("DisplayName")) { $name = $contactInfo.Get_Item("DisplayName") + ":"}
                               else { $name = $contactInfo.Get_Item("PrimaryEmailAddress") + ":" }

                               # We need to check if the Lync window (conversation?) has focus or not.
                               if (1) {
                                  # We need to send our growl notification.
                                  & 'C:\Program Files (x86)\Growl for Windows\growlnotify.exe' /a:Lync /n:new-im /t:"New Instant Message" "$name $message" 
                               }

                            }
}

# If this exits, no more events.
while (1) { }

每次我向其他人输入IM消息时,它都会执行我正在尝试为传入消息执行的操作。但对于那些只是外向的人来说,什么都没有。我已经浏览了所有文档,并且没有任何其他候选事件,我确定它就是这个。但是Modality对象只存储了一些关于它是IM还是屏幕共享等的东西,没什么用处。

http://msdn.microsoft.com/en-us/library/lync/microsoft.lync.model.conversation.instantmessagemodality_di_3_uc_ocs14mreflyncclnt_members(v=office.14).aspx

我在哪里搞砸了?我更喜欢Powershell中的答案,但我不认为这是Powershell特有的问题,所以如果你知道如何在C#或Visual Basic中做到这一点,我也会很感激。

1 个答案:

答案 0 :(得分:3)

我没有Lync,所以我可以自己测试一下,但请查看this link,了解如何使用API​​。

问题是(根据我的理解)每个媒体每个参与者都有一种模式。因此,对于仅使用文本的两个成员的对话,将有两种模式,一种用于传入消息(来自远程参与者),一种用于传出。这在

中指定
  

在收到即时消息时发生,如果InstantMessageModality属于本地参与者,则发送。

来源:MSDN

当您注册对象事件时,将其注册为“您的模态”,而不是远程模态。为了解决这个问题,你似乎需要从经理那里接受每个对话,看看除了代表你的人之外的每个参与者(检查IsSelf属性)。然后从参与者那里获取模态(除了你自己)并注册InstantMessageReceived事件。

至少那是我从中得到的,但正如我所说,我没有使用Lync的经验,所以我很容易出错。

我猜怎么做(非常未经测试):

# What do we do here?  You get the manager the keeps track of every conversation
$conversationMgr = $client.ConversationManager

# Register events for existing conversations.

#You may need to use '$conversation in $conversationMgr.GetEnumerator()'
foreach ($conversation in $conversationMgr) {

    #Get remote participants
    $conversation.Participants | where { !$_.IsSelf } | foreach {
        #Get IM modality
        $textmod = [InstantMessageModality]($_.Modalities[ModalityTypes.InstantMessage])

        Register-ObjectEvent -InputObject $textmod -EventName "InstantMessageReceived" `
                    -SourceIdentifier "new im $i" `
                    -action {
                            #...

        }
    }
}