I am building an integration that enables XProtect Smart Client users to create a soft triggers to open and close doors via an overlay button.
What I know:
You can create rules and associate the overlay buttons through the MIP VMS Config REST API using the /rules and the reference key and trigger webhooks with the `startAction: MIPAction:47c713ce-920e-5b05-ae25-b7cbc2f66235=Unlock Door Unit, Access Point`
What I don’t know:
Where in the rule to set the URL of where to send the webhook to.
I’m aware of the Code Examples in the webhook documentation, but in those examples the only discussed manner to create a webhook subscription is through the XProtect UI. Is there a way to programmatically created a webhook subscription?
Hi @David Jones,
The webhook must be created before you create the tool with the webhook as an action.
Usually when creating a rule, you will get a list of valid values for the triggers and actions, so you’ll need to create the webhook with its URL first, then that will create a MIP item in the configuration that you’ll find as an available option for the rule action.
Last I checked there were no strongly typed classes for this, so I had to use the configuration api interface directly. Here is the source code for the New-VmsWebhook command in MilestonePSTools, our PowerShell module for reference. The cmdlet is written in PowerShell rather than C# but hopefully it still helps!
function New-VmsWebhook {
[CmdletBinding(SupportsShouldProcess)]
[OutputType([MilestonePSTools.Webhook])]
[RequiresVmsConnection()]
[RequiresVmsVersion('23.1')]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 0)]
[Alias('DisplayName')]
[string]
$Name,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[uri]
$Address,
[Parameter(ValueFromPipelineByPropertyName)]
[AllowEmptyString()]
[AllowNull()]
[string]
$Token,
# Any unrecognized parameters and their values will be ignored when splatting a hashtable with keys that do not match a parameter name.
[Parameter(ValueFromRemainingArguments, DontShow)]
[object[]]
$ExtraParams
)
begin {
Assert-VmsRequirementsMet
}
process {
$folder = Get-ConfigurationItem -Path 'MIPKind[b9a5bc9c-e9a5-4a15-8453-ffa41f2815ac]/MIPItemFolder'
$invokeInfo = $folder | Invoke-Method -MethodId AddMIPItem
'ApiVersion', 'Address', 'Token' | ForEach-Object {
$invokeInfo.Properties += [VideoOS.ConfigurationApi.ClientService.Property]@{
Key = $_
DisplayName = $_
ValueType = 'String'
IsSettable = $true
}
}
$action = 'Create webhook {0}' -f $Name
if ($PSCmdlet.ShouldProcess((Get-VmsSite).Name, $action)) {
$invokeInfo | Set-ConfigurationItemProperty -Key Name -Value $Name
$invokeInfo | Set-ConfigurationItemProperty -Key Address -Value $Address
$invokeInfo | Set-ConfigurationItemProperty -Key ApiVersion -Value 'v1.0'
if (-not [string]::IsNullOrWhiteSpace($Token)) {
$invokeInfo | Set-ConfigurationItemProperty -Key Token -Value $Token
}
$invokeInfo | Invoke-Method -MethodId AddMIPItem | Get-ConfigurationItem | ConvertTo-Webhook
}
}
}