EvotecIT / PSTeams

PSTeams is a PowerShell Module working on Windows / Linux and Mac. It allows sending notifications to Microsoft Teams via WebHook Notifications. It's pretty flexible and provides a bunch of options. Initially, it only supported one sort of Team Cards but since version 2.X.X it supports Adaptive Cards, Hero Cards, List Cards, and Thumbnail Cards. All those new cards have their own cmdlets and the old version of creating Teams Cards stays as-is for compatibility reasons.
MIT License
409 stars 41 forks source link

creating a c# wrapper #48

Closed mvit777 closed 2 years ago

mvit777 commented 2 years ago

Hi for ease of use I created a c# wrapper library for sending cards. Following below an example of what I mean:

public class ThumbnailCard : BaseCard
    {
        public override void SendCard(ref PowerShell ps)
        {
            //var command = "New-ThumbnailCard -Title " + Title + " " +
            //    "-SubTitle \"" + SubTitle + "\" " +
            //    "-Text \"" + Text + "\"" +
            //    "{\r\n    New-ThumbnailImage -Url 'https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png' " +
            //    "-AltText \"Bender Rodríguez\"\r\n} -Uri " + Uri + "";
            //ps.AddCommand(command);
            ps.AddCommand("New-ThumbnailCard")
            .AddParameter("Title", Title)
            .AddParameter("SubTitle", SubTitle)
            .AddParameter("Text", Text)
            .AddParameter("Uri", Uri)
            ;
            ps.Invoke();
            ps.Dispose();
        }
    }

when calling it from powershell script

Test-CardCmdlet -Json $jsonConfig -PsTeamsPath $PsTeamsPath -CardType $config.Type

I recieve an error about a missing parameter Content which I'm not sure what is referring to as it is not part of the api. Also I'm not using addScript to import psteams into the c# lib but rather import via

initial.ImportPSModule(new string[] { PsTeamsPath });

Any idea? Thanks again for excellent module

mvit777 commented 2 years ago

Found the problem. It turned out that the thumbnail image is actually a mandatory field but it is named Content. To fix it I had to do something like the following

ScriptBlock scriptBlock = ScriptBlock.Create("{\r\n    New-ThumbnailImage -Url 'https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png' -AltText \"Bender Rodríguez\"\r\n}");

and then

.AddParameter("Content", scriptBlock)