我正在为instantmessagerreceived事件设置一个处理程序,但它似乎只在传出的文本消息上触发,而不是传入的。下面是我正在运行的代码:
# 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 Windowsgrowlnotify.exe' /a:Lync /ai:$icon /r:$types "Registration."
#We just need the Model API for this example
import-module "C:Program Files (x86)Microsoft LyncSDKAssembliesDesktopMicrosoft.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 Windowsgrowlnotify.exe' /a:Lync /n:new-im /t:"New Instant Message" "$name $message"
}
}
}
# If this exits, no more events.
while (1) { }
每次我给别人输入即时消息时,它就像我试图处理传入消息一样。但没有什么东西会对他们开火,只是外向。我看了所有的文档,没有其他的候选事件,我确定是这个。但是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或类似的东西来解决,我也很感激。
我没有Lync,所以我可以自己测试,但是看看这个链接,它展示了如何使用API。
问题是(据我所知)每个参与者每个媒体都有一个模式。因此,对于仅使用文本的两个成员的对话,将有2种模式,一种用于传入消息(来自远程参与者),另一种用于传出消息。这在
中指定在接收、或发送即时消息时发生,如果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 {
#...
}
}
}