Repository navigation
Expand file tree
/
Copy pathUtilities.vb
More file actions
58 lines (55 loc) · 2.56 KB
/
Copy pathUtilities.vb
File metadata and controls
58 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
' DingTalkBot - A DingTalk Webhook Bot Library
' Copyright 2026 xionglongztz/PawLaboratory
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
' http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
Imports System.Security.Cryptography
Imports System.Text
Imports System.Text.Json
Imports System.Text.Json.Serialization
Friend Class Utilities
''' <summary>
''' 生成带签名的 URL 地址
''' </summary>
''' <param name="accessToken">Webhook 签名</param>
''' <param name="secret">密钥</param>
''' <returns>构造好的URL</returns>
Public Shared Function GenerateSignatureUrl(accessToken As String, secret As String) As String
Dim timestamp As Long = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() '获取当前 Unix 时间戳
Dim stringToSign As String = timestamp.ToString() & vbLf & secret '构建待签名字符串:timestamp + "\n" + secret
Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secret) '转换为 UTF-8 字节数组
Dim stringToSignBytes As Byte() = Encoding.UTF8.GetBytes(stringToSign)
Using hmac As New HMACSHA256(secretBytes) '使用 HMACSHA256 计算签名
Dim signBytes As Byte() = hmac.ComputeHash(stringToSignBytes)
Dim base64 As String = Convert.ToBase64String(signBytes) 'Base64 编码
Dim sign As String = Uri.EscapeDataString(base64) 'URL 编码
Dim result As String = $"https://oapi.dingtalk.com/robot/send?access_token={accessToken}&sign={sign}×tamp={timestamp}" '构造URL
Return result
End Using
End Function
''' <summary>
''' DTO
''' </summary>
Private Class DingResponse
<JsonPropertyName("errcode")>
Public Property ErrCode As Integer
<JsonPropertyName("errmsg")>
Public Property ErrMsg As String
End Class
Public Shared Function ParseResponse(respJson As String) As DingTalkBot.ResponseResult
Dim dto As DingResponse = JsonSerializer.Deserialize(Of DingResponse)(respJson)
Return New DingTalkBot.ResponseResult With {
.errCode = dto.ErrCode,
.errMsg = dto.ErrMsg
}
End Function
End Class