-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from hyperoslo/feature/welcome-messages
Implement welcome messages
- Loading branch information
Showing
4 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'httparty' | ||
|
||
module Facebook | ||
module Messenger | ||
# This module handles setting welcome messages. | ||
# | ||
# https://developers.facebook.com/docs/messenger-platform/send-api-reference#welcome_message_configuration | ||
module Welcome | ||
include HTTParty | ||
|
||
base_uri 'https://graph.facebook.com/v2.6/me' | ||
|
||
format :json | ||
|
||
module_function | ||
|
||
def set(message) | ||
response = post '/thread_settings', | ||
body: welcome_setting_for(message).to_json | ||
|
||
raise_errors(response) | ||
|
||
true | ||
end | ||
|
||
def unset | ||
response = post '/thread_settings', | ||
body: welcome_setting_for(nil).to_json | ||
|
||
raise_errors(response) | ||
|
||
true | ||
end | ||
|
||
def raise_errors(response) | ||
raise Error, response['error']['message'] if response.key? 'error' | ||
end | ||
|
||
def default_options | ||
super.merge( | ||
query: { | ||
access_token: Facebook::Messenger.config.access_token | ||
}, | ||
headers: { | ||
'Content-Type' => 'application/json' | ||
} | ||
) | ||
end | ||
|
||
def welcome_setting_for(payload) | ||
{ | ||
setting_type: 'call_to_actions', | ||
thread_state: 'new_thread', | ||
call_to_actions: payload ? [{ message: payload }] : [] | ||
} | ||
end | ||
|
||
class Error < Facebook::Messenger::Error; end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
require 'spec_helper' | ||
|
||
describe Facebook::Messenger::Welcome do | ||
let(:access_token) { '<access token>' } | ||
|
||
let(:thread_settings_url) do | ||
Facebook::Messenger::Welcome.base_uri + '/thread_settings' | ||
end | ||
|
||
before do | ||
Facebook::Messenger.configure do |config| | ||
config.access_token = access_token | ||
end | ||
end | ||
|
||
describe '.set' do | ||
context 'with a successful response' do | ||
before do | ||
stub_request(:post, thread_settings_url) | ||
.with( | ||
query: { | ||
access_token: access_token | ||
}, | ||
headers: { | ||
'Content-Type' => 'application/json' | ||
}, | ||
body: JSON.dump( | ||
setting_type: 'call_to_actions', | ||
thread_state: 'new_thread', | ||
call_to_actions: [ | ||
{ | ||
message: { | ||
text: 'Hello, human!' | ||
} | ||
} | ||
] | ||
) | ||
) | ||
.to_return( | ||
body: JSON.dump( | ||
result: "Successfully added new thread's CTAs" | ||
), | ||
status: 200, | ||
headers: default_graph_api_response_headers | ||
) | ||
end | ||
|
||
it 'returns true' do | ||
expect(subject.set(text: 'Hello, human!')).to be(true) | ||
end | ||
end | ||
|
||
context 'with an unsuccessful response' do | ||
let(:error_message) { 'Invalid OAuth access token.' } | ||
|
||
before do | ||
stub_request(:post, thread_settings_url) | ||
.with(query: { access_token: access_token }) | ||
.to_return( | ||
body: JSON.dump( | ||
'error' => { | ||
'message' => error_message, | ||
'type' => 'OAuthException', | ||
'code' => 190, | ||
'fbtrace_id' => 'Hlssg2aiVlN' | ||
} | ||
), | ||
status: 200, | ||
headers: default_graph_api_response_headers | ||
) | ||
end | ||
|
||
it 'raises an error' do | ||
expect { subject.set text: 'Hello, human!' }.to raise_error( | ||
Facebook::Messenger::Welcome::Error, error_message | ||
) | ||
end | ||
end | ||
end | ||
|
||
describe '.unset' do | ||
context 'with a successful response' do | ||
before do | ||
stub_request(:post, thread_settings_url) | ||
.with( | ||
query: { | ||
access_token: access_token | ||
}, | ||
headers: { | ||
'Content-Type' => 'application/json' | ||
}, | ||
body: JSON.dump( | ||
setting_type: 'call_to_actions', | ||
thread_state: 'new_thread', | ||
call_to_actions: [] | ||
) | ||
) | ||
.to_return( | ||
body: JSON.dump( | ||
result: "Successfully added new thread's CTAs" | ||
), | ||
status: 200, | ||
headers: default_graph_api_response_headers | ||
) | ||
end | ||
|
||
it 'returns true' do | ||
expect(subject.unset).to be(true) | ||
end | ||
end | ||
|
||
context 'with an unsuccessful response' do | ||
let(:error_message) { 'Invalid OAuth access token.' } | ||
|
||
before do | ||
stub_request(:post, thread_settings_url) | ||
.with(query: { access_token: access_token }) | ||
.to_return( | ||
body: JSON.dump( | ||
'error' => { | ||
'message' => error_message, | ||
'type' => 'OAuthException', | ||
'code' => 190, | ||
'fbtrace_id' => 'Hlssg2aiVlN' | ||
} | ||
), | ||
status: 200, | ||
headers: default_graph_api_response_headers | ||
) | ||
end | ||
|
||
it 'raises an error' do | ||
expect { subject.unset }.to raise_error( | ||
Facebook::Messenger::Welcome::Error, error_message | ||
) | ||
end | ||
end | ||
end | ||
end |