-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessagingLib.vb
More file actions
254 lines (212 loc) · 8.56 KB
/
MessagingLib.vb
File metadata and controls
254 lines (212 loc) · 8.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
Imports System
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module MessagingLib
Class DefaultAgent
Public osPlatform As String
Public sdkVersion As String
End Class
Class Message
Public type As String
Public [to] As String
Public [from] As String
Public subject As String
Public text As String
Public imageId As String
Public kakaoOptions As KakaoOptions
End Class
Class Messages
Public messages As New List(Of Message)
Public agent As DefaultAgent = New DefaultAgent() With {
.osPlatform = Environment.OSVersion.VersionString,
.sdkVersion = "vb.net/1.0.1"
}
Public allowDuplicates As Boolean = false
Sub Add(message As Message)
messages.Add(message)
End Sub
End Class
Class MessagesDetail
Public messages As New List(Of Message)
Public agent As DefaultAgent = New DefaultAgent() With {
.osPlatform = Environment.OSVersion.VersionString,
.sdkVersion = "vb.net/1.0.1"
}
Public allowDuplicates As Boolean = false
Public scheduledDate = Nothing
Sub Add(message As Message)
messages.Add(message)
End Sub
End Class
Class GroupInfo
Public appId As String
Public strict As Boolean
Public sdkVersion As String
Public osPlatform As String
End Class
Class Image
Public type As String
Public file As String
Public name As String
Public link As String
End Class
Class KakaoButton
Public buttonType As String
Public buttonName As String
Public linkMo As String
Public linkPc As String
Public linkAnd As String
Public linkIos As String
End Class
Class KakaoOptions
Public pfId As String
Public templateId As String
Public disableSms As Boolean
Public imageId As String
Public buttons As KakaoButton()
Public variables As Dictionary(Of String, String)
End Class
Class Response
Public StatusCode As Net.HttpStatusCode
Public ErrorCode As String
Public ErrorMessage As String
Public Data As JObject
End Class
Dim _jsonSettings As JsonSerializerSettings = New JsonSerializerSettings() With {
.NullValueHandling = NullValueHandling.Ignore
}
Function GetSignature(apiKey As String, data As String, apiSecret As String)
Dim sha256 As New Security.Cryptography.HMACSHA256(Text.Encoding.UTF8.GetBytes(apiSecret))
Dim hashValue As Byte() = sha256.ComputeHash(Text.Encoding.UTF8.GetBytes(data))
Dim hash As String = Replace(BitConverter.ToString(hashValue), "-", "")
Return hash.ToLower
End Function
Function GetSalt(ByVal Optional len As Integer = 32)
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim r As New Random
Dim sb As New Text.StringBuilder
For i As Integer = 1 To len
Dim idx As Integer = r.Next(0, 35)
sb.Append(s.Substring(idx, 1))
Next
Return sb.ToString()
End Function
Function GetAuth(apiKey As String, apiSecret As String)
Dim salt As String = GetSalt()
Dim dateStr As String = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
Dim data As String = dateStr & salt
Return _
"HMAC-SHA256 apiKey=" & apiKey & ", date=" & dateStr & ", salt=" & salt & ", signature=" &
GetSignature(apiKey, data, apiSecret)
End Function
Function GetUrl(path)
Return protocol & "://" & domain & prefix & path
End Function
Function Request(path As String, method As String, Optional data As String = vbNullString)
Dim auth As String = GetAuth(apiKey, apiSecret)
Try
Dim req As Net.WebRequest = Net.WebRequest.Create(GetUrl(path))
req.Method = method
req.Headers.Add("Authorization", auth)
req.Headers.Add("Content-type", "application/json; charset=utf-8")
If Not String.IsNullOrEmpty(data) Then
Using writer = New IO.StreamWriter(req.GetRequestStream())
writer.Write(data)
writer.Close()
End Using
End If
Using response As Net.WebResponse = req.GetResponse()
Using streamReader = New IO.StreamReader(response.GetResponseStream())
Dim jsonResponseText = streamReader.ReadToEnd()
Dim jsonObj As JObject = JObject.Parse(jsonResponseText)
Return New Response() With {
.StatusCode = Net.HttpStatusCode.OK,
.Data = jsonObj,
.ErrorCode = vbNull,
.ErrorMessage = vbNull
}
End Using
End Using
Catch ex As Net.WebException
Using streamReader = New IO.StreamReader(ex.Response.GetResponseStream())
Dim jsonResponseText = streamReader.ReadToEnd()
Dim jsonObj As JObject = JObject.Parse(jsonResponseText)
Dim ErrorCode As String = jsonObj.SelectToken("errorCode")
Dim ErrorMessage As String = jsonObj.SelectToken("errorMessage")
Dim httpResp = DirectCast(ex.Response, Net.HttpWebResponse)
Return New Response() With {
.StatusCode = httpResp.StatusCode,
.Data = jsonObj,
.ErrorCode = ErrorCode,
.ErrorMessage = ErrorMessage
}
End Using
Catch ex As Exception
Dim ErrorCode = "Unknown Exception"
Dim ErrorMessage As String = ex.Message
Return New Response() With {
.StatusCode = vbNull,
.Data = jObject.Parse("{}"),
.ErrorCode = ErrorCode,
.ErrorMessage = ErrorMessage
}
End Try
End Function
Class Group
Dim groupId As String
Public Sub New()
' Nothing
End Sub
Public Sub New(groupId As String)
Me.groupId = groupId
End Sub
Function AddMessages(msgs As Messages)
If String.IsNullOrEmpty(groupId) Then
Throw New System.Exception("그룹아이디가 설정되지 않았습니다.")
End If
Return Request("/messages/v4/groups/" & groupId & "/messages", "POST",
JsonConvert.SerializeObject(msgs, Formatting.None, _jsonSettings))
End Function
Function Create()
Dim groupInfo As GroupInfo = New GroupInfo() With {
.osPlatform = Environment.OSVersion.VersionString,
.sdkVersion = "vb.net/1.0.1"
}
Return Request("/messages/v4/groups", "POST",
JsonConvert.SerializeObject(groupInfo, Formatting.None, _jsonSettings))
End Function
Function GetList()
Return Request("/messages/v4/groups", "GET")
End Function
End Class
Function SendMessagesDetail(messages As MessagesDetail)
Return Request("/messages/v4/send-many/detail", "POST",
JsonConvert.SerializeObject(messages, Formatting.None, _jsonSettings))
End Function
Function SendMessages(messages As Messages)
Return Request("/messages/v4/send-many", "POST",
JsonConvert.SerializeObject(messages, Formatting.None, _jsonSettings))
End Function
Function UploadImage(path As String)
Dim bytes As Byte() = IO.File.ReadAllBytes(path)
Dim img As Image = New Image()
img.type = "MMS"
img.file = Convert.ToBase64String(bytes)
Return Request("/storage/v1/files", "POST", JsonConvert.SerializeObject(img, Formatting.None, _jsonSettings))
End Function
Function UploadKakaoImage(path As String, url As String)
Dim bytes As Byte() = IO.File.ReadAllBytes(path)
Dim img As Image = New Image()
img.type = "KAKAO"
img.file = Convert.ToBase64String(bytes)
img.name = IO.Path.GetFileName(path)
img.link = url
Return Request("/storage/v1/files", "POST", JsonConvert.SerializeObject(img, Formatting.None, _jsonSettings))
End Function
Function GetBalance()
Return Request("/cash/v1/balance", "GET")
End Function
Sub GetGroupList()
Request("/messages/v4/groups", "GET")
End Sub
End Module