Repository navigation
Expand file tree
/
Copy pathDingTalkBot.vb
More file actions
113 lines (102 loc) · 4.17 KB
/
Copy pathDingTalkBot.vb
File metadata and controls
113 lines (102 loc) · 4.17 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
' 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.Net.Http
Imports System.Text
Imports System.Text.Json
Imports PawLab.DingTalkBot.Utilities
Public NotInheritable Class DingTalkBot
#Region "构造函数"
Private Shared ReadOnly HttpClient As New HttpClient()
Private ReadOnly _accessToken As String
Private ReadOnly _secret As String
''' <summary>
''' 新建一个钉钉机器人实例
''' </summary>
''' <param name="accessToken">Access Token值</param>
''' <param name="secret">签名密钥值</param>
Public Sub New(accessToken As String, secret As String)
If String.IsNullOrEmpty(accessToken) Then
Throw New ArgumentNullException(NameOf(accessToken))
End If
If String.IsNullOrEmpty(secret) Then
Throw New ArgumentNullException(NameOf(secret))
End If
_accessToken = accessToken
_secret = secret
End Sub
#End Region
#Region "基础用法"
''' <summary>
''' 发送一条消息
''' </summary>
''' <param name="msg">消息文本</param>
''' <param name="isAtAll">(可选)是否@全体成员, 若是, 请传True, 默认为False</param>
''' <returns>执行结果</returns>
Public Function Send(msg As String, Optional isAtAll As Boolean = False) As ResponseResult
Return SendInternal(msg, isAtAll, Nothing, Nothing)
End Function
''' <summary>
''' 发送一条消息
''' </summary>
''' <param name="msg">消息文本</param>
''' <param name="atUserID">(可选)被@的用户ID</param>
''' <param name="atUserMobile">(可选)被@的用户手机号码</param>
''' <returns>执行结果</returns>
Public Function Send(msg As String, Optional atUserID As String() = Nothing, Optional atUserMobile As String() = Nothing) As ResponseResult
Return SendInternal(msg, False, atUserID, atUserMobile)
End Function
#End Region
#Region "辅助函数"
Private Function SendInternal(msg As String, isAtAll As Boolean,
atUserIds As String(), atMobiles As String()) As ResponseResult
Dim url As String = GenerateSignatureUrl(_accessToken, _secret)
'组装请求体
Dim atDict As New Dictionary(Of String, Object) From {
{"isAtAll", isAtAll},
{"atUserIds", If(atUserIds, Array.Empty(Of String)())},
{"atMobiles", If(atMobiles, Array.Empty(Of String)())}
}
Dim textDict As New Dictionary(Of String, Object) From {
{"content", msg}
}
Dim body As New Dictionary(Of String, Object) From {
{"at", atDict},
{"text", textDict},
{"msgtype", "text"}
}
Dim json As String = JsonSerializer.Serialize(body)
Using content As New StringContent(json, Encoding.UTF8, "application/json")
'同步等待异步 API,网络错误会在此抛出 HttpRequestException
Dim response As HttpResponseMessage = HttpClient.PostAsync(url, content).GetAwaiter().GetResult()
response.EnsureSuccessStatusCode() '非 2xx 状态码抛 HttpRequestException
Dim respJson As String = response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
Return ParseResponse(respJson)
End Using
End Function
''' <summary>
''' 响应请求
''' </summary>
Public Structure ResponseResult
''' <summary>
''' 错误代码
''' </summary>
Public errCode As Integer
''' <summary>
''' 错误消息内容
''' </summary>
Public errMsg As String
End Structure
#End Region
End Class