Vb.net 钉钉Webhook机器人

https://open.dingtalk.com/document/robots/custom-robot-access
添加Webhook机器人,我选择的安全方式是关键词==“消息”==

使用Newtonsoft

Imports System.Net.Http
Imports System.Text
Imports Newtonsoft.Json

Public Class Form1
    Async Function SendMarkdownMessage(ByVal webhookUrl As String, ByVal title As String, ByVal markdownContent As String) As Task
        Using client As New HttpClient()
            Dim payload = New With {
                .msgtype = "markdown",
                .markdown = New With {
                    .title = title,
                    .text = markdownContent
                }
            }

            Dim jsonPayload As String = JsonConvert.SerializeObject(payload)
            Dim content As New StringContent(jsonPayload, Encoding.UTF8, "application/json")

            Dim response As HttpResponseMessage = Await client.PostAsync(webhookUrl, content)

            If response.IsSuccessStatusCode Then
                Console.WriteLine("Markdown message sent successfully!")
            Else
                Console.WriteLine("Failed to send Markdown message. Status code: " & response.StatusCode)
            End If
        End Using
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim webhookUrl As String = "https://oapi.dingtalk.com/robot/send?access_token=你自己的token"
        Dim markdownContent As String = "# 消息Hello, Markdown!![pic](https://p1.ssl.qhimgs1.com/sdr/400__/t0196b58441267cc507.jpg)"
        Dim title As String = "Markdown Message"

        SendMarkdownMessage(webhookUrl, title, markdownContent).Wait()
    End Sub
End Class

最简单的机器人,效果如下,但是不能发送本地文件,需要换个方式
在这里插入图片描述

不用Newtonsoft


```vbnet
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim webhookUrl As String = "https://oapi.dingtalk.com/robot/send?access_token=96eb8d5ef7a660d2f360f81d8d79143dfa4f70a895bc383cc4e4e4a311f65dfc"

    Dim message As String = "# 消息Without NewtonSoft!![pic](https://p1.ssl.qhimgs1.com/sdr/400__/t0196b58441267cc507.jpg)"
    Dim postData As String = "{""msgtype"": ""markdown"", ""markdown"": {""title"": ""DingTalk Markdown Message"", ""text"": """ & message & """}}"

    Dim request As WebRequest = WebRequest.Create(webhookUrl)
    request.Method = "POST"
    request.ContentType = "application/json"
    Dim data As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentLength = data.Length

    Using stream As Stream = request.GetRequestStream()
        stream.Write(data, 0, data.Length)
    End Using

    Dim response As WebResponse = request.GetResponse()
    Using streamReader As New StreamReader(response.GetResponseStream())
        Dim result As String = streamReader.ReadToEnd()
        Debug.Print(result)
    End Using

    Console.ReadLine()
End Sub

改用加签发方式

Imports System.Net.Http
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1


    Async Function SendMarkdownMessage(webhookUrl As String, accessToken As String, secret As String, title As String, markdownContent As String) As Task
        Dim timestamp As Long = DateTimeOffset.Now.ToUnixTimeMilliseconds()
        Dim sign As String = ComputeSignature(secret, timestamp)

        Dim url As String = $"{webhookUrl}?access_token={accessToken}&timestamp={timestamp}&sign={sign}"

        Using client As New HttpClient()
            Dim payload = New With {
                .msgtype = "markdown",
                .markdown = New With {
                    .title = title,
                    .text = markdownContent
                }
            }

            Dim jsonPayload As String = Newtonsoft.Json.JsonConvert.SerializeObject(payload)
            Dim content As New StringContent(jsonPayload, Encoding.UTF8, "application/json")

            Dim response As HttpResponseMessage = Await client.PostAsync(url, content)

            If response.IsSuccessStatusCode Then
                Console.WriteLine("Markdown message sent successfully!")
            Else
                Console.WriteLine("Failed to send Markdown message. Status code: " & response.StatusCode)
            End If
        End Using
    End Function

    Function ComputeSignature(secret As String, timestamp As Long) As String
        Dim stringToSign As String = $"{timestamp}{vbLf}{secret}"
        Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secret)
        Dim stringToSignBytes As Byte() = Encoding.UTF8.GetBytes(stringToSign)

        Using hmac As New HMACSHA256(secretBytes)
            Dim hashBytes As Byte() = hmac.ComputeHash(stringToSignBytes)
            Return Convert.ToBase64String(hashBytes)
        End Using
    End Function

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim webhookUrl As String = "https://oapi.dingtalk.com/robot/send"
        Dim accessToken As String = "你的token"
        Dim secret As String = "你设置加签后的密钥"

        Dim markdownContent As String = "# VB.NET11dian使用加签无拘无束!![pic](https://p5.ssl.qhimgs1.com/sdr/400__/t01052dd2d43d8ee815.png)"
        Dim title As String = "Markdown Message"

        Await SendMarkdownMessage(webhookUrl, accessToken, secret, title, markdownContent)
    End Sub

End Class

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值