我正在为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或屏幕共享之类的东西,没什么用。
我在哪里搞砸呢?我更喜欢Powershell中的答案,但我不认为这是Powershell特有的问题,因此,如果您知道如何使用C#或Visual Basic或类似的方式来解决问题,我也将不胜感激。
我没有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 {
#...
}
}
}
您能否根据Frode F的建议发布您的工作代码?