Skip to content

Commit

Permalink
Merge fbccfed into be8097c
Browse files Browse the repository at this point in the history
  • Loading branch information
jgorset committed Sep 6, 2016
2 parents be8097c + fbccfed commit 094b460
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ Bot.deliver(
)
```

##### Messages with quick replies

The human may appreciate hints:

```ruby
Bot.deliver(
recipient: {
id: '45123'
},
message: {
text: 'Human, who is your favorite bot?'
quick_replies: [
{
content_type: 'text',
title: 'You are!',
payload: 'HARMLESS'
}
]
}
)
```

##### Messages with buttons

The human may require simple options to communicate:
Expand Down
11 changes: 9 additions & 2 deletions examples/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
include Facebook::Messenger

Bot.on :message do |message|
puts "Received #{message.text} from #{message.sender}"
puts "Received '#{message.inspect}' from #{message.sender}"

case message.text
when /hello/i
Bot.deliver(
recipient: message.sender,
message: {
text: 'Hello, human!'
text: 'Hello, human!',
quick_replies: [
{
content_type: 'text',
title: 'Hello, bot!',
payload: 'HELLO_BOT'
}
]
}
)
when /something humans like/i
Expand Down
6 changes: 4 additions & 2 deletions lib/facebook/messenger/incoming/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def attachments
@messaging['message']['attachments']
end

def quick_replies
@messaging['message']['quick_reply']
def quick_reply
return nil unless @messaging['message']['quick_reply']

@messaging['message']['quick_reply']['payload']
end
end
end
Expand Down
24 changes: 20 additions & 4 deletions spec/facebook/messenger/incoming/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
'mid' => 'mid.1457764197618:41d102a3e1ae206a38',
'seq' => 73,
'text' => 'Hello, bot!',
'quick_reply' => 'Hi, I am a quick reply!',
'quick_reply' => {
'payload' => 'Hi, I am a quick reply!'
},
'attachments' => [{
'type' => 'image',
'payload' => {
Expand Down Expand Up @@ -75,9 +77,23 @@
end
end

describe '.quick_replies' do
it 'returns the message quick_replies' do
expect(subject.quick_replies).to eq(payload['message']['quick_reply'])
describe '.quick_reply' do
context 'when a quick reply was used' do
it 'returns the payload of the quick reply' do
expect(subject.quick_reply).to eq(
payload['message']['quick_reply']['payload']
)
end
end

context 'when a quick reply was not used' do
before do
payload['message'].delete('quick_reply')
end

it 'returns nil' do
expect(subject.quick_reply).to eq(nil)
end
end
end
end

0 comments on commit 094b460

Please sign in to comment.