Skip to content

Instantly share code, notes, and snippets.

@elpatron68
Last active April 3, 2020 06:59
Show Gist options
  • Save elpatron68/19e8741c681b26bf5317 to your computer and use it in GitHub Desktop.
Save elpatron68/19e8741c681b26bf5317 to your computer and use it in GitHub Desktop.
Posts Text to a defined Mattermost webhook (VB.NET)
Imports System.IO
Imports System.Text
Imports System.Net
Public Class mattermost
''' <summary>
''' Posts Text to a defined Mattermost webhook
''' </summary>
''' <param name="sWebhookURL"></param>
''' <param name="sText"></param>
''' <param name="sUser"></param>
''' <param name="sChannel"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function _post2Mattermost(ByVal sWebhookURL As Uri, ByVal sText As String, _
ByVal sUser As String, ByVal sChannel As String) As String
Dim jsonString As String = String.Format("{{""username"": ""{0}"", ""text"": ""{1}"", " + _
"""channel"": ""{2}""}}", sUser, sText, sChannel)
ServicePointManager.ServerCertificateValidationCallback = Function() True
Dim data = Encoding.UTF8.GetBytes(jsonString)
Dim req As WebRequest = WebRequest.Create(sWebhookURL)
req.ContentType = "application/json"
req.Method = "POST"
req.ContentLength = data.Length
Using requestWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
requestWriter.Write(jsonString)
End Using
Dim response = req.GetResponse().GetResponseStream()
Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
End Class
Imports mattermost_webhook_connector.net
Module Module1
Sub Main()
Dim res As String = mattermost._post2Mattermost(New Uri("https://<your webhook URL goes here>"), _
"Hey dude", "god of dotnet", "")
Debug.WriteLine("Response: " + res)
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment