Hi everyone,
I hve created a custom connector in Power Automate with a polling trigger that should fire when my Microsoft Teams status has changed. To achieve this I have added some custom code that checks if the provided state parameter equals the current availability and returns an empty array in the "value" property. Otherwise, It wraps the response from the Graph API as single object into an array.
The logic itself seems to work because it works on the Test tab of the custom connector. But when I put the trigger into a flow it fires every minute with the same availability though it doesn't change.
This is my Swagger:
swagger: '2.0'
info: {title: Default title, description: '', version: '1.0'}
host: graph.microsoft.com
basePath: /v1.0/
schemes: [https]
consumes: []
produces: []
paths:
/me/presence:
get:
responses:
'200':
description: OK
schema:
type: object
properties:
value:
type: array
items:
type: object
properties:
'@odata.context': {type: string, description: '@odata.context'}
id: {type: string, description: id}
availability: {type: string, description: availability}
activity: {type: string, description: activity}
description: value
summary: Fires if the presence of the current user changes
description: Fires if the presence of the current user changes
operationId: OnPresenceChange
x-ms-trigger: batch
x-ms-trigger-metadata: {mode: polling, kind: query}
parameters:
- name: availability
in: query
required: false
type: string
x-ms-visibility: internal
x-ms-trigger-value: {value-collection: value, value-path: availability}
x-ms-trigger-hint: Change your Teams status to fire the trigger
definitions: {}
parameters: {}
responses: {}
securityDefinitions:
oauth2_auth:
type: oauth2
flow: accessCode
authorizationUrl: https://login.windows.net/common/oauth2/authorize
tokenUrl: https://login.windows.net/common/oauth2/authorize
scopes: {Presence.Read: Presence.Read}
security:
- oauth2_auth: [Presence.Read]
tags: []
And here comes the custom code:
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
// Check if the operation ID matches what is specified in the OpenAPI definition of the connector
if (this.Context.OperationId == "OnPresenceChange")
{
return await this.HandleOnPresenceChange().ConfigureAwait(false);
}
// Handle an invalid operation ID
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
return response;
}
private async Task<HttpResponseMessage> HandleOnPresenceChange()
{
// Use the context to forward/send an HTTP request
HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
// Do the transformation if the response was successful, otherwise return error responses as-is
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);
// Example case: response string is some JSON object
var result = JObject.Parse(responseString);
var oldAvailability = HttpUtility.ParseQueryString(this.Context.Request.RequestUri.Query).Get("availability");
var newAvailability = result["availability"].Value<string>();
var logger = this.Context.Logger;
logger.Log(LogLevel.Debug, new EventId(100, "OldAvailability"), oldAvailability);
logger.Log(LogLevel.Debug, new EventId(200, "NewAvailability"), newAvailability);
if (newAvailability == oldAvailability) {
// Wrap the original JSON object into a new JSON object with just one key ('wrapped')
var newResult = new JObject
{
["value"] = new JArray(),
};
response.Content = CreateJsonContent(newResult.ToString());
}
else
{
// Wrap the original JSON object into a new JSON object with just one key ('wrapped')
var newResult = new JObject
{
["value"] = new JArray(result),
};
response.Content = CreateJsonContent(newResult.ToString());
}
}
return response;
}
}
It looks like if the state isn't maintained. Any ideas?
Any help is highly appreciated.
Michael