Skip to content

Commit

Permalink
Merge pull request #35 from hyperoslo/feature/welcome-messages
Browse files Browse the repository at this point in the history
Implement welcome messages
  • Loading branch information
jgorset committed May 18, 2016
2 parents f104546 + 316aadd commit f51ce97
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ Bot.on :delivery do |delivery|
end
```

#### Set a welcome message

You can greet new humans to entice them into talking to you:

```ruby
Facebook::Messenger::Welcome.set text: 'Hello, human!'
```

## Configuration

### Create an Application on Facebook
Expand Down
1 change: 1 addition & 0 deletions lib/facebook/messenger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'facebook/messenger/version'
require 'facebook/messenger/error'
require 'facebook/messenger/subscriptions'
require 'facebook/messenger/welcome'
require 'facebook/messenger/bot'
require 'facebook/messenger/server'
require 'facebook/messenger/configuration'
Expand Down
61 changes: 61 additions & 0 deletions lib/facebook/messenger/welcome.rb
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
139 changes: 139 additions & 0 deletions spec/facebook/messenger/welcome_spec.rb
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

0 comments on commit f51ce97

Please sign in to comment.