Ayan Mullick
Ayan Mullick

Reputation: 162

MSTeams echo bot using PowerShell Azure Functions

How should one parse the chat text received as JSON payload in the request body when using an outgoing webhook as an application in a Teams group to fetch the output of a PowerShell Azure function?

using namespace System.Net
param($Request, $TriggerMetadata) 

$text       =($Request.Body|ConvertFrom-Json).text #Parse request body to extract message text                     
   
$Response   = @{                                   #This simply echoes the received text
    type    = 'message'
    text    = "You said: $text"} | ConvertTo-Json

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
  StatusCode= [HttpStatusCode]::OK
  Body      = $Response})

The code is working partially so that I can get the text output of the function ['You said'] as a response in Teams. However, it cannot parse the text passed while calling the function in Teams and echo it in the response.

Upvotes: 1

Views: 124

Answers (1)

Ayan Mullick
Ayan Mullick

Reputation: 162

PFB how to parse the Teams message text from the JSON payload in the request body and the regex to remove the application name while echoing.

using namespace System.Net
param($Request, $TriggerMetadata)

$text        =  $Request.Body['text'] -replace '.*PSFuncEchoBot\s*'

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
  StatusCode = [HttpStatusCode]::OK
  Body       = @{type = 'message'; text = "You said: $text"}    
})

PFB output screenshot

enter image description here

Upvotes: 0

Related Questions