Skip to content

Commit

Permalink
Merge f84386e into fc8d8cb
Browse files Browse the repository at this point in the history
  • Loading branch information
denmarkmeralpis committed Apr 10, 2020
2 parents fc8d8cb + f84386e commit d0561a1
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@
Bot.on :delivery do |delivery|
puts "Delivered message(s) #{delivery.ids}"
end

Bot.on :reaction do |message|
message.emoji # => "👍"
message.action # => "react"
message.reaction # => "like"

message.reply(text: 'Thanks for liking my message!')
end
1 change: 1 addition & 0 deletions lib/facebook/messenger/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Bot
policy_enforcement
pass_thread_control
game_play
reaction
].freeze

class << self
Expand Down
4 changes: 3 additions & 1 deletion lib/facebook/messenger/incoming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'facebook/messenger/incoming/policy_enforcement'
require 'facebook/messenger/incoming/pass_thread_control'
require 'facebook/messenger/incoming/game_play'
require 'facebook/messenger/incoming/message_reaction'

module Facebook
module Messenger
Expand All @@ -35,7 +36,8 @@ module Incoming
'payment' => Payment,
'policy_enforcement' => PolicyEnforcement,
'pass_thread_control' => PassThreadControl,
'game_play' => GamePlay
'game_play' => GamePlay,
'reaction' => MessageReaction
}.freeze

# Parse the given payload and create new object of class related
Expand Down
23 changes: 23 additions & 0 deletions lib/facebook/messenger/incoming/message_reaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Facebook
module Messenger
module Incoming
# The Message echo class represents an incoming Facebook Messenger message
# @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-reactions
class MessageReaction
include Facebook::Messenger::Incoming::Common

def action
@messaging['reaction']['action']
end

def emoji
@messaging['reaction']['emoji']
end

def reaction
@messaging['reaction']['reaction']
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/facebook/messenger/bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@
subject.receive({})
end
end

context 'with a :reaction' do
let(:reaction) do
Facebook::Messenger::Incoming::MessageReaction.new({})
end

it 'triggers a :delivery' do
expect(Facebook::Messenger::Incoming).to receive(:parse)
.and_return(reaction)

expect(Facebook::Messenger::Bot).to receive(:trigger)
.with(:reaction, reaction)

subject.receive({})
end
end
end

describe '.trigger' do
Expand Down
58 changes: 58 additions & 0 deletions spec/facebook/messenger/incoming/message_reaction_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'spec_helper'

describe Facebook::Messenger::Incoming::MessageReaction do
let :payload do
{
'sender' => {
'id' => '3'
},
'recipient' => {
'id' => '3'
},
'timestamp' => 145_776_419_762_7,
'reaction' => {
'action' => 'react',
'emoji' => '👍',
'reaction' => 'like'
}
}
end

subject { Facebook::Messenger::Incoming::MessageReaction.new(payload) }

describe '.messaging' do
it 'returns the original payload' do
expect(subject.messaging).to eq(payload)
end
end

describe '.sender' do
it 'returns the sender' do
expect(subject.sender).to eq(payload['sender'])
end
end

describe '.recipient' do
it 'returns the recipient' do
expect(subject.recipient).to eq(payload['recipient'])
end
end

describe '.action' do
it 'returns the reaction action' do
expect(subject.action).to eq(payload['reaction']['action'])
end
end

describe '.emoji' do
it 'returns the reaction emoji' do
expect(subject.emoji).to eq(payload['reaction']['emoji'])
end
end

describe '.reaction' do
it 'returns the reaction reaction' do
expect(subject.reaction).to eq(payload['reaction']['reaction'])
end
end
end

0 comments on commit d0561a1

Please sign in to comment.