sendgo 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +288 -0
- data/lib/sendgo/alimtalk.rb +30 -0
- data/lib/sendgo/client.rb +28 -0
- data/lib/sendgo/error.rb +25 -0
- data/lib/sendgo/friendtalk.rb +29 -0
- data/lib/sendgo/http_client.rb +54 -0
- data/lib/sendgo/sms.rb +28 -0
- data/lib/sendgo/token_manager.rb +68 -0
- data/lib/sendgo/version.rb +3 -0
- data/lib/sendgo.rb +26 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a89b23db406e7ccf7ab1fe29fe9cfb2ddb5508cc7ce0367731cdbb5e3153c01f
|
|
4
|
+
data.tar.gz: bc4763817d9e59830e69e6547d406240b910727476c676cac88233fc431ee0cc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5eb5c76e420bf0f4fb5e6b73db72480c5859077ff3b6288be19a89dec37a5275f7589d7f92a8a3ada0d36bb065e1c2cf477d8613e82468bfb5e1d2bd7ac19bd1
|
|
7
|
+
data.tar.gz: 428977e7d3d037a17cf20b7c22cb4785cce578abd1766fa5db76a6c1fdb658e33181e213d589f9e6067002c2ac3525c1b7ccb2e6ef836440158dc7cddd8beca2
|
data/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# sendgo
|
|
2
|
+
|
|
3
|
+
> **Ruby에서 카카오 알림톡, 친구톡, SMS를 가장 쉽게 발송하는 공식 Ruby SDK**
|
|
4
|
+
|
|
5
|
+
[](https://rubygems.org/gems/sendgo)
|
|
6
|
+
[](https://ruby-lang.org)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
`sendgo`는 [Sendgo](https://sendgo.io) 알림 API를 위한 공식 Ruby SDK입니다.
|
|
10
|
+
**표준 라이브러리만 사용**하며, MonitorMixin 기반 스레드 안전 토큰 관리를 제공합니다.
|
|
11
|
+
Rails, Sinatra, Hanami 등 모든 Ruby 환경에서 사용할 수 있습니다.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 설치
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install sendgo
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
또는 Gemfile:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem 'sendgo', '~> 1.0'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 빠른 시작
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require 'sendgo'
|
|
33
|
+
|
|
34
|
+
client = Sendgo::Client.new(
|
|
35
|
+
access_key: ENV['SENDGO_ACCESS_KEY'],
|
|
36
|
+
secret_key: ENV['SENDGO_SECRET_KEY'],
|
|
37
|
+
kakao_sender_key: ENV['SENDGO_KAKAO_SENDER_KEY'],
|
|
38
|
+
sms_sender_key: ENV['SENDGO_SMS_SENDER_KEY'],
|
|
39
|
+
api_version: 'v2'
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# 알림톡 발송
|
|
43
|
+
client.alimtalk.send(
|
|
44
|
+
template_code: 'ORDER_CONFIRM_001',
|
|
45
|
+
contacts: [
|
|
46
|
+
{ contact: '01012345678', name: '홍길동', var1: 'ORD-001', var2: '29,000원' }
|
|
47
|
+
]
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 알림톡 상세 사용법
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
require 'sendgo'
|
|
57
|
+
|
|
58
|
+
client = Sendgo::Client.new(
|
|
59
|
+
access_key: ENV['SENDGO_ACCESS_KEY'],
|
|
60
|
+
secret_key: ENV['SENDGO_SECRET_KEY'],
|
|
61
|
+
kakao_sender_key: ENV['SENDGO_KAKAO_SENDER_KEY'],
|
|
62
|
+
sms_sender_key: ENV['SENDGO_SMS_SENDER_KEY'],
|
|
63
|
+
api_version: 'v2'
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# 다건 발송
|
|
67
|
+
client.alimtalk.send(
|
|
68
|
+
template_code: 'ORDER_CONFIRM_001',
|
|
69
|
+
contacts: [
|
|
70
|
+
{ contact: '01011111111', name: '홍길동', var1: 'ORD-001', var2: '29,000원' },
|
|
71
|
+
{ contact: '01022222222', name: '김철수', var1: 'ORD-002', var2: '15,000원' },
|
|
72
|
+
{ contact: '01033333333', name: '이영희', var1: 'ORD-003', var2: '52,000원' }
|
|
73
|
+
]
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# 예약 발송
|
|
77
|
+
client.alimtalk.send(
|
|
78
|
+
template_code: 'PROMO_SUMMER_2026',
|
|
79
|
+
schedule_type: 'SCHEDULED',
|
|
80
|
+
at: '2026-07-28 09:00:00',
|
|
81
|
+
contacts: [
|
|
82
|
+
{ contact: '01012345678', var1: '여름 한정 50% 할인' }
|
|
83
|
+
]
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# SMS 자동 대체 발송
|
|
87
|
+
client.alimtalk.send(
|
|
88
|
+
template_code: 'DELIVERY_START_001',
|
|
89
|
+
replace_sms: 'Y',
|
|
90
|
+
sms_subject: '[배송 시작 안내]',
|
|
91
|
+
sms_content: "주문하신 상품이 출고되었습니다.\n송장번호: \#{var2}",
|
|
92
|
+
contacts: [
|
|
93
|
+
{ contact: '01012345678', var1: 'ORD-001', var2: '1234567890' }
|
|
94
|
+
]
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## 친구톡 사용법
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
# 텍스트형
|
|
104
|
+
client.friendtalk.send(
|
|
105
|
+
content: '안녕하세요! 7월 한정 특가 이벤트를 확인해보세요.',
|
|
106
|
+
contacts: [{ contact: '01012345678' }]
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# 이미지형
|
|
110
|
+
client.friendtalk.send(
|
|
111
|
+
message_type: 'FI',
|
|
112
|
+
content: '이번 주 특가 상품을 확인하세요!',
|
|
113
|
+
image_url: 'https://cdn.example.com/banner.jpg',
|
|
114
|
+
image_link: 'https://example.com/event',
|
|
115
|
+
contacts: [{ contact: '01012345678' }]
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# 버튼 포함
|
|
119
|
+
client.friendtalk.send(
|
|
120
|
+
content: '7월 쿠폰이 도착했습니다! 지금 바로 사용하세요.',
|
|
121
|
+
buttons: [{ name: '쿠폰 받기', type: 'WL', link_mo: 'https://example.com/coupon' }],
|
|
122
|
+
contacts: [{ contact: '01012345678' }]
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## SMS / LMS / MMS 사용법
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
# SMS (90자 이하)
|
|
132
|
+
client.sms.send_sms(
|
|
133
|
+
content: '[Sendgo] 인증번호: 123456 (5분 이내 입력)',
|
|
134
|
+
contacts: [{ contact: '01012345678' }]
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# LMS (장문)
|
|
138
|
+
client.sms.send_lms(
|
|
139
|
+
subject: '[중요] 서비스 점검 안내',
|
|
140
|
+
content: "안녕하세요. 서비스 점검이 예정되어 있습니다.\n■ 일시: 2026-07-25 02:00 ~ 06:00",
|
|
141
|
+
contacts: [{ contact: '01012345678' }]
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# MMS (이미지 포함)
|
|
145
|
+
client.sms.send_mms(
|
|
146
|
+
subject: '[이벤트] 7월 특가',
|
|
147
|
+
content: '이번 달 특가 상품을 확인하세요!',
|
|
148
|
+
contacts: [{ contact: '01011111111' }, { contact: '01022222222' }]
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Rails 통합
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
# config/initializers/sendgo.rb
|
|
158
|
+
SENDGO_CLIENT = Sendgo::Client.new(
|
|
159
|
+
access_key: ENV['SENDGO_ACCESS_KEY'],
|
|
160
|
+
secret_key: ENV['SENDGO_SECRET_KEY'],
|
|
161
|
+
kakao_sender_key: ENV['SENDGO_KAKAO_SENDER_KEY'],
|
|
162
|
+
sms_sender_key: ENV['SENDGO_SMS_SENDER_KEY'],
|
|
163
|
+
api_version: 'v2'
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# app/services/notification_service.rb
|
|
167
|
+
class NotificationService
|
|
168
|
+
def initialize(client = SENDGO_CLIENT)
|
|
169
|
+
@client = client
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def send_order_confirm(phone:, order_no:, amount:)
|
|
173
|
+
@client.alimtalk.send(
|
|
174
|
+
template_code: 'ORDER_CONFIRM_001',
|
|
175
|
+
contacts: [{ contact: phone, var1: order_no, var2: amount }]
|
|
176
|
+
)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def send_verification_code(phone:, code:)
|
|
180
|
+
@client.alimtalk.send(
|
|
181
|
+
template_code: 'VERIFY_CODE_001',
|
|
182
|
+
replace_sms: 'Y',
|
|
183
|
+
sms_content: "[인증] 인증번호: #{code} (5분 이내 입력)",
|
|
184
|
+
contacts: [{ contact: phone, var1: code }]
|
|
185
|
+
)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# app/models/order.rb
|
|
190
|
+
class Order < ApplicationRecord
|
|
191
|
+
after_create :send_confirmation
|
|
192
|
+
|
|
193
|
+
private
|
|
194
|
+
|
|
195
|
+
def send_confirmation
|
|
196
|
+
NotificationService.new.send_order_confirm(
|
|
197
|
+
phone: user.phone,
|
|
198
|
+
order_no: number,
|
|
199
|
+
amount: "#{total.to_i.to_s(:delimited)}원"
|
|
200
|
+
)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Sidekiq 비동기 발송
|
|
206
|
+
|
|
207
|
+
```ruby
|
|
208
|
+
# app/workers/alimtalk_worker.rb
|
|
209
|
+
class AlimtalkWorker
|
|
210
|
+
include Sidekiq::Worker
|
|
211
|
+
sidekiq_options retry: 3
|
|
212
|
+
|
|
213
|
+
def perform(template_code, contacts)
|
|
214
|
+
SENDGO_CLIENT.alimtalk.send(
|
|
215
|
+
template_code: template_code,
|
|
216
|
+
contacts: contacts
|
|
217
|
+
)
|
|
218
|
+
rescue Sendgo::Error => e
|
|
219
|
+
logger.error "알림톡 발송 실패: #{e.message} [#{e.error_code}]"
|
|
220
|
+
raise e unless %w[INVALID_TEMPLATE_CODE PAYMENT_REQUIRED].include?(e.error_code)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# 사용
|
|
225
|
+
AlimtalkWorker.perform_async('ORDER_CONFIRM_001', [
|
|
226
|
+
{ 'contact' => '01012345678', 'var1' => 'ORD-001' }
|
|
227
|
+
])
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## 예외 처리
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
require 'sendgo'
|
|
236
|
+
|
|
237
|
+
begin
|
|
238
|
+
client.alimtalk.send(template_code: 'ORDER_CONFIRM_001', contacts: [...])
|
|
239
|
+
rescue Sendgo::Error => e
|
|
240
|
+
puts "발송 실패: HTTP #{e.status_code} [#{e.error_code}]"
|
|
241
|
+
|
|
242
|
+
case e.error_code
|
|
243
|
+
when 'INVALID_ACCESS_KEY', 'INVALID_SECRET_KEY'
|
|
244
|
+
alert_ops('Sendgo 인증키를 확인하세요.')
|
|
245
|
+
when 'INVALID_TEMPLATE_CODE'
|
|
246
|
+
Rails.logger.warn("존재하지 않는 템플릿: #{e.message}")
|
|
247
|
+
when 'PAYMENT_REQUIRED'
|
|
248
|
+
alert_ops('Sendgo 크레딧이 부족합니다.')
|
|
249
|
+
when 'IP_NOT_ALLOWED'
|
|
250
|
+
alert_ops('허용되지 않은 IP')
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## 설정 옵션
|
|
258
|
+
|
|
259
|
+
| 파라미터 | 타입 | 필수 | 기본값 | 설명 |
|
|
260
|
+
|---------|------|------|--------|------|
|
|
261
|
+
| `access_key` | `String` | **필수** | — | Sendgo 액세스 키 |
|
|
262
|
+
| `secret_key` | `String` | **필수** | — | Sendgo 시크릿 키 |
|
|
263
|
+
| `kakao_sender_key` | `String` | 선택 | `nil` | 카카오 발신프로필 키 |
|
|
264
|
+
| `sms_sender_key` | `String` | 선택 | `nil` | SMS 발신자 키 |
|
|
265
|
+
| `api_version` | `String` | 선택 | `'v1'` | API 버전 (`v1` \| `v2`) |
|
|
266
|
+
| `base_url` | `String` | 선택 | `'https://sendgo.io'` | API 기본 URL |
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## 관련 패키지
|
|
271
|
+
|
|
272
|
+
| 언어/프레임워크 | 패키지 | GitHub |
|
|
273
|
+
|----------------|--------|--------|
|
|
274
|
+
| Spring Boot | `io.sendgo:sendgo-spring` | [spring](https://github.com/send-go/spring) |
|
|
275
|
+
| Node.js | `@sendgo/node` | [node](https://github.com/send-go/node) |
|
|
276
|
+
| Python | `sendgo-python` | [python](https://github.com/send-go/python) |
|
|
277
|
+
| PHP | `sendgo/php` | [php](https://github.com/send-go/php) |
|
|
278
|
+
| 전체 목록 | — | [send-go GitHub 조직](https://github.com/send-go) |
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 라이선스
|
|
283
|
+
|
|
284
|
+
MIT License © 2026 [Sendgo](https://sendgo.io)
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
*키워드: 카카오 알림톡 Ruby, 카카오 친구톡 Rails, SMS 발송 Ruby, 알림톡 Ruby gem, Ruby 카카오 API 연동, Sendgo Ruby SDK, Rails 알림 발송*
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module Sendgo
|
|
2
|
+
# 카카오 알림톡 전송 서비스.
|
|
3
|
+
#
|
|
4
|
+
# @example
|
|
5
|
+
# client.alimtalk.send(
|
|
6
|
+
# template_code: "ORDER_CONFIRM_001",
|
|
7
|
+
# contacts: [{ contact: "01012345678", var1: "ORD-001" }]
|
|
8
|
+
# )
|
|
9
|
+
class AlimtalkService
|
|
10
|
+
def initialize(http:, kakao_sender_key:, sms_sender_key:)
|
|
11
|
+
@http = http
|
|
12
|
+
@kakao_sender_key = kakao_sender_key
|
|
13
|
+
@sms_sender_key = sms_sender_key
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def send(template_code:, contacts:, schedule_type: "DIRECTLY", at: nil,
|
|
17
|
+
replace_sms: "N", sms_subject: nil, sms_content: nil)
|
|
18
|
+
body = {
|
|
19
|
+
at: at, scheduleType: schedule_type, templateCode: template_code,
|
|
20
|
+
replaceSms: replace_sms,
|
|
21
|
+
smsSubject: replace_sms == "Y" ? sms_subject : nil,
|
|
22
|
+
smsContent: replace_sms == "Y" ? sms_content : nil,
|
|
23
|
+
contacts: contacts,
|
|
24
|
+
kakaoSenderKey: @kakao_sender_key,
|
|
25
|
+
senderKey: @sms_sender_key,
|
|
26
|
+
}
|
|
27
|
+
@http.post("notices/send", body)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Sendgo
|
|
2
|
+
# Sendgo Ruby SDK 메인 클라이언트.
|
|
3
|
+
#
|
|
4
|
+
# @example
|
|
5
|
+
# client = Sendgo::Client.new(
|
|
6
|
+
# access_key: ENV["SENDGO_ACCESS_KEY"],
|
|
7
|
+
# secret_key: ENV["SENDGO_SECRET_KEY"],
|
|
8
|
+
# kakao_sender_key: ENV["SENDGO_KAKAO_KEY"],
|
|
9
|
+
# sms_sender_key: ENV["SENDGO_SMS_KEY"],
|
|
10
|
+
# api_version: "v2"
|
|
11
|
+
# )
|
|
12
|
+
class Client
|
|
13
|
+
attr_reader :alimtalk, :friendtalk, :sms
|
|
14
|
+
|
|
15
|
+
def initialize(access_key:, secret_key:, kakao_sender_key: nil, sms_sender_key: nil,
|
|
16
|
+
api_version: "v1", base_url: "https://sendgo.io")
|
|
17
|
+
raise ArgumentError, "access_key와 secret_key는 필수입니다" if access_key.nil? || secret_key.nil?
|
|
18
|
+
|
|
19
|
+
token_manager = TokenManager.new(base_url: base_url, access_key: access_key,
|
|
20
|
+
secret_key: secret_key, api_version: api_version)
|
|
21
|
+
http = HttpClient.new(token_manager: token_manager, base_url: base_url, api_version: api_version)
|
|
22
|
+
|
|
23
|
+
@alimtalk = AlimtalkService.new(http: http, kakao_sender_key: kakao_sender_key, sms_sender_key: sms_sender_key)
|
|
24
|
+
@friendtalk = FriendtalkService.new(http: http, kakao_sender_key: kakao_sender_key, sms_sender_key: sms_sender_key)
|
|
25
|
+
@sms = SmsService.new(http: http, sms_sender_key: sms_sender_key)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/sendgo/error.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Sendgo
|
|
2
|
+
# Sendgo API 호출 실패 시 발생하는 에러.
|
|
3
|
+
class SendgoError < StandardError
|
|
4
|
+
attr_reader :status_code, :error_code, :endpoint, :api_version, :response_body
|
|
5
|
+
|
|
6
|
+
def initialize(message, status_code: 0, error_code: nil, endpoint: "", api_version: "", response_body: {})
|
|
7
|
+
super(message)
|
|
8
|
+
@status_code = status_code
|
|
9
|
+
@error_code = error_code
|
|
10
|
+
@endpoint = endpoint
|
|
11
|
+
@api_version = api_version
|
|
12
|
+
@response_body = response_body
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.from_response(status, body, endpoint, api_version)
|
|
16
|
+
error_code = body["code"]
|
|
17
|
+
message = body["message"] || "Unknown error"
|
|
18
|
+
msg = "HTTP #{status}"
|
|
19
|
+
msg += " [#{error_code}]" if error_code
|
|
20
|
+
msg += " #{message}"
|
|
21
|
+
new(msg, status_code: status, error_code: error_code, endpoint: endpoint,
|
|
22
|
+
api_version: api_version, response_body: body)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Sendgo
|
|
2
|
+
# 카카오 친구톡 전송 서비스.
|
|
3
|
+
class FriendtalkService
|
|
4
|
+
def initialize(http:, kakao_sender_key:, sms_sender_key:)
|
|
5
|
+
@http = http
|
|
6
|
+
@kakao_sender_key = kakao_sender_key
|
|
7
|
+
@sms_sender_key = sms_sender_key
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def send(content:, contacts:, message_type: "FT", schedule_type: "DIRECTLY",
|
|
11
|
+
at: nil, buttons: [], image_url: nil, image_link: nil,
|
|
12
|
+
ad_flag: "Y", wide: "N", adult: "N", header: nil,
|
|
13
|
+
replace_sms: "N", sms_subject: nil, sms_content: nil)
|
|
14
|
+
body = {
|
|
15
|
+
at: at, scheduleType: schedule_type, messageType: message_type,
|
|
16
|
+
content: content, buttons: buttons, image: nil,
|
|
17
|
+
imageUrl: image_url, imageLink: image_link,
|
|
18
|
+
adFlag: ad_flag, wide: wide, adult: adult, header: header,
|
|
19
|
+
replaceSms: replace_sms,
|
|
20
|
+
smsSubject: replace_sms == "Y" ? sms_subject : nil,
|
|
21
|
+
smsContent: replace_sms == "Y" ? sms_content : nil,
|
|
22
|
+
contacts: contacts,
|
|
23
|
+
kakaoSenderKey: @kakao_sender_key,
|
|
24
|
+
senderKey: @sms_sender_key,
|
|
25
|
+
}
|
|
26
|
+
@http.post("friends/send", body)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
require "base64"
|
|
4
|
+
|
|
5
|
+
module Sendgo
|
|
6
|
+
# Sendgo API HTTP 클라이언트.
|
|
7
|
+
class HttpClient
|
|
8
|
+
def initialize(token_manager:, base_url:, api_version:)
|
|
9
|
+
@token_manager = token_manager
|
|
10
|
+
@base_url = base_url
|
|
11
|
+
@api_version = api_version
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def post(path, body)
|
|
15
|
+
do_post(path, body, is_retry: false)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def do_post(path, body, is_retry: false)
|
|
21
|
+
token = @token_manager.get_token
|
|
22
|
+
url = "#{@base_url}/api/#{@api_version}/#{path}"
|
|
23
|
+
uri = URI(url)
|
|
24
|
+
payload = body.to_json
|
|
25
|
+
|
|
26
|
+
req = Net::HTTP::Post.new(uri)
|
|
27
|
+
req["Content-Type"] = "application/json"
|
|
28
|
+
req["Authorization"] = bearer_auth(token)
|
|
29
|
+
req.body = payload
|
|
30
|
+
|
|
31
|
+
resp = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
32
|
+
read_timeout: 15, open_timeout: 10) { |h| h.request(req) }
|
|
33
|
+
|
|
34
|
+
resp_body = JSON.parse(resp.body) rescue {}
|
|
35
|
+
|
|
36
|
+
unless resp.is_a?(Net::HTTPSuccess)
|
|
37
|
+
error_code = resp_body["code"]
|
|
38
|
+
endpoint = path.split("/").last
|
|
39
|
+
if !is_retry && @token_manager.should_refresh?(resp.code.to_i, error_code)
|
|
40
|
+
@token_manager.invalidate
|
|
41
|
+
return do_post(path, body, is_retry: true)
|
|
42
|
+
end
|
|
43
|
+
raise SendgoError.from_response(resp.code.to_i, resp_body, endpoint, @api_version)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
resp_body
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def bearer_auth(token)
|
|
50
|
+
return "Bearer #{token}" if @api_version == "v2"
|
|
51
|
+
"Bearer #{Base64.strict_encode64(token)}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/sendgo/sms.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Sendgo
|
|
2
|
+
# SMS / LMS / MMS 전송 서비스.
|
|
3
|
+
#
|
|
4
|
+
# @example
|
|
5
|
+
# client.sms.send_sms(content: "인증번호: 123456", contacts: [{ contact: "01012345678" }])
|
|
6
|
+
class SmsService
|
|
7
|
+
def initialize(http:, sms_sender_key:)
|
|
8
|
+
@http = http
|
|
9
|
+
@sms_sender_key = sms_sender_key
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def send_sms(content:, contacts:, **opts) = send(content: content, contacts: contacts, message_type: "SMS", **opts)
|
|
13
|
+
def send_lms(content:, contacts:, **opts) = send(content: content, contacts: contacts, message_type: "LMS", **opts)
|
|
14
|
+
def send_mms(content:, contacts:, **opts) = send(content: content, contacts: contacts, message_type: "MMS", **opts)
|
|
15
|
+
|
|
16
|
+
def send(content:, contacts:, message_type: "SMS", campaign_type: "MESSAGE",
|
|
17
|
+
schedule_type: "DIRECTLY", at: nil, subject: nil, files: [])
|
|
18
|
+
body = {
|
|
19
|
+
campaignType: campaign_type, messageType: message_type,
|
|
20
|
+
scheduleType: schedule_type, at: at,
|
|
21
|
+
subject: subject, content: content,
|
|
22
|
+
files: files, contacts: contacts,
|
|
23
|
+
senderKey: @sms_sender_key,
|
|
24
|
+
}
|
|
25
|
+
@http.post("messages/send", body)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
require "base64"
|
|
4
|
+
require "monitor"
|
|
5
|
+
|
|
6
|
+
module Sendgo
|
|
7
|
+
NO_REFRESH_CODES = %w[
|
|
8
|
+
INVALID_AUTH_HEADER INVALID_BASIC_AUTH INVALID_BASIC_AUTH_PAYLOAD
|
|
9
|
+
INVALID_ACCESS_KEY INVALID_SECRET_KEY ACCESS_KEY_NOT_APPROVED
|
|
10
|
+
TEAM_REQUIRED_FOR_KAKAO IP_NOT_ALLOWED INVALID_SENDER_KEY INVALID_KAKAO_SENDER_KEY
|
|
11
|
+
].freeze
|
|
12
|
+
|
|
13
|
+
TOKEN_TTL = 50 * 60 # 50분 (초)
|
|
14
|
+
|
|
15
|
+
# 토큰 발급 및 캐시 관리.
|
|
16
|
+
class TokenManager
|
|
17
|
+
include MonitorMixin
|
|
18
|
+
|
|
19
|
+
def initialize(base_url:, access_key:, secret_key:, api_version:)
|
|
20
|
+
super()
|
|
21
|
+
@base_url = base_url
|
|
22
|
+
@access_key = access_key
|
|
23
|
+
@secret_key = secret_key
|
|
24
|
+
@api_version = api_version
|
|
25
|
+
@token = nil
|
|
26
|
+
@expires_at = Time.at(0)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def get_token
|
|
30
|
+
synchronize do
|
|
31
|
+
return @token if @token && Time.now < @expires_at
|
|
32
|
+
fetch_token
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def invalidate
|
|
37
|
+
synchronize { @token = nil; @expires_at = Time.at(0) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def should_refresh?(status, error_code)
|
|
41
|
+
return false unless [401, 403].include?(status)
|
|
42
|
+
return false if @api_version == "v2" && NO_REFRESH_CODES.include?(error_code)
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def fetch_token
|
|
49
|
+
uri = URI("#{@base_url}/api/#{@api_version}/token")
|
|
50
|
+
credentials = Base64.strict_encode64("#{@access_key}:#{@secret_key}")
|
|
51
|
+
|
|
52
|
+
req = Net::HTTP::Post.new(uri)
|
|
53
|
+
req["Content-Type"] = "application/json"
|
|
54
|
+
req["Authorization"] = "Basic #{credentials}"
|
|
55
|
+
|
|
56
|
+
resp = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |h| h.request(req) }
|
|
57
|
+
body = JSON.parse(resp.body) rescue {}
|
|
58
|
+
|
|
59
|
+
unless resp.is_a?(Net::HTTPSuccess) && body.dig("data", "token")
|
|
60
|
+
raise SendgoError.from_response(resp.code.to_i, body, "token", @api_version)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
@token = body["data"]["token"]
|
|
64
|
+
@expires_at = Time.now + TOKEN_TTL
|
|
65
|
+
@token
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
data/lib/sendgo.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require_relative "sendgo/version"
|
|
2
|
+
require_relative "sendgo/error"
|
|
3
|
+
require_relative "sendgo/token_manager"
|
|
4
|
+
require_relative "sendgo/http_client"
|
|
5
|
+
require_relative "sendgo/alimtalk"
|
|
6
|
+
require_relative "sendgo/friendtalk"
|
|
7
|
+
require_relative "sendgo/sms"
|
|
8
|
+
require_relative "sendgo/client"
|
|
9
|
+
|
|
10
|
+
# Sendgo Ruby SDK — 카카오 알림톡/친구톡, SMS/LMS/MMS
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# client = Sendgo::Client.new(
|
|
14
|
+
# access_key: ENV["SENDGO_ACCESS_KEY"],
|
|
15
|
+
# secret_key: ENV["SENDGO_SECRET_KEY"],
|
|
16
|
+
# kakao_sender_key: ENV["SENDGO_KAKAO_KEY"],
|
|
17
|
+
# sms_sender_key: ENV["SENDGO_SMS_KEY"],
|
|
18
|
+
# api_version: "v2"
|
|
19
|
+
# )
|
|
20
|
+
#
|
|
21
|
+
# client.alimtalk.send(
|
|
22
|
+
# template_code: "ORDER_CONFIRM_001",
|
|
23
|
+
# contacts: [{ contact: "01012345678", var1: "ORD-001" }]
|
|
24
|
+
# )
|
|
25
|
+
module Sendgo
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sendgo
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sendgo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-08-09 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Sendgo API를 Ruby에서 간편하게 사용하기 위한 공식 SDK
|
|
14
|
+
email:
|
|
15
|
+
- dev@sendgo.io
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/sendgo.rb
|
|
22
|
+
- lib/sendgo/alimtalk.rb
|
|
23
|
+
- lib/sendgo/client.rb
|
|
24
|
+
- lib/sendgo/error.rb
|
|
25
|
+
- lib/sendgo/friendtalk.rb
|
|
26
|
+
- lib/sendgo/http_client.rb
|
|
27
|
+
- lib/sendgo/sms.rb
|
|
28
|
+
- lib/sendgo/token_manager.rb
|
|
29
|
+
- lib/sendgo/version.rb
|
|
30
|
+
homepage: https://sendgo.io
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata: {}
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '3.1'
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.5.22
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: Sendgo Ruby SDK — 카카오 알림톡/친구톡, SMS/LMS/MMS
|
|
53
|
+
test_files: []
|