Skip to content

Commit 699738d

Browse files
committed
Handle incoming webhook notifications
1 parent b781fcd commit 699738d

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

lib/intercom.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require "intercom/admin"
1111
require "intercom/count"
1212
require "intercom/request"
13+
require "intercom/webhook_payload"
1314
require "intercom/utils"
1415
require "intercom/errors"
1516
require "json"

lib/intercom/webhook_payload.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'intercom/traits/api_resource'
2+
3+
module Intercom
4+
module Webhook
5+
class Payload
6+
include Traits::ApiResource
7+
8+
# This method could be renamed, e.g. response_object or model
9+
def to_model
10+
data.item
11+
end
12+
13+
def model_type
14+
to_model.class
15+
end
16+
17+
def load
18+
to_model.load
19+
end
20+
end
21+
end
22+
end

spec/spec_helper.rb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,136 @@ def test_tag
132132
}
133133
end
134134

135+
def test_webhook_user
136+
{
137+
"type" => "notification_event",
138+
"id" => "notif_123456-56465-546546",
139+
"topic" => "user.created",
140+
"app_id" => "aaaaaa",
141+
"data" =>
142+
{
143+
"type" => "notification_event_data",
144+
"item" =>
145+
{
146+
"type" => "user",
147+
"id" => "aaaaaaaaaaaaaaaaaaaaaaaa",
148+
"user_id" => nil,
149+
"email" => "[email protected]",
150+
"name" => "Joe Schmoe",
151+
"avatar" =>
152+
{
153+
"type" => "avatar",
154+
"image_url" => nil
155+
},
156+
"app_id" => "aaaaa",
157+
"companies" =>
158+
{
159+
"type" => "company.list",
160+
"companies" => [ ]
161+
},
162+
"location_data" =>
163+
{
164+
},
165+
"last_request_at" => nil,
166+
"created_at" => "1401970114",
167+
"remote_created_at" => nil,
168+
"updated_at" => "1401970114",
169+
"session_count" => 0,
170+
"social_profiles" =>
171+
{
172+
"type" => "social_profile.list",
173+
"social_profiles" => [ ]
174+
},
175+
"unsubscribed_from_emails" => false,
176+
"user_agent_data" => nil,
177+
"tags" =>
178+
{
179+
"type" => "tag.list",
180+
"tags" => [ ]
181+
},
182+
"segments" =>
183+
{
184+
"type" => "segment.list",
185+
"segments" => [ ]
186+
},
187+
"custom_attributes" =>
188+
{
189+
}
190+
}
191+
},
192+
"delivery_status" => nil,
193+
"delivery_attempts" => 1,
194+
"delivered_at" => 0,
195+
"first_sent_at" => 1410188629,
196+
"created_at" => 1410188628,
197+
"links" => { },
198+
"self" => nil
199+
}
200+
end
201+
202+
def test_webhook_conversation
203+
{
204+
"type"=>"notification_event",
205+
"id"=>"notif_123456-56465-546546",
206+
"topic"=>"conversation.user.created",
207+
"app_id"=>"aaaaa",
208+
"data"=>
209+
{
210+
"type"=>"notification_event_data",
211+
"item"=>
212+
{
213+
"type"=>"conversation",
214+
"id"=>"123456789",
215+
"created_at"=>"1410335293",
216+
"updated_at"=>"1410335293",
217+
"user"=>
218+
{
219+
"type"=>"user",
220+
"id"=>"540f1de7112d3d1d51001637",
221+
"name"=>"Kill Bill",
222+
"email"=>"[email protected]"},
223+
"assignee"=>
224+
{
225+
"type"=>"nobody_admin",
226+
"id"=>nil
227+
},
228+
"conversation_message"=>
229+
{
230+
"type"=>"conversation_message",
231+
"id"=>"321546",
232+
"subject"=>"",
233+
"body"=>"<p>An important message</p>",
234+
"author"=>
235+
{
236+
"type"=>"user",
237+
"id"=>"aaaaaaaaaaaaaaaaaaaaaa",
238+
"name"=>"Kill Bill",
239+
"email"=>"[email protected]"},
240+
"attachments"=>[]
241+
},
242+
"conversation_parts"=>
243+
{
244+
"type"=>"conversation_part.list",
245+
"conversation_parts"=>[]
246+
},
247+
"open"=>nil,
248+
"read"=>true,
249+
"links"=>
250+
{
251+
"conversation_web"=>
252+
"https://app.intercom.io/a/apps/aaaaaa/inbox/all/conversations/123456789"}
253+
}
254+
},
255+
"delivery_status"=>nil,
256+
"delivery_attempts"=>1,
257+
"delivered_at"=>0,
258+
"first_sent_at"=>1410335293,
259+
"created_at"=>1410335293,
260+
"links"=>{},
261+
"self"=>nil
262+
}
263+
end
264+
135265
def error_on_modify_frozen
136266
RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
137267
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'spec_helper'
2+
3+
describe "Intercom::Webhook::Payload" do
4+
5+
it "converts webhook hash to object" do
6+
payload = Intercom::Webhook::Payload.new(test_webhook_user)
7+
payload.must_be_instance_of Intercom::Webhook::Payload
8+
end
9+
10+
it "returns correct model type for User" do
11+
payload = Intercom::Webhook::Payload.new(test_webhook_user)
12+
payload.model_type.must_equal Intercom::User
13+
end
14+
15+
it "returns correct notification topic" do
16+
payload = Intercom::Webhook::Payload.new(test_webhook_user)
17+
payload.topic.must_equal "user.created"
18+
end
19+
20+
it "returns instance of User" do
21+
payload = Intercom::Webhook::Payload.new(test_webhook_user)
22+
payload.to_model.must_be_instance_of Intercom::User
23+
end
24+
25+
it "returns instance of Conversation" do
26+
payload = Intercom::Webhook::Payload.new(test_webhook_conversation)
27+
payload.to_model.must_be_instance_of Intercom::Conversation
28+
end
29+
30+
it "returns correct model type for User" do
31+
payload = Intercom::Webhook::Payload.new(test_webhook_conversation)
32+
payload.model_type.must_equal Intercom::Conversation
33+
end
34+
35+
it "returns correct notification topic" do
36+
payload = Intercom::Webhook::Payload.new(test_webhook_conversation)
37+
payload.topic.must_equal "conversation.user.created"
38+
end
39+
40+
it "returns inner User object for Conversation" do
41+
payload = Intercom::Webhook::Payload.new(test_webhook_conversation)
42+
payload.to_model.user.must_be_instance_of Intercom::User
43+
end
44+
45+
end

0 commit comments

Comments
 (0)