Error in Template for "Microsoft Teams - Post a message"

Hi,

I´m testing the template for posting messages from Octopus Deploy to Microsoft Teams with the template made for this.

When I try it I get the post in Teams as long as I use simple text in “Message Body” and “Message Title”.
So far all is well.

When I use attributes instead like #{Octopus.Project.Name} I don’t get any posts in Teams and in the log for the deploy
I see the error below in the POST content.

“System.Text.DecoderFallbackException: Unable to translate bytes [F6] at index 27 from specified code page to Unicode.”

Any ideas what could be wrong? What does this mean?

Regards
//Stefan

Hi Stefan,

Thanks for reaching out! It looks like your variables aren’t translating properly. Could you follow these steps and send us a verbose deployment log?

  1. Add these 2 variables to your project shown in our documentation

  2. Create a new release (so the new variables take effect) and deploy it.

  3. Send us the raw log of that deployment, as shown in our documentation

Can you also include any other script(s) you may be using?
Are you using those same variables anywhere else in the process?

That will be a perfect starting point to figure out why those variables aren’t working as expected.

Kind regards,

Kenny

Hi,

Thank you for your answer.
I just realized I was using Swedish characters in the projectname.
I changed the projectname and now it works as it’s supposted to do.

Hi,

I have attatched the RAW-file.
This is from a simple testproject I have set up.

It deploys a website, apppool and stops and starts a windows service.

In the end it sends an email to me and lastly sends a post to one of my
channels in Microsoft Teams.
The email contains the same variable as for posting to Teams.
The email shows the correct coding for Swedish characters.
The post to Teams never appear.

ServerTasks-18238.log.txt (821 KB)

Hi Stefan,

I’m going to pick this up from Kenneth just in case there’s a need to go back to Microsoft.

It looks like the issue is likely on the Microsoft Teams side. In my tests, Swedish characters are sent correctly using that Powershell, but it looks like there’s an exception thrown by Teams when it tries to use the content.

To try to work around this, can I get you to modify the step template script slightly?

  • Can you try removing the charset definition from the Content-Type argument? Just leaving: -ContentType "application/json"
  • If that doesn’t work, can you add it back but try using UTF-16 instead? -ContentType "application/json; charset=utf-16"

Let me know how you go!

Thanks,
Damian

Hi,

I tested your suggestions but still same type of result.
I agree with you that this is most likly a Teams issue.
I tried a similar script from powershell that sent a post to Teams
via another webhook and the post appeard but swedish characters looked all
wrong.

Hi Stefan,

Thanks for trying those things. I’ll get in contact with Microsoft to see what kind of encoding they support via the API.

It would help if you were able to send that powershell script (feel free to remove the webhook URL) and some screenshots to show the encoding breaking!

Thanks,
Damian

Hi,

I took another look at my ps-script before answering you and noticed an
error.
Well. Now when I run this ps-script it do post the characters correctly.
(I forgot to add UTF-8 in my script the first time.)

$uri = ‘(insert your own webhook url)’
$body = ConvertTo-JSON @{
text = ‘Posting testmessage to channel with ÅÄÖ’
}
#last 3 characters in text is swedish characters.

Invoke-WebRequest -Method Post -uri $uri -body $body -ContentType
“application/json; charset=utf-8”

This gives me the following in Teams:
[image: Infogad bild 1]

This is the output from powershell when I run the script:
StatusCode : 200
StatusDescription : OK
Content : 1
RawContent : HTTP/1.1 200 OK
Pragma: no-cache
X-CalculatedFETarget: DB3PR05CU001.internal.outlook.com
X-BackEndHttpStatus: 200,200
X-FEProxyInfo: DB3PR05CA0078.EURPRD05.PROD.OUTLOOK.COM
X-CalculatedBETarget: …
Forms : {}
Headers : {[Pragma, no-cache], [X-CalculatedFETarget,
DB3PR05CU001.internal.outlook.com], [X-BackEndHttpStatus, 200,200],
[X-FEProxyInfo, DB3PR05CA0078.EURPRD05.PROD.OUTLOOK.COM]…
.}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 1

image.png

Hi Stefan,

Are you able to add a “Title” property to the JSON object you’re sending? I’m wondering whether it’s the title field that’s not able to handle special characters (rather than the body text).

For example:

$uri = '(insert your own webhook url)' 
$body = ConvertTo-JSON @{ text = 'Posting testmessage to channel with ÅÄÖ', title = 'Title with with ÅÄÖ' } #last 3 characters in text is swedish characters.

Thanks,
Damian

Ran the following script and it worked fine:
$uri = ‘(insert your own webhook url)’
$body = ConvertTo-JSON @{
text = ‘Posting testmessage to channel with ÅÄÖ’
title = ‘Title with with ÅÄÖ’
}
Invoke-WebRequest -Method Post -uri $uri -body $body -ContentType
“application/json; charset=utf-8” -UseBasicParsing

Screenshot with both Title and Text filed included.

[image: Infogad bild 1]

image.png

I think I got it working now. :slight_smile:

If I change your template for Teams so that the powershell script looks
like this it seems to work for me.

$json = ConvertTo-Json -Compress -InputObject @{“title” = $Title; “text” =
$Body; }
Invoke-WebRequest -Method Post -Uri $Url -ContentType ‘application/json;
charset=utf8’ -Body ([System.Text.Encoding]::UTF8.GetBytes($json))
-UseBasicParsing

The release goes without throwing any exceptions in Octopus and it look
alright in Teams.

image.png

Hi Stefan,

Interesting, thanks for looking at that!

I’m happy to change the existing community step template if you like, but given you discovered a fix, did you want to submit a Pull Request? That way you’ll get all the glory :wink:

The step in GitHub is here. You should really just need to replace that file with the export of your updated step template. There a couple of additional instructions in the readme for the GitHub repository.

Let me know if you have any issues or if you’d prefer me to do it!

Thanks,
Damian

Hi and Thanks.

I just need to go thru the script again first.
I had one issue with “my” script today but I’m not sure why yet.

Regards
//Stefan