Skip to content

Commit

Permalink
Merge pull request #268 from zenizh/validate-message-objects
Browse files Browse the repository at this point in the history
Add APIs to validate message objects
  • Loading branch information
zenizh authored Nov 9, 2022
2 parents 7a064f2 + 9705d4e commit 239362c
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/line/bot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,71 @@ def get_message_delivery_broadcast(date)
get(endpoint, endpoint_path, credentials)
end

# Validate message objects of a reply message
#
# @param messages [Array] Array of message objects to validate
#
# @return [Net::HTTPResponse]
def validate_reply_message_objects(messages)
channel_token_required

endpoint_path = '/bot/message/validate/reply'
payload = { messages: messages }.to_json
post(endpoint, endpoint_path, payload, credentials)
end

# Validate message objects of a push message
#
# @param messages [Array] Array of message objects to validate
#
# @return [Net::HTTPResponse]
def validate_push_message_objects(messages)
channel_token_required

endpoint_path = '/bot/message/validate/push'
payload = { messages: messages }.to_json
post(endpoint, endpoint_path, payload, credentials)
end

# Validate message objects of a multicast message
#
# @param messages [Array] Array of message objects to validate
#
# @return [Net::HTTPResponse]
def validate_multicast_message_objects(messages)
channel_token_required

endpoint_path = '/bot/message/validate/multicast'
payload = { messages: messages }.to_json
post(endpoint, endpoint_path, payload, credentials)
end

# Validate message objects of a narrowcast message
#
# @param messages [Array] Array of message objects to validate
#
# @return [Net::HTTPResponse]
def validate_narrowcast_message_objects(messages)
channel_token_required

endpoint_path = '/bot/message/validate/narrowcast'
payload = { messages: messages }.to_json
post(endpoint, endpoint_path, payload, credentials)
end

# Validate message objects of a broadcast message
#
# @param messages [Array] Array of message objects to validate
#
# @return [Net::HTTPResponse]
def validate_broadcast_message_objects(messages)
channel_token_required

endpoint_path = '/bot/message/validate/broadcast'
payload = { messages: messages }.to_json
post(endpoint, endpoint_path, payload, credentials)
end

# Create a rich menu
#
# @param rich_menu [Hash] The rich menu represented as a rich menu object
Expand Down
110 changes: 110 additions & 0 deletions spec/line/bot/client_validate_message_objects_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'spec_helper'
require 'webmock/rspec'
require 'json'

MESSAGE_OBJECTS = {
messages: [
{
type: 'text',
text: 'Hello, world'
}
]
}

RESPONSE_BODY = <<"EOS"
{}
EOS

describe Line::Bot::Client do
def dummy_config
{
channel_token: 'access token',
}
end

def client
Line::Bot::Client.new do |config|
config.channel_token = dummy_config[:channel_token]
end
end

it 'validates message objects of a reply message' do
uri_template = Addressable::Template.new(Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/validate/reply')
stub_request(:post, uri_template).with(body: MESSAGE_OBJECTS).to_return { |request| { body: RESPONSE_BODY, status: 200 } }

messages = [
{
type: 'text',
text: 'Hello, world'
}
]

response = client.validate_reply_message_objects(messages)
expect(response).to be_a(Net::HTTPOK)
expect(JSON.parse(response.body)).to eq({})
end

it 'validates message objects of a push message' do
uri_template = Addressable::Template.new(Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/validate/push')
stub_request(:post, uri_template).with(body: MESSAGE_OBJECTS).to_return { |request| { body: RESPONSE_BODY, status: 200 } }

messages = [
{
type: 'text',
text: 'Hello, world'
}
]

response = client.validate_push_message_objects(messages)
expect(response).to be_a(Net::HTTPOK)
expect(JSON.parse(response.body)).to eq({})
end

it 'validates message objects of a multicast message' do
uri_template = Addressable::Template.new(Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/validate/multicast')
stub_request(:post, uri_template).with(body: MESSAGE_OBJECTS).to_return { |request| { body: RESPONSE_BODY, status: 200 } }

messages = [
{
type: 'text',
text: 'Hello, world'
}
]

response = client.validate_multicast_message_objects(messages)
expect(response).to be_a(Net::HTTPOK)
expect(JSON.parse(response.body)).to eq({})
end

it 'validates message objects of a narrowcast message' do
uri_template = Addressable::Template.new(Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/validate/narrowcast')
stub_request(:post, uri_template).with(body: MESSAGE_OBJECTS).to_return { |request| { body: RESPONSE_BODY, status: 200 } }

messages = [
{
type: 'text',
text: 'Hello, world'
}
]

response = client.validate_narrowcast_message_objects(messages)
expect(response).to be_a(Net::HTTPOK)
expect(JSON.parse(response.body)).to eq({})
end

it 'validates message objects of a broadcast message' do
uri_template = Addressable::Template.new(Line::Bot::API::DEFAULT_ENDPOINT + '/bot/message/validate/broadcast')
stub_request(:post, uri_template).with(body: MESSAGE_OBJECTS).to_return { |request| { body: RESPONSE_BODY, status: 200 } }

messages = [
{
type: 'text',
text: 'Hello, world'
}
]

response = client.validate_broadcast_message_objects(messages)
expect(response).to be_a(Net::HTTPOK)
expect(JSON.parse(response.body)).to eq({})
end
end

0 comments on commit 239362c

Please sign in to comment.