@@ -8,118 +8,266 @@ Ruby bindings for the Intercom API (https://api.intercom.io).
8
8
9
9
For generating Intercom javascript script tags for Rails, please see https://github.com/intercom/intercom-rails
10
10
11
+ ## Upgrading information
12
+ Version 2 of intercom-ruby is not backwards compatible with previous versions. Be sure to test this new version before deploying to production. One other change you will need to make as part of the upgrade is to set ` Intercom.app_api_key ` and not set ` Intercom.api_key ` (you can continue to use your existing API key).
13
+
11
14
## Installation
12
15
13
16
gem install intercom
14
17
15
18
Using bundler:
16
19
17
- gem 'intercom'
20
+ gem 'intercom', "~> 2.0"
18
21
19
22
## Basic Usage
20
23
21
24
### Configure your access credentials
22
25
23
26
``` ruby
24
27
Intercom .app_id = " my_app_id"
25
- Intercom .api_key = " my-super-crazy-api-key"
28
+ Intercom .app_api_key = " my-super-crazy-api-key"
26
29
```
27
30
28
31
### Resources
29
32
30
- The API supports:
33
+ Resources this API supports:
31
34
32
- POST,PUT,GET https://api.intercom.io/v1/users
33
- POST,PUT,GET https://api.intercom.io/v1/users/messages
34
- POST https://api.intercom.io/v1/users/impressions
35
- POST https://api.intercom.io/v1/users/notes
35
+ https://api.intercom.io/users
36
+ https://api.intercom.io/companies
37
+ https://api.intercom.io/tags
38
+ https://api.intercom.io/notes
39
+ https://api.intercom.io/segments
40
+ https://api.intercom.io/events
41
+ https://api.intercom.io/conversations
42
+ https://api.intercom.io/messages
43
+ https://api.intercom.io/counts
36
44
37
45
### Examples
38
46
39
47
#### Users
40
48
41
49
``` ruby
42
- user
= Intercom ::
User .find_by_email(
" [email protected] " )
43
- user.custom_data[" average_monthly_spend" ] = 1234.56
44
- user.save
45
- user = Intercom ::User .find_by_user_id(" 1" )
50
+ # Find user by email
51
+ user
= Intercom ::
User .find(
:email =>
" [email protected] " )
52
+ # Find user by user_id
53
+ user = Intercom ::User .find(:user_id => " 1" )
54
+ # Find user by id
55
+ user = Intercom ::User .find(:id => " 1" )
56
+ # Create a user
46
57
user
= Intercom ::
User .create(
:email =>
" [email protected] " ,
:name =>
" Bob Smith" )
47
- user = Intercom ::User .new (params)
48
- user.save
49
- Intercom ::User .all.count
50
- Intercom ::User .all.each {|user | puts %Q( #{ user.email } - #{ user.custom_data[" average_monthly_spend" ] } ) }
58
+ # Update custom_attributes for a user
59
+ user.custom_attributes[" average_monthly_spend" ] = 1234.56 ; user.save
60
+ # Perform incrementing
61
+ user.increment(' karma' ); user.save
62
+ # Iterate over all users
63
+ Intercom ::User .all.each {|user | puts %Q( #{ user.email } - #{ user.custom_attributes[" average_monthly_spend" ] } ) }
51
64
Intercom ::User .all.map {|user | user.email }
52
65
```
53
66
54
67
#### Companies
55
68
``` ruby
56
- user
= Intercom ::
User .find_by_email(
" [email protected] " )
57
- user.company = {:id => 6 , :name => " Intercom" }
58
- user.companies = [{:id => 6 , :name => " Intercom" }, {:id => 9 , :name => " Test Company" }]
69
+ # Add a user to one or more companies
70
+ user
= Intercom ::
User .find(
:email =>
" [email protected] " )
71
+ user.companies = [{:company_id => 6 , :name => " Intercom" }, {:company_id => 9 , :name => " Test Company" }]; user.save
72
+ # You can also pass custom attributes within a company as you do this
73
+ user.companies = [{:id => 6 , :name => " Intercom" , :custom_attributes => {:referral_source => " Google" } } ]; user.save
74
+ # Find a company by company_id
75
+ company = Intercom ::Company .find(:company_id => " 44" )
76
+ # Find a company by name
77
+ company = Intercom ::Company .find(:name => " Some company" )
78
+ # Find a company by id
79
+ company = Intercom ::Company .find(:id => " 41e66f0313708347cb0000d0" )
80
+ # Update a company
81
+ company.name = ' Updated company name' ; company.save
82
+ # Iterate over all companies
83
+ Intercom ::Company .all.each {|company | puts %Q( #{ company.name } - #{ company.custom_attributes[" referral_source" ] } ) }
84
+ Intercom ::Company .all.map {|company | company.name }
85
+ # Get a list of users in a company
86
+ company.users
59
87
```
60
88
61
- You can also pass custom data within a company:
89
+ #### Tags
90
+ ``` ruby
91
+ # Tag users
92
+ tag = Intercom ::Tag .tag_users(' blue' , [" 42ea2f1b93891f6a99000427" ])
93
+ # Untag users
94
+ Intercom ::Tag .untag_users(' blue' , [" 42ea2f1b93891f6a99000427" ])
95
+ # Iterate over all tags
96
+ Intercom ::Tag .all.each {|tag | " #{ tag.id } - #{ tag.name } " }
97
+ Intercom ::Tag .all.map {|tag | tag.name }
98
+ # Iterate over all tags for user
99
+ Intercom ::Tag .find_all_for_user(:id => ' 53357ddc3c776629e0000029' )
100
+ Intercom ::
Tag .find_all_for_user(
:email =>
' [email protected] ' )
101
+ Intercom ::Tag .find_all_for_user(:user_id => ' 3' )
102
+ # Tag companies
103
+ tag = Intercom ::Tag .tag_companies(' red' , [" 42ea2f1b93891f6a99000427" ])
104
+ # Untag companies
105
+ Intercom ::Tag .untag_users(' blue' , [" 42ea2f1b93891f6a99000427" ])
106
+ # Iterate over all tags for company
107
+ Intercom ::Tag .find_all_for_company(:id => ' 43357e2c3c77661e25000026' )
108
+ Intercom ::Tag .find_all_for_company(:company_id => ' 6' )
109
+ ```
62
110
111
+ #### Segments
63
112
``` ruby
64
- user.company = {:id => 6 , :name => " Intercom" , :referral_source => " Google" }
113
+ # Find a segment
114
+ segment = Intercom ::Segment .find(:id => segment_id)
115
+ # Update a segment
116
+ segment.name = ' Updated name' ; segment.save
117
+ # Iterate over all segments
118
+ Intercom ::Segment .all.each {|segment | puts " id: #{ segment.id } name: #{ segment.name } " }
65
119
```
66
120
67
- #### Message Threads
121
+ #### Notes
68
122
``` ruby
69
- Intercom ::
MessageThread .create(
:email =>
" [email protected] " ,
:body =>
" Example message from [email protected] to your application on Intercom." )
70
- Intercom ::
MessageThread .find(
:email =>
" [email protected] " ,
:thread_id =>
123 )
71
- Intercom ::
MessageThread .find_all(
:email =>
" [email protected] " )
72
- Intercom ::
MessageThread .mark_as_read(
:email =>
" [email protected] " ,
:thread_id =>
123 )
123
+ # Find a note by id
124
+ note = Intercom ::Note .find(:id => note)
125
+ # Create a note for a user
126
+ note
= Intercom ::
Note .create(
:body =>
" <p>Text for the note</p>" ,
:email =>
' [email protected] ' )
127
+ # Iterate over all notes for a user via their email address
128
+ Intercom ::
Note .find_all(
:email =>
' [email protected] ' ).each {|
note |
puts note.body}
129
+ # Iterate over all notes for a user via their user_id
130
+ Intercom ::Note .find_all(:user_id => ' 123' ).each {|note | puts note.body}
73
131
```
74
132
75
- #### Impressions
133
+ #### Conversations
76
134
``` ruby
77
- Intercom ::
Impression .create(
:email =>
" [email protected] " ,
:location =>
" /path/in/my/app" ,
:user_ip =>
" 1.2.3.4" ,
:user_agent =>
" my-savage-iphone-app-0.1"
135
+ # FINDING CONVERSATIONS FOR AN ADMIN
136
+ # Iterate over all conversations (open and closed) assigned to an admin
137
+ Intercom ::Conversation .find_all(:type => ' admin' , :id => ' 7' ).each do {|convo | ... }
138
+ # Iterate over all open conversations assigned to an admin
139
+ Intercom ::Conversation .find_all(:type => ' admin' , :id => 7 , :open => true ).each do {|convo | ... }
140
+ # Iterate over closed conversations assigned to an admin
141
+ Intercom ::Conversation .find_all(:type => ' admin' , :id => 7 , :open => false ).each do {|convo | ... }
142
+ # Iterate over closed conversations for assigned an admin, before a certain moment in time
143
+ Intercom ::Conversation .find_all(:type => ' admin' , :id => 7 , :open => false , :before => 1374844930 ).each do {|convo | ... }
144
+
145
+ # FINDING CONVERSATIONS FOR A USER
146
+ # Iterate over all conversations (read + unread, correct) with a user based on the users email
147
+ Intercom ::
Conversation .find_all(
:email =>
' [email protected] ' ,
:type =>
' user' ).each
do {|
convo | ... }
148
+ # Iterate over through all conversations (read + unread) with a user based on the users email
149
+ Intercom ::
Conversation .find_all(
:email =>
' [email protected] ' ,
:type =>
' user' ,
:unread =>
false ).each
do {|
convo | ... }
150
+ # Iterate over all unread conversations with a user based on the users email
151
+ Intercom ::
Conversation .find_all(
:email =>
' [email protected] ' ,
:type =>
' user' ,
:unread =>
true ).each
do {|
convo | ... }
152
+
153
+ # FINDING A SINGLE CONVERSATION
154
+ conversation = Intercom ::Conversation .find(:id => ' 1' )
155
+
156
+ # INTERACTING WITH THE PARTS OF A CONVERSATION
157
+ # Getting the subject of a part (only applies to email-based conversations)
158
+ conversation.rendered_message.subject
159
+ # Get the part_type of the first part
160
+ conversation.conversation_parts[0 ].part_type
161
+ # Get the body of the second part
162
+ conversation.conversation_parts[1 ].body
163
+
164
+ # REPLYING TO CONVERSATIONS
165
+ # User (identified by email) replies with a comment
166
+ conversation.reply(
:type =>
' user' ,
:email =>
' [email protected] ' ,
:message_type =>
' comment' ,
:body =>
' foo' )
167
+ # Admin (identified by email) replies with a comment
168
+ conversation.reply(
:type =>
' admin' ,
:email =>
' [email protected] ' ,
:message_type =>
' comment' ,
:body =>
' bar' )
78
169
```
79
170
80
- #### Notes
171
+ #### Counts
81
172
``` ruby
82
- Intercom ::
Note .create(
:email =>
" [email protected] " ,
:body =>
" This is the text of the note" )
173
+ # Get Conversation per Admin
174
+ conversation_counts_for_each_admin = Intercom ::Count .conversation_counts_for_each_admin
175
+ conversation_counts_for_each_admin.each{|count | puts " Admin: #{ count.name } (id: #{ count.id } ) Open: #{ count.open } Closed: #{ count.closed } " }
176
+ # Get User Tag Count Object
177
+ Intercom ::Count .user_counts_for_each_tag
178
+ # Get User Segment Count Object
179
+ Intercom ::Count .user_counts_for_each_segment
180
+ # Get Company Segment Count Object
181
+ Intercom ::Count .company_counts_for_each_segment
182
+ # Get Company Tag Count Object
183
+ Intercom ::Count .company_counts_for_each_tag
184
+ # Get Company User Count Object
185
+ Intercom ::Count .company_counts_for_each_user
186
+ # Get total count of companies, users, segments or tags across app
187
+ Intercom ::Company .count
188
+ Intercom ::User .count
189
+ Intercom ::Segment .count
190
+ Intercom ::Tag .count
83
191
```
84
192
85
- #### Events
86
- The simplest way to create an event is directly on the user
193
+ #### Full loading of and embedded entity
87
194
``` ruby
88
- user
= Intercom :: User .find_by_email( " [email protected] " )
89
- user.track_event( " invited-friend " )
195
+ # Given a converation with a partial user, load the full user. This can be done for any entity
196
+ conversation. user.load
90
197
```
91
198
92
- For more control create events through Intercom::Event
199
+ #### Sending messages
93
200
``` ruby
94
- Intercom ::Event .create(:event_name => " invited-friend" , :user => user)
95
201
96
- # With an explicit event creation date
97
- Intercom ::Event .create(:event_name => " invited-friend" , :user => user, :created_at => 1391691571 )
202
+ # InApp message from admin to user
203
+ Intercom ::Message .create({
204
+ :message_type => ' inapp' ,
205
+ :body => " What's up :)" ,
206
+ :from => {
207
+ :type => ' admin' ,
208
+ :id => " 1234"
209
+ },
210
+ :to => {
211
+ :type => :user ,
212
+ :id => " 5678"
213
+ }
214
+ })
98
215
99
- # With metadata
100
- Intercom ::Event .create(:event_name => " invited-friend" , :user => user,
101
- metadata => {
102
- :invitee_email =>
' [email protected] ' ,
103
- :invite_code => ' ADDAFRIEND'
216
+ # Email message from admin to user
217
+ Intercom ::Message .create({
218
+ :message_type => ' email' ,
219
+ :subject => ' Hey there' ,
220
+ :body => " What's up :)" ,
221
+ :template => " plain" , # or "personal",
222
+ :from => {
223
+ :type => " admin" ,
224
+ :id => " 1234"
225
+ },
226
+ :to => {
227
+ :type => " user" ,
228
+ :id => " 536e564f316c83104c000020"
229
+ }
230
+ })
231
+
232
+ # Message from a user
233
+ Intercom ::Message .create({
234
+ :from => {
235
+ :type => " user" ,
236
+ :id => " 536e564f316c83104c000020"
237
+ },
238
+ :body => " halp"
239
+ })
240
+ ```
241
+
242
+ #### Events
243
+ ``` ruby
244
+ Intercom ::Event .create(
245
+ :event_name => " invited-friend" , :created_at => Time .now.to_i,
246
+ :email => user.email,
247
+ :metadata => {
248
+ " invitee_email" =>
" [email protected] " ,
249
+ :invite_code => " ADDAFRIEND" ,
250
+ " found_date" => 12909364407
104
251
}
105
252
)
106
253
```
107
254
108
- Metadata Objects support a few simple types that Intercom can present on your behalf
255
+ Metadata Objects support a few simple types that Intercom can present on your behalf
109
256
110
257
``` ruby
111
- Intercom ::Event .create(:event_name => " placed-order" , :user => current_user,
112
- metadata => {
258
+ Intercom ::Event .create(:event_name => " placed-order" , :email => current_user.email,
259
+ :created_at => 1403001013
260
+ :metadata => {
113
261
:order_date => Time .now.to_i,
114
- :stripe_invoice => ' inv_3434343434' ,
115
- :order_number => {
116
- :value => ' 3434-3434' ,
117
- :url => ' https://example.org/orders/3434-3434'
262
+ :stripe_invoice => ' inv_3434343434' ,
263
+ :order_number => {
264
+ :value => ' 3434-3434' ,
265
+ :url => ' https://example.org/orders/3434-3434'
118
266
},
119
- price: {
267
+ price: {
120
268
:currency => ' usd' ,
121
- :amount => 2999
122
- }
269
+ :amount => 2999
270
+ }
123
271
}
124
272
)
125
273
```
@@ -131,9 +279,17 @@ The metadata key values in the example are treated as follows-
131
279
- price: An Amount in US Dollars (value contains 'amount' and 'currency' keys)
132
280
133
281
### Errors
282
+ You do not need to deal with the HTTP response from an API call directly. If there is an unsuccessful response then an error that is a subclass of Intercom: Error will be raised. If desired, you can get at the http_code of an Intercom::Error via it's ` http_code ` method.
283
+
284
+ The list of different error subclasses are listed below. As they all inherit off Intercom::Error you can choose to rescue Intercom::Error or
285
+ else rescue the more specific error subclass.
286
+
134
287
``` ruby
135
288
Intercom ::AuthenticationError
136
289
Intercom ::ServerError
137
290
Intercom ::ServiceUnavailableError
138
291
Intercom ::ResourceNotFound
292
+ Intercom ::BadRequestError
293
+ Intercom ::RateLimitExceeded
294
+ Intercom ::AttributeNotSetError # Raised when you try to call a getter that does not exist on an object
139
295
```
0 commit comments