Skip to content

Commit c4a8f99

Browse files
authored
Merge pull request #183 from line/audience-group-api
Add audience API endpoints
2 parents 3e7eb70 + f916f6d commit c4a8f99

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed

lib/line/bot/client.rb

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,154 @@ def delete_liff_app(liff_id)
673673
delete(liff_endpoint, endpoint_path, credentials)
674674
end
675675

676+
# Create an audience group by uploading user_ids
677+
#
678+
# Parameters are described here.
679+
# https://developers.line.biz/en/reference/messaging-api/#create-upload-audience-group
680+
#
681+
# @param params [Hash] options
682+
#
683+
# @return [Net::HTTPResponse] This response includes an audience_group_id.
684+
def create_user_id_audience(params)
685+
channel_token_required
686+
687+
endpoint_path = '/bot/audienceGroup/upload'
688+
post(endpoint, endpoint_path, params.to_json, credentials)
689+
end
690+
691+
# Update an audience group
692+
#
693+
# Parameters are described here.
694+
# https://developers.line.biz/en/reference/messaging-api/#update-upload-audience-group
695+
#
696+
# @param params [Hash] options
697+
#
698+
# @return [Net::HTTPResponse] This response includes an audience_group_id.
699+
def update_user_id_audience(params)
700+
channel_token_required
701+
702+
endpoint_path = '/bot/audienceGroup/upload'
703+
put(endpoint, endpoint_path, params.to_json, credentials)
704+
end
705+
706+
# Create an audience group of users that clicked a URL in a message sent in the past
707+
#
708+
# Parameters are described here.
709+
# https://developers.line.biz/en/reference/messaging-api/#create-click-audience-group
710+
#
711+
# @param params [Hash] options
712+
#
713+
# @return [Net::HTTPResponse] This response includes an audience_group_id.
714+
def create_click_audience(params)
715+
channel_token_required
716+
717+
endpoint_path = '/bot/audienceGroup/click'
718+
post(endpoint, endpoint_path, params.to_json, credentials)
719+
end
720+
721+
# Create an audience group of users that opened a message sent in the past
722+
#
723+
# Parameters are described here.
724+
# https://developers.line.biz/en/reference/messaging-api/#create-imp-audience-group
725+
#
726+
# @param params [Hash] options
727+
#
728+
# @return [Net::HTTPResponse] This response includes an audience_group_id.
729+
def create_impression_audience(params)
730+
channel_token_required
731+
732+
endpoint_path = '/bot/audienceGroup/imp'
733+
post(endpoint, endpoint_path, params.to_json, credentials)
734+
end
735+
736+
# Rename an existing audience group
737+
#
738+
# @param audience_group_id [Integer]
739+
# @param description [String]
740+
#
741+
# @return [Net::HTTPResponse]
742+
def rename_audience(audience_group_id, description)
743+
channel_token_required
744+
745+
endpoint_path = "/bot/audienceGroup/#{audience_group_id}/updateDescription"
746+
body = {description: description}
747+
put(endpoint, endpoint_path, body.to_json, credentials)
748+
end
749+
750+
# Delete an existing audience group
751+
#
752+
# Parameters are described here.
753+
# https://developers.line.biz/en/reference/messaging-api/#delete-audience-group
754+
#
755+
# @param audience_group_id [Integer]
756+
#
757+
# @return [Net::HTTPResponse]
758+
def delete_audience(audience_group_id)
759+
channel_token_required
760+
761+
endpoint_path = "/bot/audienceGroup/#{audience_group_id}"
762+
delete(endpoint, endpoint_path, credentials)
763+
end
764+
765+
# Get audience group data
766+
#
767+
# Parameters are described here.
768+
# https://developers.line.biz/en/reference/messaging-api/#get-audience-group
769+
#
770+
# @param audience_group_id [Integer]
771+
#
772+
# @return [Net::HTTPResponse]
773+
def get_audience(audience_group_id)
774+
channel_token_required
775+
776+
endpoint_path = "/bot/audienceGroup/#{audience_group_id}"
777+
get(endpoint, endpoint_path, credentials)
778+
end
779+
780+
# Get data for more than one audience group
781+
#
782+
# Parameters are described here.
783+
# https://developers.line.biz/en/reference/messaging-api/#get-audience-groups
784+
#
785+
# @param params [Hash] key name `page` is required
786+
#
787+
# @return [Net::HTTPResponse]
788+
def get_audiences(params)
789+
channel_token_required
790+
791+
endpoint_path = "/bot/audienceGroup/list?" + URI.encode_www_form(params)
792+
get(endpoint, endpoint_path, credentials)
793+
end
794+
795+
# Get the authority level of the audience
796+
#
797+
# Parameters are described here.
798+
# https://developers.line.biz/en/reference/messaging-api/#get-authority-level
799+
#
800+
# @return [Net::HTTPResponse]
801+
def get_audience_authority_level
802+
channel_token_required
803+
804+
endpoint_path = "/bot/audienceGroup/authorityLevel"
805+
get(endpoint, endpoint_path, credentials)
806+
end
807+
808+
# Change the authority level of the audience
809+
#
810+
# Parameters are described here.
811+
# https://developers.line.biz/en/reference/messaging-api/#change-authority-level
812+
#
813+
# @param authority_level [String] value must be `PUBLIC` or `PRIVATE`
814+
#
815+
# @return [Net::HTTPResponse]
816+
def update_audience_authority_level(authority_level)
817+
channel_token_required
818+
819+
endpoint_path = "/bot/audienceGroup/authorityLevel"
820+
body = {authorityLevel: authority_level}
821+
put(endpoint, endpoint_path, body.to_json, credentials)
822+
end
823+
676824
# Fetch data, get content of specified URL.
677825
#
678826
# @param endpoint_base [String]

spec/line/bot/client_audience_spec.rb

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
require 'spec_helper'
2+
require 'json'
3+
require_relative 'mock_http_client'
4+
5+
describe Line::Bot::Client do
6+
def generate_client
7+
Line::Bot::Client.new do |config|
8+
config.channel_id = '1234567'
9+
config.channel_token = 'channelToken'
10+
config.channel_secret = 'channelSecret'
11+
config.httpclient = MockHTTPClient.new
12+
end
13+
end
14+
15+
it 'execute create_user_id_audience' do
16+
payload = {
17+
description: 'create_user_id_audience 1',
18+
audiences: [{id: 'u123abc'}]
19+
}
20+
21+
response = generate_client.create_user_id_audience(payload)
22+
23+
expect(response).to eq(
24+
method: :post,
25+
url: 'https://api.line.me/v2/bot/audienceGroup/upload',
26+
payload: payload.to_json
27+
)
28+
end
29+
30+
it 'execute update_user_id_audience' do
31+
payload = {
32+
audienceGroupId: 123,
33+
description: 'audience 1',
34+
audiences: [{id: 'u123abc'}]
35+
}
36+
37+
response = generate_client.update_user_id_audience(payload)
38+
39+
expect(response).to eq(
40+
method: :put,
41+
url: 'https://api.line.me/v2/bot/audienceGroup/upload',
42+
payload: payload.to_json
43+
)
44+
end
45+
46+
it 'execute create_click_audience' do
47+
payload = {
48+
description: 'audience 2',
49+
requestId: 'a523d8b2-4728-4ac5-ad4f-b176afd43267'
50+
}
51+
52+
response = generate_client.create_click_audience(payload)
53+
54+
expect(response).to eq(
55+
method: :post,
56+
url: 'https://api.line.me/v2/bot/audienceGroup/click',
57+
payload: payload.to_json
58+
)
59+
end
60+
61+
it 'execute create_impression_audience' do
62+
payload = {
63+
description: 'audience 3',
64+
requestId: 'a523d8b2-4728-4ac5-ad4f-b176afd43267'
65+
}
66+
67+
response = generate_client.create_impression_audience(payload)
68+
69+
expect(response).to eq(
70+
method: :post,
71+
url: 'https://api.line.me/v2/bot/audienceGroup/imp',
72+
payload: payload.to_json
73+
)
74+
end
75+
76+
it 'execute rename_audience' do
77+
response = generate_client.rename_audience(123, 'audience 10')
78+
79+
expect(response).to eq(
80+
method: :put,
81+
url: 'https://api.line.me/v2/bot/audienceGroup/123/updateDescription',
82+
payload: {description: 'audience 10'}.to_json
83+
)
84+
end
85+
86+
it 'execute delete_audience' do
87+
response = generate_client.delete_audience(123)
88+
89+
expect(response).to eq(
90+
method: :delete,
91+
url: 'https://api.line.me/v2/bot/audienceGroup/123',
92+
payload: nil
93+
)
94+
end
95+
96+
it 'execute get_audience' do
97+
response = generate_client.get_audience(123)
98+
99+
expect(response).to eq(
100+
method: :get,
101+
url: 'https://api.line.me/v2/bot/audienceGroup/123',
102+
payload: nil
103+
)
104+
end
105+
106+
it 'execute get_audiences' do
107+
params = {page: 2, status: 'READY'}
108+
response = generate_client.get_audiences(params)
109+
110+
expect(response).to eq(
111+
method: :get,
112+
url: 'https://api.line.me/v2/bot/audienceGroup/list?page=2&status=READY',
113+
payload: nil
114+
)
115+
end
116+
117+
it 'execute get_audience_authority_level' do
118+
response = generate_client.get_audience_authority_level
119+
120+
expect(response).to eq(
121+
method: :get,
122+
url: 'https://api.line.me/v2/bot/audienceGroup/authorityLevel',
123+
payload: nil
124+
)
125+
end
126+
127+
it 'execute update_audience_authority_level' do
128+
response = generate_client.update_audience_authority_level('PRIVATE')
129+
130+
expect(response).to eq(
131+
method: :put,
132+
url: 'https://api.line.me/v2/bot/audienceGroup/authorityLevel',
133+
payload: {authorityLevel: 'PRIVATE'}.to_json
134+
)
135+
end
136+
end

spec/line/bot/mock_http_client.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class MockHTTPClient
2+
def get(url, _header = {})
3+
build_mock_response(:get, url, nil)
4+
end
5+
6+
def post(url, payload, _header = {})
7+
build_mock_response(:post, url, payload)
8+
end
9+
10+
def put(url, payload, _header = {})
11+
build_mock_response(:put, url, payload)
12+
end
13+
14+
def delete(url, _header = {})
15+
build_mock_response(:delete, url, nil)
16+
end
17+
18+
private
19+
20+
def build_mock_response(method, url, payload)
21+
{
22+
method: method,
23+
url: url,
24+
payload: payload
25+
}
26+
end
27+
end

0 commit comments

Comments
 (0)