Text to ms teams

Hi,

I use http request activity to send message to ms teams. In my body I have e.g.
“{‘Text’:‘I start my process’}”
It is possible to use in my text string variable (strText=“I start my process”)? How to write it?

Hi @Krzysztof,

You can use a string templating technique.
Assign

  1. Message (str) = “{‘Text’:‘PAYLOAD’}”
  2. Payload = “I start my process”

Assign
3. Message (str) = Message.Replace(“PAYLOAD”, Payload)

Your Message variable will then be “{‘Text’:‘I start my process’}”
Try this.

For complex messages to be sent to teams you will need to ensure that you follow a fixed Json format: Sending messages to Connectors and Webhooks - Teams | Microsoft Docs

Challenge
However, you may have two totally new problems when you try sending the Message string to MS Teams

  1. The Message string value has single quotes and teams expects that you send a proper Json string
  2. Teams incoming webhooks have a fixed json formatting and if it does not receive incoming string in the same format, it will not post it and you will get an error

Ok, but then to my body I have to write a variable. Could you prepare send test bot?

I got my output message, but I do not know what to write to my body (http request activity).

Probably the problem is that in my output I get
{‘Text’:‘I start my process’}
against
“{‘Text’:‘I start my process’}”
image

To body I have to write “{‘Text’:‘I start my process’}”

Hi again @Krzysztof,

I advise you first check the attached xaml file and the Payload.txt file to understand this slightly different approach, which I normally use with MS Teams integration.

  1. Read Json string from a text file, makes it easy to handle Json formatting with double quotes especially if you want to use adaptive cards features in MS Teams.

  2. Use .Replace method any number of times to suit your requirement. For example, if you want to send an adaptive card to teams you can use multiple template string and use .Replace method to allocate any string (variable in UiPath) you wish.

  3. Most important is to replace quotes “’” with double quotes for each instance “”""

    FinalPayload = YourModifiedPayload.Replace("'","""")
    
  4. Use the HTTP request activity and provide the incoming webhook link and the body will be FinalPayload. It is advisable to use the StatusCode output from HTTP activity to check if the request was a success.

You can reuse the same concept to send messages to any webhook endpoint not only teams.

Result of the run- a message in teams channel.

I have made additional annotations in the xaml file. It has one argument which needs to be provided by you i.e., your teams incoming webhook url.
ProjectFiles : SendTeamsMessage.zip (36.3 KB)

Hope this helps!

1 Like

The simply solution:
“{‘Text’:’” + strMessage + “’}”
It works.

The problem was that I copy-paste from Forum the ’ is different on the forum please WRITE Step by step the characters from KEYBOARD.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.