Skip to content

[feature] Conversation : Add/remove nested resource 'contact' #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Resources this API supports:
https://api.intercom.io/counts
https://api.intercom.io/subscriptions
https://api.intercom.io/jobs


### Examples

Expand Down Expand Up @@ -99,7 +99,7 @@ contacts = intercom.contacts.search(
}
)
contacts.each {|c| p c.email}
# For full detail on possible queries, please refer to the API documentation:
# For full detail on possible queries, please refer to the API documentation:
# https://developers.intercom.com/intercom-api-reference/reference

# Merge a lead into an existing user
Expand Down Expand Up @@ -191,7 +191,7 @@ intercom.data_attributes.save(attribute)
attribute.archived = true
intercom.data_attributes.save(attribute)

# Find all customer attributes including archived
# Find all customer attributes including archived
customer_attributes_incl_archived = intercom.data_attributes.find_all({"model": "contact", "include_archived": true})
customer_attributes_incl_archived.each { |attr| p attribute.name }
```
Expand Down Expand Up @@ -321,7 +321,7 @@ conversation.statistics.time_to_admin_reply
conversation.statistics.last_assignment_at

# Get information on the sla applied to a conversation
conversation.sla_applied.sla_name
conversation.sla_applied.sla_name

# REPLYING TO CONVERSATIONS
# User (identified by email) replies with a comment
Expand Down Expand Up @@ -370,9 +370,9 @@ intercom.conversations.mark_read(conversation.id)
intercom.conversations.run_assignment_rules(conversation.id)

# Search for conversations
# For full detail on possible queries, please refer to the API documentation:
# https://developers.intercom.com/intercom-api-reference/reference
# For full detail on possible queries, please refer to the API documentation:
# https://developers.intercom.com/intercom-api-reference/reference

# Search for open conversations sorted by the created_at date
conversations = intercom.conversations.search(
query: {
Expand All @@ -386,18 +386,23 @@ conversations = intercom.conversations.search(
conversations.each {|c| p c.id}

# Tagging for conversations
tag = intercom.tags.find(id: "2")
tag = intercom.tags.find(id: "2")
conversation = intercom.conversations.find(id: "1")

# An Admin ID is required to add or remove tag on a conversation
admin = intercom.admins.find(id: "1")
admin = intercom.admins.find(id: "1")

# Add a tag to a conversation
conversation.add_tag(id: tag.id, admin_id: admin.id)

# Remove a tag from a conversation
conversation.remove_tag(id: tag.id, admin_id: admin.id)


# Add a contact to a conversation
conversation.add_contact(admin_id: admin.id, customer: { intercom_user_id: contact.id })

# Remove a contact from a conversation
conversation.remove_contact(id: contact.id, admin_id: admin.id)
```

#### Full loading of an embedded entity
Expand Down
1 change: 1 addition & 0 deletions lib/intercom/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class Conversation
include ApiOperations::NestedResource

nested_resource_methods :tag, operations: %i[add delete]
nested_resource_methods :contact, operations: %i[add delete], path: :customers
end
end
26 changes: 26 additions & 0 deletions spec/unit/intercom/conversation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,31 @@
client.expects(:delete).with("/conversations/1/tags/1", { "id": tag.id }).returns(nil)
_(proc { conversation.remove_tag({ "id": tag.id }) }).must_raise Intercom::HttpError
end

describe 'contacts' do
let(:response) do
{
customers: [
{ type: test_contact['type'], id: test_contact['id'] }
]
}
end

it 'adds a contact to a conversation' do
client.expects(:post)
.with("/conversations/1/customers",
{ admin_id: test_admin['id'], customer: { intercom_user_id: test_contact['id'] } })
.returns(response.to_hash)
conversation.add_contact(admin_id: test_admin['id'], customer: { intercom_user_id: test_contact['id'] })
end

it 'removes a contact from a conversation' do
client.expects(:delete)
.with("/conversations/1/customers/aaaaaaaaaaaaaaaaaaaaaaaa",
{ id: 'aaaaaaaaaaaaaaaaaaaaaaaa', admin_id: '1234' })
.returns(response.to_hash)
conversation.remove_contact(id: test_contact['id'], admin_id: test_admin['id'])
end
end
end
end