Skip to content

Commit 15d507a

Browse files
authored
feat: Support LINE notification messages API (#326)
1 parent 660ae22 commit 15d507a

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

lib/line/bot/client.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,36 @@ def get_delivery_result_sent_by_phone_numbers(date)
12071207
get(endpoint, endpoint_path, credentials)
12081208
end
12091209

1210+
# Send a LINE notification message by specifying the user's phone number.
1211+
#
1212+
# @param hashed_phone_number [String] Phone number that has been normalized.
1213+
# @param messages [Hash, Array] Message Objects.
1214+
# @param headers [Hash] HTTP Headers.
1215+
# @param payload [Hash] Additional request body.
1216+
#
1217+
# @return [Net::HTTPResponse]
1218+
def push_pnp(hashed_phone_number, messages, headers: {}, payload: {})
1219+
channel_token_required
1220+
1221+
messages = [messages] if messages.is_a?(Hash)
1222+
1223+
endpoint_path = '/bot/pnp/push'
1224+
payload = payload.merge({ to: hashed_phone_number, messages: messages }).to_json
1225+
post(oauth_endpoint, endpoint_path, payload, credentials.merge(headers))
1226+
end
1227+
1228+
# Get the number of LINE notification messages sent using the /bot/pnp/push endpoint.
1229+
#
1230+
# @param date [String] Date the messages were sent (format: yyyyMMdd).
1231+
#
1232+
# @return [Net::HTTPResponse]
1233+
def get_message_delivery_pnp(date)
1234+
channel_token_required
1235+
1236+
endpoint_path = "/bot/message/delivery/pnp?date=#{date}"
1237+
get(endpoint, endpoint_path, credentials)
1238+
end
1239+
12101240
# Fetch data, get content of specified URL.
12111241
#
12121242
# @param endpoint_base [String]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'spec_helper'
2+
require 'webmock/rspec'
3+
4+
DELIVERY_RESULT_CONTENT = <<~"EOS"
5+
{
6+
"status": "ready",
7+
"success": 3
8+
}
9+
EOS
10+
11+
describe Line::Bot::Client do
12+
describe '#push_pnp' do
13+
before do
14+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_OAUTH_ENDPOINT + '/bot/pnp/push'
15+
stub_request(:post, uri_template).to_return { |request| { body: '{}', status: 200 } }
16+
end
17+
18+
let(:client) do
19+
Line::Bot::Client.new do |config|
20+
config.channel_token = 'channel_token'
21+
end
22+
end
23+
let(:phone_number) { "+818000001234" }
24+
let(:hashed_phone_number) { Digest::SHA256.hexdigest(phone_number) }
25+
let(:x_line_delivery_tag) { 'd41e0ad70dddfeb68f149ad6fc61574b9c5780ab7bcb2fba5517771ffbb2409c' }
26+
let(:message) do
27+
{
28+
type: 'text',
29+
text: 'Hello, world'
30+
}
31+
end
32+
33+
context 'normal scenario' do
34+
it 'pushes the message' do
35+
response = client.push_pnp(hashed_phone_number, message)
36+
37+
expect(response).to be_a(Net::HTTPOK)
38+
expect(response.body).to eq('{}')
39+
end
40+
end
41+
42+
context 'with X-Line-Delivery-Tag header' do
43+
it 'pushes the message with additional header' do
44+
response = client.push_pnp(hashed_phone_number, message, headers: { 'X-Line-Delivery-Tag' => x_line_delivery_tag })
45+
46+
expect(response).to be_a(Net::HTTPOK)
47+
expect(response.body).to eq('{}')
48+
end
49+
end
50+
end
51+
52+
describe '#get_message_delivery_pnp' do
53+
before do
54+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/delivery/pnp?date={date}'
55+
stub_request(:get, uri_template).to_return { |request| { body: DELIVERY_RESULT_CONTENT, status: 200 } }
56+
end
57+
58+
let(:client) do
59+
Line::Bot::Client.new do |config|
60+
config.channel_token = 'channel_token'
61+
end
62+
end
63+
64+
it 'gets the delivery result' do
65+
response = client.get_message_delivery_pnp('20240524')
66+
67+
expect(response).to be_a(Net::HTTPOK)
68+
expect(response.body).to eq(DELIVERY_RESULT_CONTENT)
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)