3

我正在为 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 或类似的东西中做到这一点,我也会很感激。

4

1 回答 1

3

我没有 Lync,因此我可以自己进行测试,但请查看此链接,其中显示了如何使用 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 {
                            #...

        }
    }
}
于 2013-03-06T23:16:32.187 回答