Skip to content

Commit

Permalink
Merge pull request #135 from fcatuhe/messenger-profile
Browse files Browse the repository at this point in the history
Messenger profile
  • Loading branch information
jgorset committed Apr 8, 2017
2 parents a47df79 + fe65c3a commit 0ce1531
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 67 deletions.
86 changes: 54 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ end
Show the human you are preparing a message for them:

```ruby
Bot.on :message do |message|
Bot.on :message do |message|
message.typing_on

# Do something expensive
Expand All @@ -137,7 +137,7 @@ end
Or that you changed your mind:

```ruby
Bot.on :message do |message|
Bot.on :message do |message|
message.typing_on

if # something
Expand Down Expand Up @@ -204,55 +204,77 @@ Bot.on :referral do |referral|
end
```

#### Change thread settings
#### Change messenger profile

You can greet new humans to entice them into talking to you:
You can greet new humans to entice them into talking to you, in different locales:

```ruby
Facebook::Messenger::Thread.set({
setting_type: 'greeting',
greeting: {
text: 'Welcome to your new bot overlord!'
},
Facebook::Messenger::Profile.set({
greeting: [
{
locale: 'default',
text: 'Welcome to your new bot overlord!'
},
{
locale: 'fr_FR',
text: 'Bienvenue dans le bot du Wagon !'
}
]
}, access_token: ENV['ACCESS_TOKEN'])
```

You can define the action to trigger when new humans click on the Get
Started button. Before doing it you should check to select the messaging_postbacks field when setting up your webhook.

```ruby
Facebook::Messenger::Thread.set({
setting_type: 'call_to_actions',
thread_state: 'new_thread',
call_to_actions: [
{
payload: 'DEVELOPER_DEFINED_PAYLOAD_FOR_WELCOME'
}
]
Facebook::Messenger::Profile.set({
get_started: {
payload: 'GET_STARTED_PAYLOAD'
}
}, access_token: ENV['ACCESS_TOKEN'])
```

You can show a persistent menu to humans.

```ruby
Facebook::Messenger::Thread.set({
setting_type: 'call_to_actions',
thread_state: 'existing_thread',
call_to_actions: [
Facebook::Messenger::Profile.set({
persistent_menu: [
{
type: 'postback',
title: 'Help',
payload: 'DEVELOPER_DEFINED_PAYLOAD_FOR_HELP'
},
{
type: 'postback',
title: 'Start a New Order',
payload: 'DEVELOPER_DEFINED_PAYLOAD_FOR_START_ORDER'
locale: 'default',
composer_input_disabled: true,
call_to_actions: [
{
title: 'My Account',
type: 'nested',
call_to_actions: [
{
title: 'What's a chatbot?',
type: 'postback',
payload: 'EXTERMINATE'
},
{
title: 'History',
type: 'postback',
payload: 'HISTORY_PAYLOAD'
},
{
title: 'Contact Info',
type: 'postback',
payload: 'CONTACT_INFO_PAYLOAD'
}
]
},
{
type: 'web_url',
title: 'Get some help',
url: 'https://github.com/hyperoslo/facebook-messenger',
webview_height_ratio: 'full'
}
]
},
{
type: 'web_url',
title: 'View Website',
url: 'http://example.com/'
locale: 'zh_CN',
composer_input_disabled: false
}
]
}, access_token: ENV['ACCESS_TOKEN'])
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require 'rspec/core/rake_task'
RuboCop::RakeTask.new
RSpec::Core::RakeTask.new(:spec)

task default: %i(rubocop spec)
task default: %i[rubocop spec]
2 changes: 1 addition & 1 deletion lib/facebook/messenger.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'facebook/messenger/version'
require 'facebook/messenger/error'
require 'facebook/messenger/subscriptions'
require 'facebook/messenger/thread'
require 'facebook/messenger/profile'
require 'facebook/messenger/bot'
require 'facebook/messenger/server'
require 'facebook/messenger/configuration'
Expand Down
4 changes: 2 additions & 2 deletions lib/facebook/messenger/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Bot

base_uri 'https://graph.facebook.com/v2.6/me'

EVENTS = %i(
EVENTS = %i[
message delivery postback optin read account_linking referral
).freeze
].freeze

class << self
# Deliver a message with the given payload.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Facebook
module Messenger
# This module handles changing thread settings.
# This module handles changing messenger profile (menu, start & greeting).
#
# https://developers.facebook.com/docs/messenger-platform/thread-settings
module Thread
# https://developers.facebook.com/docs/messenger-platform/messenger-profile
module Profile
include HTTParty

base_uri 'https://graph.facebook.com/v2.6/me'
Expand All @@ -15,7 +15,7 @@ module Thread
module_function

def set(settings, access_token:)
response = post '/thread_settings', body: settings.to_json, query: {
response = post '/messenger_profile', body: settings.to_json, query: {
access_token: access_token
}

Expand All @@ -25,7 +25,7 @@ def set(settings, access_token:)
end

def unset(settings, access_token:)
response = delete '/thread_settings', body: settings.to_json, query: {
response = delete '/messenger_profile', body: settings.to_json, query: {
access_token: access_token
}

Expand Down
55 changes: 29 additions & 26 deletions spec/facebook/messenger/thread_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'spec_helper'

describe Facebook::Messenger::Thread do
describe Facebook::Messenger::Profile do
let(:access_token) { 'access token' }

let(:thread_settings_url) do
Facebook::Messenger::Thread.base_uri + '/thread_settings'
let(:messenger_profile_url) do
Facebook::Messenger::Profile.base_uri + '/messenger_profile'
end

before do
Expand All @@ -14,7 +14,7 @@
describe '.set' do
context 'with a successful response' do
before do
stub_request(:post, thread_settings_url)
stub_request(:post, messenger_profile_url)
.with(
query: {
access_token: access_token
Expand All @@ -23,14 +23,14 @@
'Content-Type' => 'application/json'
},
body: JSON.dump(
setting_type: 'call_to_actions',
thread_state: 'new_thread',
call_to_actions: [{ payload: 'USER_DEFINED_PAYLOAD' }]
get_started: {
payload: 'GET_STARTED_PAYLOAD'
}
)
)
.to_return(
body: JSON.dump(
result: "Successfully added new thread's CTAs"
result: 'Successfully added Get Started button'
),
status: 200,
headers: default_graph_api_response_headers
Expand All @@ -39,9 +39,9 @@

let :options do
{
setting_type: 'call_to_actions',
thread_state: 'new_thread',
call_to_actions: [{ payload: 'USER_DEFINED_PAYLOAD' }]
get_started: {
payload: 'GET_STARTED_PAYLOAD'
}
}
end

Expand All @@ -54,7 +54,7 @@
let(:error_message) { 'Invalid OAuth access token.' }

before do
stub_request(:post, thread_settings_url)
stub_request(:post, messenger_profile_url)
.with(query: { access_token: access_token })
.to_return(
body: JSON.dump(
Expand All @@ -73,14 +73,14 @@
it 'raises an error' do
expect do
options = {
setting_type: 'call_to_actions',
thread_state: 'new_thread',
call_to_actions: [{ payload: 'USER_DEFINED_PAYLOAD' }]
get_started: {
payload: 'GET_STARTED_PAYLOAD'
}
}

subject.set(options, access_token: access_token)
end.to raise_error(
Facebook::Messenger::Thread::Error, error_message
Facebook::Messenger::Profile::Error, error_message
)
end
end
Expand All @@ -89,7 +89,7 @@
describe '.unset' do
context 'with a successful response' do
before do
stub_request(:delete, thread_settings_url)
stub_request(:delete, messenger_profile_url)
.with(
query: {
access_token: access_token
Expand All @@ -98,13 +98,14 @@
'Content-Type' => 'application/json'
},
body: JSON.dump(
setting_type: 'call_to_actions',
thread_state: 'new_thread'
fields: [
'get_started'
]
)
)
.to_return(
body: JSON.dump(
result: "Successfully added new thread's CTAs"
result: 'Successfully deleted Get Started button'
),
status: 200,
headers: default_graph_api_response_headers
Expand All @@ -113,8 +114,9 @@

let :options do
{
setting_type: 'call_to_actions',
thread_state: 'new_thread'
fields: [
'get_started'
]
}
end

Expand All @@ -127,7 +129,7 @@
let(:error_message) { 'Invalid OAuth access token.' }

before do
stub_request(:delete, thread_settings_url)
stub_request(:delete, messenger_profile_url)
.with(query: { access_token: access_token })
.to_return(
body: JSON.dump(
Expand All @@ -146,12 +148,13 @@
it 'raises an error' do
expect do
options = {
setting_type: 'call_to_actions',
thread_state: 'new_thread'
fields: [
'get_started'
]
}
subject.unset(options, access_token: access_token)
end.to raise_error(
Facebook::Messenger::Thread::Error, error_message
Facebook::Messenger::Profile::Error, error_message
)
end
end
Expand Down

0 comments on commit 0ce1531

Please sign in to comment.