├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── GitVersion.yml ├── LICENSE ├── README.md ├── TODO ├── build.cake ├── build.ps1 ├── build.sh ├── cake.config ├── slack.png └── src ├── Slack.Webhooks.Tests ├── ActionsBlockFixtures.cs ├── BlockFixtures.cs ├── ButtonElementFixtures.cs ├── ConfirmationElementFixtures.cs ├── ContextBlockFixtures.cs ├── DatePickerElementFixtures.cs ├── ElementFixtures.cs ├── EmojiShould.cs ├── FileBlockFixtures.cs ├── HeaderBlockFixtures.cs ├── ImageBlockFixtures.cs ├── ImageElementFixtures.cs ├── InputBlockFixtures.cs ├── MultiSelectExternalElementFixtures.cs ├── MultiSelectStaticElementFixtures.cs ├── MultiSelectUsersElementFixtures.cs ├── OptionElementFixtures.cs ├── OptionGroupElementFixtures.cs ├── OverflowElementFixtures.cs ├── PlainTextInputElementFixtures.cs ├── Properties │ └── AssemblyInfo.cs ├── RadioButtonElementFixtures.cs ├── SectionBlockFixtures.cs ├── SelectElementFixtures.cs ├── SelectExternalElementFixtures.cs ├── SelectStaticElementFixtures.cs ├── SelectUsersElementFixtures.cs ├── Slack.Webhooks.Tests.csproj ├── SlackClientFixtures.cs ├── SlackMessageFixtures.cs ├── TextElementFixtures.cs └── packages.config ├── Slack.Webhooks.sln └── Slack.Webhooks ├── Action ├── Confirm.cs ├── Option.cs └── OptionGroup.cs ├── Block.cs ├── Blocks ├── Actions.cs ├── Context.cs ├── Divider.cs ├── File.cs ├── Header.cs ├── Image.cs ├── Input.cs └── Section.cs ├── Elements ├── Button.cs ├── Confirmation.cs ├── DatePicker.cs ├── Element.cs ├── Image.cs ├── MultiSelect.cs ├── Option.cs ├── OptionGroup.cs ├── Overflow.cs ├── PlainTextInput.cs ├── RadioButton.cs ├── Select.cs └── TextObject.cs ├── Emoji.cs ├── Enums.cs ├── ISlackClient.cs ├── Interfaces ├── IActionElement.cs ├── IContextElement.cs └── IInputElement.cs ├── Properties └── AssemblyInfo.cs ├── Slack.Webhooks.csproj ├── Slack.Webhooks.nuspec ├── SlackAction.cs ├── SlackAttachment.cs ├── SlackClient.cs ├── SlackField.cs └── SlackMessage.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Stuff 2 | on: 3 | push: 4 | branches: 5 | - feature/* 6 | - hotfix/* 7 | - master 8 | tags: 9 | - v* 10 | pull_request: 11 | 12 | jobs: 13 | build: 14 | runs-on: windows-latest 15 | name: Dotnet build 16 | steps: 17 | - uses: actions/checkout@v2 18 | - run: | 19 | git fetch --prune --unshallow 20 | - run: | 21 | git branch master origin/master 22 | if: github.event_name != 'push' || (github.event_name == 'push' && github.ref != 'refs/heads/master') 23 | - name: Setup dotnet 24 | uses: actions/setup-dotnet@v1 25 | with: 26 | dotnet-version: "2.2.103" 27 | - name: Build 28 | run: .\build.ps1 29 | shell: pwsh 30 | id: build 31 | env: 32 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 33 | NUGET_TOKEN: ${{ secrets.NugetKey }} 34 | - name: Add artifacts 35 | uses: actions/upload-artifact@v1 36 | with: 37 | name: ${{ steps.build.outputs.nupkg_name }} 38 | path: ${{ steps.build.outputs.nupkg }} 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | src/.vs 16 | tools/ 17 | artifacts/ 18 | coverage-results/ 19 | 20 | # Build results 21 | [Dd]ebug/ 22 | [Rr]elease/ 23 | x64/ 24 | *_i.c 25 | *_p.c 26 | *.ilk 27 | *.meta 28 | *.obj 29 | *.pch 30 | *.pdb 31 | *.pgc 32 | *.pgd 33 | *.rsp 34 | *.sbr 35 | *.tlb 36 | *.tli 37 | *.tlh 38 | *.tmp 39 | *.log 40 | *.vspscc 41 | *.vssscc 42 | .builds 43 | 44 | # Visual C++ cache files 45 | ipch/ 46 | *.aps 47 | *.ncb 48 | *.opensdf 49 | *.sdf 50 | 51 | # Visual Studio profiler 52 | *.psess 53 | *.vsp 54 | *.vspx 55 | 56 | # Guidance Automation Toolkit 57 | *.gpState 58 | 59 | # ReSharper is a .NET coding add-in 60 | _ReSharper* 61 | 62 | # NCrunch 63 | *.ncrunch* 64 | .*crunch*.local.xml 65 | 66 | # Installshield output folder 67 | [Ee]xpress 68 | 69 | # DocProject is a documentation generator add-in 70 | DocProject/buildhelp/ 71 | DocProject/Help/*.HxT 72 | DocProject/Help/*.HxC 73 | DocProject/Help/*.hhc 74 | DocProject/Help/*.hhk 75 | DocProject/Help/*.hhp 76 | DocProject/Help/Html2 77 | DocProject/Help/html 78 | 79 | # Click-Once directory 80 | publish 81 | 82 | # Publish Web Output 83 | *.Publish.xml 84 | 85 | # NuGet Packages Directory 86 | packages 87 | 88 | # Windows Azure Build Output 89 | csx 90 | *.build.csdef 91 | 92 | # Windows Store app package directory 93 | AppPackages/ 94 | 95 | # Others 96 | [Bb]in 97 | [Oo]bj 98 | sql 99 | TestResults 100 | [Tt]est[Rr]esult* 101 | *.Cache 102 | ClientBin 103 | [Ss]tyle[Cc]op.* 104 | ~$* 105 | *.dbmdl 106 | Generated_Code #added for RIA/Silverlight projects 107 | 108 | # Backup & report files from converting an old project file to a newer 109 | # Visual Studio version. Backup files are not needed, because we have git ;-) 110 | _UpgradeReport_Files/ 111 | Backup*/ 112 | UpgradeLog*.XML 113 | /src/.vs/Slack.Webhooks/v15/Server/sqlite3 114 | /.vs 115 | -------------------------------------------------------------------------------- /GitVersion.yml: -------------------------------------------------------------------------------- 1 | assembly-versioning-scheme: MajorMinorPatch 2 | mode: ContinuousDeployment 3 | branches: {} 4 | ignore: 5 | sha: [] 6 | merge-message-formats: {} 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Benn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slack.Webhooks ![Continuous Stuff](https://github.com/mrb0nj/Slack.Webhooks/workflows/Continuous%20Stuff/badge.svg?branch=master) [![NuGet Version](http://img.shields.io/nuget/v/Slack.Webhooks.svg?style=flat)](https://www.nuget.org/packages/Slack.Webhooks/) 2 | 3 | Even simpler integration with Slack's Incoming/Outgoing webhooks API for .net 4 | 5 | ### IMPORTANT 6 | 7 | On Feb 19th 2020 Slack will end support for TLS version 1.0 and 1.1. This means you may (depending on your .NET version) need to force the use of TLS1.2. 8 | 9 | If you receive an error stating that "The underlying connection was closed:" it's quite possibly a TLS issue. You can work around this by setting the default TLS version using the following: 10 | 11 | ```csharp 12 | System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 13 | ``` 14 | 15 | ### Change Log 16 | 17 | #### v1.1.4 18 | 19 | - Add Block elements to Attachments 20 | 21 | #### v1.1.3 22 | 23 | - Fixes potential deadlock issues 24 | 25 | #### v1.1.2 26 | 27 | - Fix SlackMessage.Clone does not clone all properties 28 | 29 | #### v1.1.0 30 | 31 | - Support Slack's Block Kit with SlackMessage.Blocks property 32 | - Support Thread replies with SlackMessage.ThreadId property 33 | - Changed the whole Emoji setup. SlackMessage.Emoji is now a string and Emoji.\* are constants. This shouldn't cause problems in the most part! 34 | - Allow HttpClient to be injected into SlackClient. 35 | - SlackClient implements IDisposable to match the contained HttpClient instance even though this isn't the recommended usage. SlackClient, like HttpClient, there should only be a single instance of this class within the lifecycle of your application, for more information on why see: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ 36 | 37 | ### v1.0.0 BREAKING CHANGES 38 | 39 | We no longer use RestSharp in favour of HttpClient - this however means that 40 | .NET 4.0 and below are no longer supported. 41 | 42 | Also, the PostAsync method signature has changed. The return type is now `Task` 43 | in place of `Task` which was tied directly to RestSharp. 44 | 45 | ## Outgoing Webhooks 46 | 47 | [Blog Post](https://mrb0nj.dev/2015/04/07/slack-outgoing-webhooks/) 48 | 49 | ## Incoming Webhooks 50 | 51 | Requirements: 52 | 53 | 1. You must first enable the Webhooks integration for your Slack Account to get the Token. You can enable it here: https://slack.com/services/new/incoming-webhook 54 | 2. Slack.Webhooks depends on JSON.net 55 | 3. Compatible with .NET 4.5+ and .NET Core. If you need .NET 3.5/4 you can use an older release, but this may be out of date. 56 | 57 | Download: 58 | 59 | Package is hosted on [Nuget](https://www.nuget.org/packages/Slack.Webhooks/) and can be installed from the package manager: 60 | 61 | ``` 62 | PM> Install-Package Slack.Webhooks 63 | ``` 64 | 65 | For older .NET framework support: 66 | 67 | ``` 68 | PM> Install-Package Slack.Webhooks -Version 0.1.8 69 | ``` 70 | 71 | Then, create a SlackClient with your Webhook URL. 72 | 73 | ```csharp 74 | var slackClient = new SlackClient("[YOUR_WEBHOOK_URL]"); 75 | ``` 76 | 77 | Create a new SlackMessage 78 | 79 | ```csharp 80 | var slackMessage = new SlackMessage 81 | { 82 | Channel = "#random", 83 | Text = "Your message", 84 | IconEmoji = Emoji.Ghost, 85 | Username = "nerdfury" 86 | }; 87 | ``` 88 | 89 | By default the text can contain Markdown but this default behaviour can be disabled: 90 | 91 | ```csharp 92 | slackMessage.Mrkdwn = false; 93 | ``` 94 | 95 | More info on message formatting can be found in the [Docs](https://api.slack.com/docs/formatting) 96 | 97 | Attachments can be added to a message: 98 | 99 | ```csharp 100 | var slackAttachment = new SlackAttachment 101 | { 102 | Fallback = "New open task [Urgent]: ", 103 | Text = "New open task *[Urgent]*: ", 104 | Color = "#D00000", 105 | Fields = 106 | new List 107 | { 108 | new SlackField 109 | { 110 | Title = "Notes", 111 | Value = "This is much *easier* than I thought it would be." 112 | } 113 | } 114 | }; 115 | slackMessage.Attachments = new List {slackAttachment}; 116 | ``` 117 | 118 | Please see the [Docs](https://api.slack.com/docs/attachments) for further info on attachments. 119 | 120 | You can also provide a list of `Block` objects in `SlackMessage.Blocks` to create more interactive content: 121 | 122 | `Blocks.Divider` 123 | 124 | ```csharp 125 | slackMessage.Blocks = new List 126 | { 127 | new Blocks.Divider() 128 | }; 129 | ``` 130 | 131 | `Blocks.Image` 132 | 133 | ```csharp 134 | slackMessage.Blocks = new List 135 | { 136 | new Blocks.Image 137 | { 138 | AltText = "Sexy Skyline", 139 | ImageUrl = "https://placekitten.com/500/500", 140 | Title = new TextObject("Hello, this is text") 141 | } 142 | }; 143 | ``` 144 | 145 | `Blocks.Context` 146 | 147 | ```csharp 148 | slackMessage.Blocks = new List 149 | { 150 | new Blocks.Context 151 | { 152 | Elements = new List 153 | { 154 | new TextObject("_markdown_") 155 | { 156 | Type = TextObject.TextType.Markdown 157 | }, 158 | new Elements.Image 159 | { 160 | ImageUrl = "https://placekitten.com/200/300", AltText = "Random Kitten" 161 | } 162 | } 163 | } 164 | }; 165 | ``` 166 | 167 | `Blocks.Section` 168 | 169 | ```csharp 170 | slackMessage.Blocks = new List 171 | { 172 | new Blocks.Section 173 | { 174 | Text = new TextObject("_markdown_") 175 | { 176 | Type = TextObject.TextType.Markdown 177 | }, 178 | Fields = new List 179 | { 180 | new TextObject { Text = "Field 1" }, 181 | new TextObject { Text = "Field 2" }, 182 | new TextObject { Text = "Field 3" } 183 | } 184 | } 185 | }; 186 | ``` 187 | 188 | `Blocks.Section` with `Accessory` 189 | 190 | ```csharp 191 | var confirmation = new Confirmation 192 | { 193 | Confirm = new TextObject("This OK?"), 194 | Text = new TextObject("This is the Text"), 195 | Deny = new TextObject("This is the Deny Text"), 196 | Title = new TextObject("Title") 197 | }; 198 | 199 | var options = new List