1

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.

5
  • 2
    Have you tried inspecting the raw JSON structure in $Request.Body? What message do you receive if you do text = "You said: '$($Request.Body)'"? Commented Mar 18, 2024 at 20:42
  • This sounds like a typical CHAT issue. Is the text being sent in one message or multiple messages? You can't parse a message if it is not complete. Often with chat applications it is using timers to send data so the receiver doesn't get the entire text until more than one response is read. You have to wait until the entire message is received before processing. Which means joining messages and waiting for a terminating character like a return before processing. Commented Mar 18, 2024 at 21:58
  • @MathiasR.Jessen, Yes, I get You said: 'System.Management.Automation.OrderedHashtable' Commented Mar 18, 2024 at 23:17
  • By the looks of things, $Request.Body (maybe) already contains the parsed json in a hashtable and $text = $Request.Body | ComvertFrom-Json is silently failing, leaving $text equal to $null and results in the output string You said: . Try $text = $Request.Body.text instead and see if that shows the text in the output message… Commented Mar 19, 2024 at 0:23
  • @mclayton, $text = $Request.Body.text results in a null output after You said: too. Commented Mar 19, 2024 at 0:56

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.