Skip to content

Commit 0ac2b4e

Browse files
committed
Merge branch 'master' into feature/endpoints_for_friend_statistics
2 parents b00df4e + 72faea0 commit 0ac2b4e

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lib/line/bot/client.rb

+26
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ def broadcast(messages)
177177
post(endpoint, endpoint_path, payload, credentials)
178178
end
179179

180+
# Narrowcast messages to users
181+
#
182+
# API Documentation is here.
183+
# https://developers.line.biz/en/reference/messaging-api/#send-narrowcast-message
184+
#
185+
# @param messages [Hash or Array]
186+
# @param recipient [Hash]
187+
# @param filter [Hash]
188+
# @param limit [Hash]
189+
#
190+
# @return [Net::HTTPResponse]
191+
def narrowcast(messages, recipient: nil, filter: nil, limit: nil)
192+
channel_token_required
193+
194+
messages = [messages] if messages.is_a?(Hash)
195+
196+
endpoint_path = '/bot/message/narrowcast'
197+
payload = {
198+
messages: messages,
199+
recipient: recipient,
200+
filter: filter,
201+
limit: limit
202+
}.to_json
203+
post(endpoint, endpoint_path, payload, credentials)
204+
end
205+
180206
def leave_group(group_id)
181207
channel_token_required
182208

spec/line/bot/send_message_01_text_spec.rb

+61
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,65 @@
9898
}.to_json
9999
expect(response.body).to eq(expected)
100100
end
101+
102+
it 'narrowcast the text message without any conditions' do
103+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/narrowcast'
104+
stub_request(:post, uri_template).to_return { |request| {body: request.body, status: 200} }
105+
106+
client = Line::Bot::Client.new do |config|
107+
config.channel_token = 'channel_token'
108+
end
109+
110+
message = {
111+
type: 'text',
112+
text: 'Hello, world'
113+
}
114+
response = client.narrowcast(message)
115+
116+
expected = {
117+
messages: [message],
118+
recipient: nil,
119+
filter: nil,
120+
limit: nil
121+
}.to_json
122+
expect(response.body).to eq(expected)
123+
end
124+
125+
it 'narrowcast the text message' do
126+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/narrowcast'
127+
stub_request(:post, uri_template).to_return { |request| {body: request.body, status: 200} }
128+
129+
client = Line::Bot::Client.new do |config|
130+
config.channel_token = 'channel_token'
131+
end
132+
133+
message = {
134+
type: 'text',
135+
text: 'Hello, world'
136+
}
137+
recipient = {
138+
type: 'audience',
139+
audienceGroupId: 5614991017776
140+
}
141+
filter = {
142+
demographic: {
143+
type: 'appType',
144+
oneOf: [
145+
'android'
146+
]
147+
}
148+
}
149+
limit = {
150+
max: 300
151+
}
152+
response = client.narrowcast(message, recipient: recipient, filter: filter, limit: limit)
153+
154+
expected = {
155+
messages: [message],
156+
recipient: recipient,
157+
filter: filter,
158+
limit: limit
159+
}.to_json
160+
expect(response.body).to eq(expected)
161+
end
101162
end

0 commit comments

Comments
 (0)